Skip to content

Commit b9d7c49

Browse files
authored
Merge pull request #12 from productsupcom/ACC-1167
Adds Process object
2 parents 2107709 + 52fd0a5 commit b9d7c49

5 files changed

Lines changed: 171 additions & 1 deletion

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ require_once(__DIR__.'/Platform-API-Client/autoload.php');
2424
* [PHP cURL](http://php.net/manual/en/curl.installation.php)
2525

2626
# Basic usage
27-
see the [example scripts](https://github.com/productsupcom/Platform-API-Client/tree/master/examples/Service).
27+
See the [example scripts](https://github.com/productsupcom/Platform-API-Client/tree/master/examples/Service).
28+
29+
# API Documentation
30+
31+
See [the documentation page](http://api-docs.productsup.io/)
2832

2933

examples/Service/Process.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use Productsup\Platform\Process;
4+
5+
include __DIR__.'/../../autoload.php';
6+
7+
/**
8+
* Authentication
9+
*
10+
* You'll get the client id and secret at the plaform (API Access)
11+
**/
12+
$Client = new Productsup\Client();
13+
$Client->id = 4558;
14+
$Client->secret = 'b011f60ae834da8a8054d149b5ed5727';
15+
16+
/**
17+
* Initialize the Sites Service where you can
18+
*
19+
* Create a new site (Sites->insert())
20+
* Delete a site (Sites->delete())
21+
* Get a list of sites (Sites->get())
22+
*/
23+
$processCall = new Productsup\Service\Process($Client);
24+
25+
/**
26+
* to get a certain site you may pass a reference,
27+
* how references are created is explained later
28+
*/
29+
$reference = new \Productsup\Platform\Site\Reference();
30+
$reference->setKey($reference::REFERENCE_SITE);
31+
$reference->setValue(434456); // Site ID
32+
33+
/**
34+
* Triggering an action
35+
*
36+
* Valid actions are:
37+
* - import: triggers an import
38+
* - export-all: triggers all exports and channels
39+
* - export: triggers an export (old style), action_id parameter with export id is required
40+
* - channel: triggers a channel (new style), action_id parameter with channel is required
41+
* - all: triggers an import and all exports and channels
42+
*/
43+
$processModel = new Process();
44+
$processModel->action = 'channel';
45+
$processModel->action_id = 51395;
46+
$processModel->addReference($reference);
47+
// This also works, but using a reference is preferred
48+
// $processModel->site_id = $reference->getValue();
49+
50+
// The results reveals whether jenkins accepted the job or, but not does not say
51+
// anything about if the job is already started or queued, in most cases
52+
// it will run immediately
53+
$result = $processCall->post($processModel);
54+
55+
var_dump($result);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Productsup\Platform;
4+
5+
use Productsup\Exceptions\ClientException;
6+
use Productsup\Platform\Site\Reference;
7+
8+
class Process extends DataModel {
9+
public $action;
10+
public $action_id;
11+
public $site_id;
12+
13+
/**
14+
* adds a reference to a site that can later be used as an identifier
15+
* note: this is only possible when creating a site or project
16+
* @param Reference $reference
17+
* @throws ClientException When adding non site reference
18+
*/
19+
public function addReference(Reference $reference) {
20+
if ($reference->getKey() != Reference::REFERENCE_SITE) {
21+
throw new ClientException('Process only accepts site as a reference.');
22+
}
23+
24+
$this->site_id = $reference->getValue();
25+
parent::addReference($reference);
26+
}
27+
28+
/**
29+
* cast data to an array
30+
* @param boolean $full
31+
* @return array
32+
*/
33+
public function toArray($full = true) {
34+
$data = array(
35+
'id' => $this->action_id,
36+
'action' => $this->action,
37+
);
38+
39+
if ($full) {
40+
$data['site_id'] = $this->site_id;
41+
}
42+
43+
return $data;
44+
}
45+
}

lib/Productsup/Platform/Site/Reference.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,12 @@ public function setValue($value)
6666
public function getValue() {
6767
return $this->_value;
6868
}
69+
70+
/**
71+
* get the defined key
72+
* @return string|null
73+
*/
74+
public function getKey() {
75+
return $this->_key;
76+
}
6977
}

lib/Productsup/Service/Process.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Productsup\Service;
4+
5+
use Productsup\Exceptions\ClientException;
6+
use Productsup\Http\Request;
7+
use Productsup\Platform\Process as ProcessModel;
8+
9+
class Process extends Service {
10+
11+
protected $serviceName = 'process';
12+
protected $validActions = array(
13+
'import', 'export', 'channel', 'all', 'export-all'
14+
);
15+
protected $actionsRequiringId = array('export', 'channel');
16+
17+
// Not applicable to process
18+
protected function getDataModel()
19+
{
20+
return new \Productsup\Platform\Process();
21+
}
22+
23+
/**
24+
* Trigger an action for a site
25+
*
26+
* @param ProcessModel $model
27+
*
28+
* @return bool
29+
*
30+
* @throws ClientException When an invalid model is given
31+
*/
32+
public function post(ProcessModel $model) {
33+
if (!$model->site_id) {
34+
throw new ClientException('A site id is required for a process.');
35+
}
36+
if (!in_array($model->action, $this->validActions)) {
37+
throw new ClientException(sprintf(
38+
'Only the following actions are allowed: %s',
39+
implode(', ', $this->validActions)
40+
));
41+
}
42+
if (in_array($model->action, $this->actionsRequiringId) && !$model->action_id) {
43+
throw new ClientException('An export or channel id needs to be set for this action.');
44+
}
45+
46+
$request = $this->getRequest();
47+
$request->method = Request::METHOD_POST;
48+
$request->postBody = $model->toArray(false);
49+
$request->url .= '/'.$model->site_id;
50+
$data = $this->executeRequest($request);
51+
52+
if (isset($data['success'])) {
53+
return $data['success'];
54+
}
55+
56+
return false;
57+
}
58+
}

0 commit comments

Comments
 (0)