-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from productsupcom/ACC-1167
Adds Process object
- Loading branch information
Showing
5 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |