diff --git a/src/Evotor/Client.php b/src/Evotor/Client.php index 83b2231..982e5e4 100644 --- a/src/Evotor/Client.php +++ b/src/Evotor/Client.php @@ -10,7 +10,6 @@ class Client { - protected $api_key; protected $app_key; @@ -29,7 +28,8 @@ class Client protected $is_called = false; - public function __construct($api_key,$app_key='',$options=[]) { + public function __construct($api_key, $app_key='', $options=[]) + { $this->api_key = $api_key; $this->app_key = $app_key; $this->client = new Guzzle(array_replace_recursive([ @@ -42,10 +42,11 @@ public function __construct($api_key,$app_key='',$options=[]) { //'X-Authorization'=>$this->api, 'Authorization'=>'bearer '.$this->api_key, ], - ],$options)); + ], $options)); } - public function _request($method,$uri,$data,$filter = null) { + public function _request($method, $uri, $data, $filter = null) + { $resp = null; $this->http_code = null; @@ -56,10 +57,10 @@ public function _request($method,$uri,$data,$filter = null) { $options = $data; try { - $resp = $this->client->request($method,$uri,$options); - } catch(TransferException $e) { - if($e instanceof TransferException) { - if($e->hasResponse() && ($resp = $e->getResponse()) ) { + $resp = $this->client->request($method, $uri, $options); + } catch (TransferException $e) { + if ($e instanceof TransferException) { + if ($e->hasResponse() && ($resp = $e->getResponse())) { $this->http_code = $resp->getStatusCode(); $this->http_message = $resp->getReasonPhrase(); } else { @@ -71,7 +72,7 @@ public function _request($method,$uri,$data,$filter = null) { $this->http_message = $e->getMessage(); return null; } - } catch(\Exception $e) { + } catch (\Exception $e) { $this->http_code = 0; $this->http_message = 'Library error'; } finally { @@ -79,42 +80,50 @@ public function _request($method,$uri,$data,$filter = null) { $this->http_message = $resp->getReasonPhrase(); } - $this->response = new Response($this,$resp,$filter); + $this->response = new Response($this, $resp, $filter); $this->requested = []; return $this->response; } - public function getClient() { + public function getClient() + { return $this->client; } - public function __call($name,$arguments=[]) { - $op = OperationFactory::fromName($this,$name,$arguments); + public function __call($name, $arguments=[]) + { + $op = OperationFactory::fromName($this, $name, $arguments); return $op->run(); } - public function getHttpErrorMessage() { + public function getHttpErrorMessage() + { return $this->http_message; } - public function getHttpErrorCode() { + public function getHttpErrorCode() + { return $this->http_code; } - public function getErrorMessage() { + public function getErrorMessage() + { return $this->error_message; } - public function getErrorCode() { + public function getErrorCode() + { return $this->error_code; } - public function isOk() { - return strpos((string)$this->http_code,'2') === 0; + public function isOk() + { + return strpos((string)$this->http_code, '2') === 0; } - public function getAppKey() { + public function getAppKey() + { return $this->app_key; } } diff --git a/src/Evotor/Exception.php b/src/Evotor/Exception.php index adc0718..81e7198 100644 --- a/src/Evotor/Exception.php +++ b/src/Evotor/Exception.php @@ -2,4 +2,6 @@ namespace Kily\API\Evotor; -class Exception extends \Exception {} +class Exception extends \Exception +{ +} diff --git a/src/Evotor/Operations/BulksOperation.php b/src/Evotor/Operations/BulksOperation.php index 4348717..fdc246a 100644 --- a/src/Evotor/Operations/BulksOperation.php +++ b/src/Evotor/Operations/BulksOperation.php @@ -4,38 +4,39 @@ use Kily\API\Evotor\Client; -class BulksOperation extends Operation { - - const PATH = 'bulks'; +class BulksOperation extends Operation +{ + public const PATH = 'bulks'; protected $path = self::PATH; protected $allowed_methods = ['get']; protected $id; - public function run(Operation $prev = null) { + public function run(Operation $prev = null) + { return $this; } - protected function init($args) { + protected function init($args) + { $id = $args[0] ?? null; - if($id) { + if ($id) { $this->id($id); } } - public function id($id=false) { - if($id === false) { + public function id($id=false) + { + if ($id === false) { return $this->id; } else { $this->id = $id; - if($id) { + if ($id) { $this->path = self::PATH.'/'.$id; } else { $this->path = self::PATH; } } } - - } diff --git a/src/Evotor/Operations/DeviceOperation.php b/src/Evotor/Operations/DeviceOperation.php index 02e897e..4b4faac 100644 --- a/src/Evotor/Operations/DeviceOperation.php +++ b/src/Evotor/Operations/DeviceOperation.php @@ -6,22 +6,24 @@ use Kily\API\Evotor\Exception; use Kily\API\Evotor\Operations\OperationFactory; -class DeviceOperation extends Operation { - - const PATH = 'stores/{store_id}/devices/{device_id}'; +class DeviceOperation extends Operation +{ + public const PATH = 'stores/{store_id}/devices/{device_id}'; protected $path = self::PATH; protected $allowed_methods = ['get']; protected $id = null; - public function run(Operation $prev = null) { + public function run(Operation $prev = null) + { return $this; } - protected function init($args) { - if($this->prev_operation && ($this->prev_operation instanceof StoresOperation)) { + protected function init($args) + { + if ($this->prev_operation && ($this->prev_operation instanceof StoresOperation)) { $id = $args[0] ?? null; - if($id) { + if ($id) { $this->id($id); } else { throw new Exception('You must supply device id'); @@ -31,20 +33,22 @@ protected function init($args) { } } - public function id($id=false) { - if($id === false) { + public function id($id=false) + { + if ($id === false) { return $this->id; } else { $this->id = $id; - if($id) { - $this->path = str_replace(['{store_id}','{device_id}'],[$this->prev_operation->id(),$id],self::PATH); + if ($id) { + $this->path = str_replace(['{store_id}','{device_id}'], [$this->prev_operation->id(),$id], self::PATH); } } } - public function documents() { - if($this->id) { - $op = OperationFactory::fromName($this->client,'documents',[],$this); + public function documents() + { + if ($this->id) { + $op = OperationFactory::fromName($this->client, 'documents', [], $this); return $op->run(); } else { throw new Exception('You should define device id for using documents'); diff --git a/src/Evotor/Operations/DevicesOperation.php b/src/Evotor/Operations/DevicesOperation.php index 7f54a16..ba3f1bc 100644 --- a/src/Evotor/Operations/DevicesOperation.php +++ b/src/Evotor/Operations/DevicesOperation.php @@ -4,21 +4,22 @@ use Kily\API\Evotor\Client; -class DevicesOperation extends Operation { - - const PATH = 'devices'; +class DevicesOperation extends Operation +{ + public const PATH = 'devices'; protected $path = self::PATH; protected $allowed_methods = ['get']; protected $id; - public function run(Operation $prev = null) { + public function run(Operation $prev = null) + { return $this; } - public function id($id=false) { + public function id($id=false) + { throw new Exception('Employees operation does not support fetch by id'); } - } diff --git a/src/Evotor/Operations/DocumentsOperation.php b/src/Evotor/Operations/DocumentsOperation.php index 759b472..4bfe991 100644 --- a/src/Evotor/Operations/DocumentsOperation.php +++ b/src/Evotor/Operations/DocumentsOperation.php @@ -5,23 +5,25 @@ use Kily\API\Evotor\Client; use Kily\API\Evotor\Exception; -class DocumentsOperation extends Operation { - - const PATH = 'stores/{store_id}/documents'; +class DocumentsOperation extends Operation +{ + public const PATH = 'stores/{store_id}/documents'; protected $path = self::PATH; protected $allowed_methods = ['get','post']; protected $id = null; - public function run(Operation $prev = null) { + public function run(Operation $prev = null) + { return $this; } - protected function init($args) { - if($this->prev_operation) { - if($this->prev_operation instanceof StoresOperation) { - $this->path = str_replace('{store_id}',$this->prev_operation->id(),self::PATH); - } elseif($this->prev_operation instanceof DeviceOperation) { + protected function init($args) + { + if ($this->prev_operation) { + if ($this->prev_operation instanceof StoresOperation) { + $this->path = str_replace('{store_id}', $this->prev_operation->id(), self::PATH); + } elseif ($this->prev_operation instanceof DeviceOperation) { $this->path = $this->prev_operation->getPath().'/documents'; } } else { @@ -29,20 +31,20 @@ protected function init($args) { } } - public function id($id=false) { - if($id === false) { + public function id($id=false) + { + if ($id === false) { return $this->id; } else { - if($this->prev_operation instanceof DeviceOperation) { + if ($this->prev_operation instanceof DeviceOperation) { throw new Exception('You should not use id when fetching device documents'); } $this->id = $id; - if($id) { - $this->path = str_replace('{store_id}',$this->prev_operation->id(),self::PATH).'/'.$id; + if ($id) { + $this->path = str_replace('{store_id}', $this->prev_operation->id(), self::PATH).'/'.$id; } else { - $this->path = str_replace('{store_id}',$this->prev_operation->id(),self::PATH); + $this->path = str_replace('{store_id}', $this->prev_operation->id(), self::PATH); } } } - } diff --git a/src/Evotor/Operations/EmployeesOperation.php b/src/Evotor/Operations/EmployeesOperation.php index d01636b..dfc8fd7 100644 --- a/src/Evotor/Operations/EmployeesOperation.php +++ b/src/Evotor/Operations/EmployeesOperation.php @@ -4,21 +4,22 @@ use Kily\API\Evotor\Client; -class EmployeesOperation extends Operation { - - const PATH = 'employees'; +class EmployeesOperation extends Operation +{ + public const PATH = 'employees'; protected $path = self::PATH; protected $allowed_methods = ['get']; protected $id; - public function run(Operation $prev = null) { + public function run(Operation $prev = null) + { return $this; } - public function id($id=false) { + public function id($id=false) + { throw new Exception('Employees operation does not support fetch by id'); } - } diff --git a/src/Evotor/Operations/GroupsOperation.php b/src/Evotor/Operations/GroupsOperation.php index 8f5e4ed..1d1cf7d 100644 --- a/src/Evotor/Operations/GroupsOperation.php +++ b/src/Evotor/Operations/GroupsOperation.php @@ -5,20 +5,22 @@ use Kily\API\Evotor\Client; use Kily\API\Evotor\Exception; -class GroupsOperation extends Operation { - - const PATH = 'stores/{store_id}/product-groups'; +class GroupsOperation extends Operation +{ + public const PATH = 'stores/{store_id}/product-groups'; protected $path = self::PATH; protected $allowed_methods = ['get','put','post','delete']; protected $id = null; - public function run(Operation $prev = null) { + public function run(Operation $prev = null) + { return $this; } - protected function init($args) { - if($this->prev_operation instanceof StoresOperation) { + protected function init($args) + { + if ($this->prev_operation instanceof StoresOperation) { $id = $args[0] ?? null; $this->id($id); } else { @@ -26,23 +28,26 @@ protected function init($args) { } } - public function id($id=false) { - if($id === false) { + public function id($id=false) + { + if ($id === false) { return $this->id; } else { - if(is_array($id)) { - if(isset($id['id'])) $id['id'] = implode(',',$id['id']); + if (is_array($id)) { + if (isset($id['id'])) { + $id['id'] = implode(',', $id['id']); + } } else { $this->id = $id; } - if($id) { - if(is_array($id)) { - $this->path = str_replace('{store_id}',$this->prev_operation->id(),self::PATH).'?'.http_build_query($id); + if ($id) { + if (is_array($id)) { + $this->path = str_replace('{store_id}', $this->prev_operation->id(), self::PATH).'?'.http_build_query($id); } else { - $this->path = str_replace('{store_id}',$this->prev_operation->id(),self::PATH).'/'.$id; + $this->path = str_replace('{store_id}', $this->prev_operation->id(), self::PATH).'/'.$id; } } else { - $this->path = str_replace('{store_id}',$this->prev_operation->id(),self::PATH); + $this->path = str_replace('{store_id}', $this->prev_operation->id(), self::PATH); } } } diff --git a/src/Evotor/Operations/Operation.php b/src/Evotor/Operations/Operation.php index 9ae1ce7..72e501d 100644 --- a/src/Evotor/Operations/Operation.php +++ b/src/Evotor/Operations/Operation.php @@ -6,8 +6,8 @@ use Kily\API\Evotor\Exception; use Kily\API\Evotor\Request; -class Operation implements OperationInterface { - +class Operation implements OperationInterface +{ protected $client; protected $name; @@ -21,7 +21,8 @@ class Operation implements OperationInterface { protected $prev_operation = null; protected $is_bulk = false; - public function __construct(Client $client, string $name, array $arguments, Operation $op = null) { + public function __construct(Client $client, string $name, array $arguments, Operation $op = null) + { $this->client = $client; $this->name = $name; $this->prev_operation = $op; @@ -29,96 +30,110 @@ public function __construct(Client $client, string $name, array $arguments, Oper $this->init($arguments); } - protected function init($arguments) { + protected function init($arguments) + { } - public function run(Operation $prev = null) { + public function run(Operation $prev = null) + { throw new Exception('This should be overriden'); } - public function get($id=null,$options = []) { - if(!in_array('get',$this->allowed_methods)) { + public function get($id=null, $options = []) + { + if (!in_array('get', $this->allowed_methods)) { throw new Exception('Operation '.__CLASS__.' does not support get() request method'); } - if($id) { + if ($id) { $this->id($id); } - $req = $this->generateRequest('get',[$id,$options]); + $req = $this->generateRequest('get', [$id,$options]); return $req->request(); } - public function create($data,$options = []) { - if(!in_array('post',$this->allowed_methods)) { + public function create($data, $options = []) + { + if (!in_array('post', $this->allowed_methods)) { throw new Exception('Operation '.__CLASS__.' does not support post() request method'); } - if($this->id()) { + if ($this->id()) { throw new Exception('Operation '.__CLASS__.' does not require id'); } - if($this->is_bulk) { - if(!isset($data[0])) $data = [$data]; + if ($this->is_bulk) { + if (!isset($data[0])) { + $data = [$data]; + } } $this->data = $data; - $req = $this->generateRequest('post',[$data,$options]); + $req = $this->generateRequest('post', [$data,$options]); return $req->request(); } - public function update($id,$data = null,$options = []) { - if(!in_array('put',$this->allowed_methods)) { + public function update($id, $data = null, $options = []) + { + if (!in_array('put', $this->allowed_methods)) { throw new Exception('Operation '.__CLASS__.' does not support put() request method'); } - if($this->is_bulk) { + if ($this->is_bulk) { $options = $data; $data = $id; $id = null; - if(!isset($data[0])) $data = [$data]; + if (!isset($data[0])) { + $data = [$data]; + } } else { - if(is_array($id)) { - if(!$this->id()) { + if (is_array($id)) { + if (!$this->id()) { throw new Exception('For operation '.__CLASS__.' you mustt supply id on update'); } $options = $data; $data = $id; $id = $this->id(); } - if($id) { + if ($id) { $this->id($id); } } $this->data = $data; - $req = $this->generateRequest('put',[$data,$options]); + $req = $this->generateRequest('put', [$data,$options]); return $req->request(); } - public function delete($id=null,$options = []) { - if(!in_array('delete',$this->allowed_methods)) { + public function delete($id=null, $options = []) + { + if (!in_array('delete', $this->allowed_methods)) { throw new Exception('Operation '.__CLASS__.' does not support delete() request method'); } - if($this->is_bulk) { - if(!isset($this->request_options['query'])) { + if ($this->is_bulk) { + if (!isset($this->request_options['query'])) { $this->request_options['query'] = []; } - if(!is_array($id)) $id = [$id]; - $this->request_options['query']['id'] = implode(',',$id);; - $req = $this->generateRequest('delete',[null,$options]); + if (!is_array($id)) { + $id = [$id]; + } + $this->request_options['query']['id'] = implode(',', $id); + ; + $req = $this->generateRequest('delete', [null,$options]); } else { - if(is_array($id)) { - if(!$this->id()) { + if (is_array($id)) { + if (!$this->id()) { throw new Exception('For operation '.__CLASS__.' you mustt supply id on delete'); } $options = $id; $id = $this->id(); } - if($id) { + if ($id) { $this->id($id); } - $req = $this->generateRequest('delete',[$id,$options]); + $req = $this->generateRequest('delete', [$id,$options]); } return $req->request(); } - public function fetchUpdate($id,$data = null, $options = []) { - if(is_array($id)) { - if(!$this->id()) { + public function fetchUpdate($id, $data = null, $options = []) + { + if (is_array($id)) { + if (!$this->id()) { throw new Exception('For operation '.__CLASS__.' you mustt supply id on update'); } $options = $data; @@ -126,19 +141,20 @@ public function fetchUpdate($id,$data = null, $options = []) { $id = $this->id(); } $g_data = $this->get($id); - if($g_data && ($g_data = $g_data->toArray()) && isset($g_data['id'])) { + if ($g_data && ($g_data = $g_data->toArray()) && isset($g_data['id'])) { unset($g_data['id']); unset($g_data['created_at']); unset($g_data['updated_at']); - $data = array_merge($g_data,$data); - return $this->update($id,$data,$options); + $data = array_merge($g_data, $data); + return $this->update($id, $data, $options); } else { return $g_data; } } - public function bulk() { - if(!isset($this->request_options['headers'])) { + public function bulk() + { + if (!isset($this->request_options['headers'])) { $this->request_options['headers'] = []; } $this->request_options['headers']['Content-Type'] = 'application/vnd.evotor.v2+bulk+json'; @@ -146,14 +162,16 @@ public function bulk() { return $this; } - protected function generateRequest($name,$arguments) { - $req = Request::fromName($this->client,$name,$arguments,$this->generateRequestData($name,$arguments)); + protected function generateRequest($name, $arguments) + { + $req = Request::fromName($this->client, $name, $arguments, $this->generateRequestData($name, $arguments)); return $req; } - protected function generateRequestData($name,$arguments) { + protected function generateRequestData($name, $arguments) + { $data = [ - 'uri'=>$this->path_parts ? implode('/',$this->path_parts) : $this->path, + 'uri'=>$this->path_parts ? implode('/', $this->path_parts) : $this->path, 'data'=>$this->data, 'type'=>$this->type, 'options'=>$this->request_options, @@ -161,18 +179,17 @@ protected function generateRequestData($name,$arguments) { return $data; } - public function limit($cnt) { - if(!isset($this->request_options['query'])) { + public function limit($cnt) + { + if (!isset($this->request_options['query'])) { $this->request_options['query'] = []; } $this->request_options['query']['limit'] = $cnt; return $this; } - public function getPath() { + public function getPath() + { return $this->path; } - } - - diff --git a/src/Evotor/Operations/OperationFactory.php b/src/Evotor/Operations/OperationFactory.php index e577a4b..80f37c9 100644 --- a/src/Evotor/Operations/OperationFactory.php +++ b/src/Evotor/Operations/OperationFactory.php @@ -6,12 +6,13 @@ use Kily\API\Evotor\Exception; use Kily\API\Evotor\Operations\Operation; -class OperationFactory +class OperationFactory { - public static function fromName(Client $client, string $name, array $arguments, Operation $op=null) { + public static function fromName(Client $client, string $name, array $arguments, Operation $op=null) + { $cls = 'Kily\\API\\Evotor\\Operations\\'.ucfirst($name).'Operation'; - if($name && class_exists($cls)) { - return new $cls($client,$name,$arguments,$op); + if ($name && class_exists($cls)) { + return new $cls($client, $name, $arguments, $op); } throw new Exception('Unable to find method '.$name.' of class '.ucfirst($name).'Operation'); } diff --git a/src/Evotor/Operations/OperationInterface.php b/src/Evotor/Operations/OperationInterface.php index b745b3e..fdfe8b5 100644 --- a/src/Evotor/Operations/OperationInterface.php +++ b/src/Evotor/Operations/OperationInterface.php @@ -4,9 +4,8 @@ use Kily\API\Evotor\Client; -interface OperationInterface { - +interface OperationInterface +{ public function __construct(Client $client, string $name, array $arguments); public function run(); - } diff --git a/src/Evotor/Operations/ProductsOperation.php b/src/Evotor/Operations/ProductsOperation.php index 93e8e40..ac3690d 100644 --- a/src/Evotor/Operations/ProductsOperation.php +++ b/src/Evotor/Operations/ProductsOperation.php @@ -5,20 +5,22 @@ use Kily\API\Evotor\Client; use Kily\API\Evotor\Exception; -class ProductsOperation extends Operation { - - const PATH = 'stores/{store_id}/products'; +class ProductsOperation extends Operation +{ + public const PATH = 'stores/{store_id}/products'; protected $path = self::PATH; protected $allowed_methods = ['get','post','put','delete']; protected $id = null; - public function run(Operation $prev = null) { + public function run(Operation $prev = null) + { return $this; } - protected function init($args) { - if($this->prev_operation instanceof StoresOperation) { + protected function init($args) + { + if ($this->prev_operation instanceof StoresOperation) { $id = $args[0] ?? null; $this->id($id); } else { @@ -26,23 +28,26 @@ protected function init($args) { } } - public function id($id=false) { - if($id === false) { + public function id($id=false) + { + if ($id === false) { return $this->id; } else { - if(is_array($id)) { - if(isset($id['id'])) $id['id'] = implode(',',$id['id']); + if (is_array($id)) { + if (isset($id['id'])) { + $id['id'] = implode(',', $id['id']); + } } else { $this->id = $id; } - if($id) { - if(is_array($id)) { - $this->path = str_replace('{store_id}',$this->prev_operation->id(),self::PATH).'?'.http_build_query($id); + if ($id) { + if (is_array($id)) { + $this->path = str_replace('{store_id}', $this->prev_operation->id(), self::PATH).'?'.http_build_query($id); } else { - $this->path = str_replace('{store_id}',$this->prev_operation->id(),self::PATH).'/'.$id; + $this->path = str_replace('{store_id}', $this->prev_operation->id(), self::PATH).'/'.$id; } } else { - $this->path = str_replace('{store_id}',$this->prev_operation->id(),self::PATH); + $this->path = str_replace('{store_id}', $this->prev_operation->id(), self::PATH); } } } diff --git a/src/Evotor/Operations/PushOperation.php b/src/Evotor/Operations/PushOperation.php index 9b0876d..8e61d8a 100644 --- a/src/Evotor/Operations/PushOperation.php +++ b/src/Evotor/Operations/PushOperation.php @@ -5,37 +5,41 @@ use Kily\API\Evotor\Client; use Kily\API\Evotor\Exception; -class PushOperation extends Operation { - +class PushOperation extends Operation +{ protected $path = 'api/apps/{app_id}/push-notifications'; protected $allowed_methods = ['post']; protected $payload = []; - public function run() { + public function run() + { return $this; } - protected function init($args) { + protected function init($args) + { $payload = $args[0] ?? null; - if($payload) { + if ($payload) { $this->payload($payload); } - $this->path = str_replace('{app_id}',$this->client->getAppKey(),$this->path); + $this->path = str_replace('{app_id}', $this->client->getAppKey(), $this->path); } - public function payload($payload=false) { - if($payload === false) { + public function payload($payload=false) + { + if ($payload === false) { return $this->payload; } else { $this->payload = $payload; } } - protected function generateRequestData($name,$arguments) { - $data = parent::generateRequestData($name,$arguments); + protected function generateRequestData($name, $arguments) + { + $data = parent::generateRequestData($name, $arguments); $data['data']['payload'] = $this->payload; $devices = $arguments[0] ?? []; - if(!$devices) { + if (!$devices) { throw new Exception('You must supply devices to send push notification to'); } $data['data']['devices'] = $devices; diff --git a/src/Evotor/Operations/StoresOperation.php b/src/Evotor/Operations/StoresOperation.php index 4ceb6f2..c243991 100644 --- a/src/Evotor/Operations/StoresOperation.php +++ b/src/Evotor/Operations/StoresOperation.php @@ -5,31 +5,34 @@ use Kily\API\Evotor\Client; use Kily\API\Evotor\Exception; -class StoresOperation extends Operation { - - const PATH = 'stores'; +class StoresOperation extends Operation +{ + public const PATH = 'stores'; protected $path = self::PATH; protected $allowed_methods = ['get']; protected $id = null; - public function run(Operation $prev = null) { + public function run(Operation $prev = null) + { return $this; } - protected function init($args) { + protected function init($args) + { $id = $args[0] ?? null; - if($id) { + if ($id) { $this->id($id); } } - public function id($id=false) { - if($id === false) { + public function id($id=false) + { + if ($id === false) { return $this->id; } else { $this->id = $id; - if($id) { + if ($id) { $this->path = self::PATH.'/'.$id; } else { $this->path = self::PATH; @@ -37,40 +40,43 @@ public function id($id=false) { } } - public function device($id) { - if($this->id) { - $op = OperationFactory::fromName($this->client,'device',[$id],$this); + public function device($id) + { + if ($this->id) { + $op = OperationFactory::fromName($this->client, 'device', [$id], $this); return $op->run(); } else { throw new Exception('You should define store id for using groups'); } } - public function products($id=null) { - if($this->id) { - $op = OperationFactory::fromName($this->client,'products',[$id],$this); + public function products($id=null) + { + if ($this->id) { + $op = OperationFactory::fromName($this->client, 'products', [$id], $this); return $op->run(); } else { throw new Exception('You should define store id for using products'); } } - public function documents($id=null) { - if($this->id) { - $op = OperationFactory::fromName($this->client,'documents',[$id],$this); + public function documents($id=null) + { + if ($this->id) { + $op = OperationFactory::fromName($this->client, 'documents', [$id], $this); return $op->run(); } else { throw new Exception('You should define store id for using documents'); } } - public function groups($id=null) { - if($this->id) { - $op = OperationFactory::fromName($this->client,'groups',[$id],$this); + public function groups($id=null) + { + if ($this->id) { + $op = OperationFactory::fromName($this->client, 'groups', [$id], $this); return $op->run(); } else { throw new Exception('You should define store id for using groups'); } } - } diff --git a/src/Evotor/Request.php b/src/Evotor/Request.php index e335d8d..5e58ae1 100644 --- a/src/Evotor/Request.php +++ b/src/Evotor/Request.php @@ -19,40 +19,41 @@ class Request public $filter; public $options = []; - public static function fromName(Client $client, string $name, array $arguments = [], array $request_data = []) { + public static function fromName(Client $client, string $name, array $arguments = [], array $request_data = []) + { $cls = static::class; - $req = new $cls($client,$name,$arguments); - foreach($request_data as $k=>$v) { + $req = new $cls($client, $name, $arguments); + foreach ($request_data as $k=>$v) { $req->$k = $v; } return $req; } - public function __construct(Client $client, string $name, array $arguments = []) { + public function __construct(Client $client, string $name, array $arguments = []) + { $this->client = $client; $this->name = $this->rname = mb_strtolower($name); - if($this->name == 'bulk') { + if ($this->name == 'bulk') { $this->name = 'post'; - } else if($name == 'get') { + } elseif ($name == 'get') { $this->filter = $arguments[0] ?? null; } - } - public function request() { + public function request() + { $data = []; - if($this->data !== null) { - if($this->type == 'form_data') { + if ($this->data !== null) { + if ($this->type == 'form_data') { $data['form_data'] = $this->data; } else { $data['body'] = json_encode($this->data); } } - if($this->options) { - $data = array_replace_recursive($this->options,$data); + if ($this->options) { + $data = array_replace_recursive($this->options, $data); } - return $this->client->_request($this->name,$this->uri,$data,$this->filter); + return $this->client->_request($this->name, $this->uri, $data, $this->filter); } - } diff --git a/src/Evotor/Response.php b/src/Evotor/Response.php index 636897e..76247c4 100644 --- a/src/Evotor/Response.php +++ b/src/Evotor/Response.php @@ -13,23 +13,27 @@ class Response private $arr; - public function __construct(Client $clnt, ResponseInterface $resp, $filter = null) { + public function __construct(Client $clnt, ResponseInterface $resp, $filter = null) + { $this->client = $clnt; $this->response = $resp; $this->filter = $filter; } - public function __toString() { + public function __toString() + { return $this->response->getBody()->__toString(); } - public function getResponse() { + public function getResponse() + { return $this->response; } - public function toArray() { - if(!$this->arr) { - $data = json_decode($this->response->getBody(),true); + public function toArray() + { + if (!$this->arr) { + $data = json_decode($this->response->getBody(), true); $this->arr = $data['items'] ?? $data; } /* @@ -48,12 +52,13 @@ public function toArray() { return $this->arr; } - public function first() { + public function first() + { return $this->toArray()[0] ?? null; } - public function __call($name,$arguments) { - return call_user_func_array([Collection::from($this->toArray()?:[]),$name],$arguments); + public function __call($name, $arguments) + { + return call_user_func_array([Collection::from($this->toArray() ?: []),$name], $arguments); } - } diff --git a/tests/Evotor/ClientTest.php b/tests/Evotor/ClientTest.php index 350dc6e..b0d0b74 100644 --- a/tests/Evotor/ClientTest.php +++ b/tests/Evotor/ClientTest.php @@ -1,4 +1,6 @@ -markTestSkipped( 'You should define API_KEY in phpunit.xml to pass this test' ); } - if(!isset($_SERVER['APP_KEY'])) { + if (!isset($_SERVER['APP_KEY'])) { $this->markTestSkipped( 'You should define APP_KEY in phpunit.xml to pass this test' ); @@ -43,19 +45,20 @@ protected function setUp():void ); } - public function testGet() { + public function testGet() + { $this->assertTrue(is_array($stores = $this->client->stores()->get()->toArray())); $this->assertTrue(is_array($devices = $this->client->devices()->get()->toArray())); $this->assertTrue(is_array($this->client->employees()->get()->toArray())); $this->assertTrue(is_array($this->client->bulks()->get()->toArray())); - if(isset($stores[0])) { + if (isset($stores[0])) { $store_id = $stores[0]['id']; $this->assertTrue(is_array($this->client->stores($store_id)->documents()->limit(1)->get()->toArray())); $this->assertTrue(is_array($this->client->stores($store_id)->products()->limit(1)->get()->toArray())); $this->assertTrue(is_array($this->client->stores($store_id)->groups()->limit(1)->get()->toArray())); - if(isset($devices[0])) { + if (isset($devices[0])) { $device_id = $devices[0]['id']; $this->assertTrue(is_array($this->client->stores($store_id)->device($device_id)->documents()->limit(1)->get()->toArray())); } else { @@ -70,8 +73,9 @@ public function testGet() { } } - public function testCreateUpdateDelete() { - if(!isset($_SERVER['ALLOW_UNSAFE_OPERATIONS']) || !$_SERVER['ALLOW_UNSAFE_OPERATIONS']) { + public function testCreateUpdateDelete() + { + if (!isset($_SERVER['ALLOW_UNSAFE_OPERATIONS']) || !$_SERVER['ALLOW_UNSAFE_OPERATIONS']) { $this->markTestSkipped( 'You should set ALLOW_UNSAFE_OPERATIONS in phpunit.xml to true to pass the test' ); @@ -80,7 +84,7 @@ public function testCreateUpdateDelete() { $stores = $this->client->stores()->get()->toArray(); $devices = $this->client->devices()->get()->toArray(); - if(isset($stores[0])) { + if (isset($stores[0])) { $store_id = $stores[0]['id']; // PRODUCTS @@ -96,10 +100,10 @@ public function testCreateUpdateDelete() { 'allow_to_sell' => true, 'description' => 'JACKET', 'article_number' => '3773-9001-046', - 'barcodes' => - array ( + 'barcodes' => + [ 0 => '3773-9001-046', - ), + ], ]); $this->assertTrue(is_array($data->toArray())); $this->assertTrue(isset($data->toArray()['id'])); @@ -107,7 +111,7 @@ public function testCreateUpdateDelete() { $product_id = $data->toArray()['id']; - $data = $this->client->stores($store_id)->products()->update($product_id,[ + $data = $this->client->stores($store_id)->products()->update($product_id, [ 'type' => 'NORMAL', 'name' => 'JACKET2', 'code' => '100500', @@ -119,17 +123,17 @@ public function testCreateUpdateDelete() { 'allow_to_sell' => true, 'description' => 'JACKET', 'article_number' => '3773-9001-046', - 'barcodes' => - array ( + 'barcodes' => + [ 0 => '3773-9001-046', - ), + ], ]); $this->assertTrue(is_array($data->toArray())); $this->assertTrue(isset($data->toArray()['id'])); $this->assertTrue($data->toArray()['name'] === 'JACKET2'); $this->assertTrue($data->toArray()['quantity'] === 2); - $data = $this->client->stores($store_id)->products()->fetchUpdate($product_id,[ + $data = $this->client->stores($store_id)->products()->fetchUpdate($product_id, [ 'quantity'=>3 ]); $this->assertTrue(is_array($data->toArray())); @@ -152,7 +156,7 @@ public function testCreateUpdateDelete() { $group_id = $data->toArray()['id']; - $data = $this->client->stores($store_id)->groups()->update($group_id,[ + $data = $this->client->stores($store_id)->groups()->update($group_id, [ 'name' => 'TESTGROUP2', ]); $this->assertTrue(is_array($data->toArray())); @@ -162,7 +166,6 @@ public function testCreateUpdateDelete() { $data = $this->client->stores($store_id)->groups()->delete($group_id); $this->assertTrue(strlen($data->__toString()) === 0); $this->assertTrue(!count($this->client->stores($store_id)->groups($group_id)->get()->toArray())); - } else { $this->markTestSkipped( 'There is no store, so we cant check other operations' @@ -170,8 +173,9 @@ public function testCreateUpdateDelete() { } } - public function testBULKCreateUpdateDelete() { - if(!isset($_SERVER['ALLOW_UNSAFE_OPERATIONS']) || !$_SERVER['ALLOW_UNSAFE_OPERATIONS']) { + public function testBULKCreateUpdateDelete() + { + if (!isset($_SERVER['ALLOW_UNSAFE_OPERATIONS']) || !$_SERVER['ALLOW_UNSAFE_OPERATIONS']) { $this->markTestSkipped( 'You should set ALLOW_UNSAFE_OPERATIONS in phpunit.xml to true to pass the test' ); @@ -180,13 +184,12 @@ public function testBULKCreateUpdateDelete() { $stores = $this->client->stores()->get()->toArray(); $devices = $this->client->devices()->get()->toArray(); - if(isset($stores[0])) { + if (isset($stores[0])) { $store_id = $stores[0]['id']; $this->markTestIncomplete( 'We need complex logic here, because of async nature of bulk request... so trust me bulk requests works fine! No, really! Bulk requests SHOULD work fine... :-)' ); - } else { $this->markTestSkipped( 'There is no store, so we cant check other operations' @@ -194,27 +197,30 @@ public function testBULKCreateUpdateDelete() { } } - public function test_request() { - $resp = $this->client->_request('get','_nonexistent_',[]); + public function test_request() + { + $resp = $this->client->_request('get', '_nonexistent_', []); $this->assertTrue($this->client->getHttpErrorMessage() === 'Not Found'); $this->assertTrue($this->client->getHttpErrorCode() === 404); } - public function testGetClient() { + public function testGetClient() + { $this->assertTrue($this->client->getClient() instanceof \GuzzleHttp\Client); } - public function test__call() { + public function test__call() + { $this->assertTrue($this->client->stores() instanceof Operation); $this->expectException(Exception::class); $this->client->nonexistent(); } - public function testIsOk() { + public function testIsOk() + { $resp = $this->client->stores()->get(); $this->assertTrue($this->client->isOk()); $resp = $this->client->stores('_NONEXISTENT_')->get(); $this->assertTrue(!$this->client->isOk()); } - } diff --git a/tests/Evotor/ExceptionTest.php b/tests/Evotor/ExceptionTest.php index 87e2138..f474e62 100644 --- a/tests/Evotor/ExceptionTest.php +++ b/tests/Evotor/ExceptionTest.php @@ -1,4 +1,6 @@ -expectException(Exception::class); - OperationFactory::fromName($this->clnt,'_nonexistent_',[]); + OperationFactory::fromName($this->clnt, '_nonexistent_', []); } public function testFromNameGood() { - $this->assertTrue(OperationFactory::fromName($this->clnt,'stores',[]) instanceof Operation); + $this->assertTrue(OperationFactory::fromName($this->clnt, 'stores', []) instanceof Operation); } } diff --git a/tests/Evotor/Operations/OperationTest.php b/tests/Evotor/Operations/OperationTest.php index 7331bad..fd98c11 100644 --- a/tests/Evotor/Operations/OperationTest.php +++ b/tests/Evotor/Operations/OperationTest.php @@ -1,4 +1,6 @@ -assertTrue($this->storesOperation->id() === "123"); } - public function testProductsBad() { + public function testProductsBad() + { $this->expectException(Exception::class); $this->storesOperation->products(); } - public function testProductsGood() { + public function testProductsGood() + { $this->storesOperation->id("123"); $this->assertTrue($this->storesOperation->products() instanceof ProductsOperation); } - public function testDocumentsBad() { + public function testDocumentsBad() + { $this->expectException(Exception::class); $this->storesOperation->documents(); } - public function testDocumentsGood() { + public function testDocumentsGood() + { $this->storesOperation->id("123"); $this->assertTrue($this->storesOperation->documents() instanceof DocumentsOperation); } - public function testGroupsBad() { + public function testGroupsBad() + { $this->expectException(Exception::class); $this->storesOperation->groups(); } - public function testGroupsGood() { + public function testGroupsGood() + { $this->storesOperation->id("123"); $this->assertTrue($this->storesOperation->groups() instanceof GroupsOperation); } diff --git a/tests/Evotor/RequestTest.php b/tests/Evotor/RequestTest.php index 375709d..8c7506f 100644 --- a/tests/Evotor/RequestTest.php +++ b/tests/Evotor/RequestTest.php @@ -1,4 +1,6 @@ -client, 'gEt', [], []); $this->assertTrue($req instanceof Request); $this->assertTrue($req->name === 'get'); @@ -50,7 +53,8 @@ public function testFromName() { $this->assertTrue($req->rname === 'bulk'); } - public function testRequest() { + public function testRequest() + { $this->markTestSkipped("To be done"); } } diff --git a/tests/Evotor/ResponseTest.php b/tests/Evotor/ResponseTest.php index 6dc89f2..2baf266 100644 --- a/tests/Evotor/ResponseTest.php +++ b/tests/Evotor/ResponseTest.php @@ -1,4 +1,6 @@ -assertTrue(is_string($this->response->__toString())); } - public function testToArray() { - $this->assertTrue(is_array(json_decode($this->response->__toString(),true))); - $this->assertTrue($this->response->toArray() === json_decode($this->response->__toString(),true)['items']); + public function testToArray() + { + $this->assertTrue(is_array(json_decode($this->response->__toString(), true))); + $this->assertTrue($this->response->toArray() === json_decode($this->response->__toString(), true)['items']); } - public function testFirst() { + public function testFirst() + { $this->assertTrue(($this->response->first()) == ['id'=>'123']); } - }