Skip to content

Commit

Permalink
Merge pull request #12 from productsupcom/ACC-1167
Browse files Browse the repository at this point in the history
Adds Process object
  • Loading branch information
m038 authored Sep 13, 2017
2 parents 2107709 + 52fd0a5 commit b9d7c49
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ require_once(__DIR__.'/Platform-API-Client/autoload.php');
* [PHP cURL](http://php.net/manual/en/curl.installation.php)

# Basic usage
see the [example scripts](https://github.com/productsupcom/Platform-API-Client/tree/master/examples/Service).
See the [example scripts](https://github.com/productsupcom/Platform-API-Client/tree/master/examples/Service).

# API Documentation

See [the documentation page](http://api-docs.productsup.io/)


55 changes: 55 additions & 0 deletions examples/Service/Process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

use Productsup\Platform\Process;

include __DIR__.'/../../autoload.php';

/**
* Authentication
*
* You'll get the client id and secret at the plaform (API Access)
**/
$Client = new Productsup\Client();
$Client->id = 4558;
$Client->secret = 'b011f60ae834da8a8054d149b5ed5727';

/**
* Initialize the Sites Service where you can
*
* Create a new site (Sites->insert())
* Delete a site (Sites->delete())
* Get a list of sites (Sites->get())
*/
$processCall = new Productsup\Service\Process($Client);

/**
* to get a certain site you may pass a reference,
* how references are created is explained later
*/
$reference = new \Productsup\Platform\Site\Reference();
$reference->setKey($reference::REFERENCE_SITE);
$reference->setValue(434456); // Site ID

/**
* Triggering an action
*
* Valid actions are:
* - import: triggers an import
* - export-all: triggers all exports and channels
* - export: triggers an export (old style), action_id parameter with export id is required
* - channel: triggers a channel (new style), action_id parameter with channel is required
* - all: triggers an import and all exports and channels
*/
$processModel = new Process();
$processModel->action = 'channel';
$processModel->action_id = 51395;
$processModel->addReference($reference);
// This also works, but using a reference is preferred
// $processModel->site_id = $reference->getValue();

// The results reveals whether jenkins accepted the job or, but not does not say
// anything about if the job is already started or queued, in most cases
// it will run immediately
$result = $processCall->post($processModel);

var_dump($result);
45 changes: 45 additions & 0 deletions lib/Productsup/Platform/Process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Productsup\Platform;

use Productsup\Exceptions\ClientException;
use Productsup\Platform\Site\Reference;

class Process extends DataModel {
public $action;
public $action_id;
public $site_id;

/**
* adds a reference to a site that can later be used as an identifier
* note: this is only possible when creating a site or project
* @param Reference $reference
* @throws ClientException When adding non site reference
*/
public function addReference(Reference $reference) {
if ($reference->getKey() != Reference::REFERENCE_SITE) {
throw new ClientException('Process only accepts site as a reference.');
}

$this->site_id = $reference->getValue();
parent::addReference($reference);
}

/**
* cast data to an array
* @param boolean $full
* @return array
*/
public function toArray($full = true) {
$data = array(
'id' => $this->action_id,
'action' => $this->action,
);

if ($full) {
$data['site_id'] = $this->site_id;
}

return $data;
}
}
8 changes: 8 additions & 0 deletions lib/Productsup/Platform/Site/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ public function setValue($value)
public function getValue() {
return $this->_value;
}

/**
* get the defined key
* @return string|null
*/
public function getKey() {
return $this->_key;
}
}
58 changes: 58 additions & 0 deletions lib/Productsup/Service/Process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Productsup\Service;

use Productsup\Exceptions\ClientException;
use Productsup\Http\Request;
use Productsup\Platform\Process as ProcessModel;

class Process extends Service {

protected $serviceName = 'process';
protected $validActions = array(
'import', 'export', 'channel', 'all', 'export-all'
);
protected $actionsRequiringId = array('export', 'channel');

// Not applicable to process
protected function getDataModel()
{
return new \Productsup\Platform\Process();
}

/**
* Trigger an action for a site
*
* @param ProcessModel $model
*
* @return bool
*
* @throws ClientException When an invalid model is given
*/
public function post(ProcessModel $model) {
if (!$model->site_id) {
throw new ClientException('A site id is required for a process.');
}
if (!in_array($model->action, $this->validActions)) {
throw new ClientException(sprintf(
'Only the following actions are allowed: %s',
implode(', ', $this->validActions)
));
}
if (in_array($model->action, $this->actionsRequiringId) && !$model->action_id) {
throw new ClientException('An export or channel id needs to be set for this action.');
}

$request = $this->getRequest();
$request->method = Request::METHOD_POST;
$request->postBody = $model->toArray(false);
$request->url .= '/'.$model->site_id;
$data = $this->executeRequest($request);

if (isset($data['success'])) {
return $data['success'];
}

return false;
}
}

0 comments on commit b9d7c49

Please sign in to comment.