|
| 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