From 741ee9bb1ac31ced8e6e507c91d5574b767ecb83 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Mon, 15 Jun 2015 16:40:39 +0300 Subject: [PATCH 01/12] stores get & stores edit --- lib/RetailCrm/ApiClient.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index 593c63f..bbc3d2a 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -520,6 +520,16 @@ class ApiClient { return $this->client->makeRequest('/reference/sites', Client::METHOD_GET); } + + /** + * Returns stores list + * + * @return ApiResponse + */ + public function storesList() + { + return $this->client->makeRequest('/reference/stores', Client::METHOD_GET); + } /** * Edit deliveryService @@ -710,6 +720,31 @@ class ApiClient ); } + /** + * Edit store + * + * @param array $data site data + * @return ApiResponse + */ + public function storesEdit(array $data) + { + if (!isset($data['code'])) { + throw new \InvalidArgumentException('Data must contain "code" parameter.'); + } + + if (!isset($data['name'])) { + throw new \InvalidArgumentException('Data must contain "name" parameter.'); + } + + return $this->client->makeRequest( + '/reference/stores/' . $data['code'] . '/edit', + Client::METHOD_POST, + array( + 'site' => json_encode($data) + ) + ); + } + /** * Update CRM basic statistic * From 1cb4074470d8444d2a96df1d6e04a46bcefe55cf Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Mon, 15 Jun 2015 16:47:47 +0300 Subject: [PATCH 02/12] stores get & stores edit --- lib/RetailCrm/ApiClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index bbc3d2a..f90db5a 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -740,7 +740,7 @@ class ApiClient '/reference/stores/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'site' => json_encode($data) + 'store' => json_encode($data) ) ); } From 6ee64527487c0d61370cae72483c5a8bc91bf125 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Mon, 15 Jun 2015 17:50:31 +0300 Subject: [PATCH 03/12] =?UTF-8?q?=D1=80=D0=B5=D0=BA=D1=83=D1=80=D1=81?= =?UTF-8?q?=D0=B8=D0=B2=D0=BD=D0=B0=D1=8F=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BA=D0=B0=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=B0?= =?UTF-8?q?=20=D0=B2=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B5=20timeout,=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D1=82=D0=B2=D0=BE=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BE=D0=BA=20-=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/RetailCrm/Http/Client.php | 31 ++++++++++++++++++----- tests/RetailCrm/Tests/Http/ClientTest.php | 6 ++--- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index a3374dc..c0814c4 100644 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -15,7 +15,7 @@ class Client protected $url; protected $defaultParameters; - protected $retry; + public $retry; public function __construct($url, array $defaultParameters = array()) { @@ -58,22 +58,27 @@ class Client $parameters = array_merge($this->defaultParameters, $parameters); - $path = $this->url . $path; + $url = $this->url . $path; if (self::METHOD_GET === $method && sizeof($parameters)) { - $path .= '?' . http_build_query($parameters); + $url .= '?' . http_build_query($parameters); } $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $path); - curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout); + curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify); + if (!$debug) { + curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout); + } else { + curl_setopt($ch, CURLOPT_TIMEOUT_MS, (int) $timeout + ($this->retry * 2000)); + } + if (self::METHOD_POST === $method) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); @@ -86,6 +91,20 @@ class Client curl_close($ch); + if ($errno && in_array($errno, array(6, 7, 28, 34, 35)) && $this->retry < 3) { + $errno = null; + $error = null; + $this->retry += 1; + $this->makeRequest( + $path, + $method, + $parameters, + $timeout, + $verify, + $debug + ); + } + if ($errno) { throw new CurlException($error, $errno); } diff --git a/tests/RetailCrm/Tests/Http/ClientTest.php b/tests/RetailCrm/Tests/Http/ClientTest.php index 84eecf2..61e614c 100644 --- a/tests/RetailCrm/Tests/Http/ClientTest.php +++ b/tests/RetailCrm/Tests/Http/ClientTest.php @@ -61,11 +61,11 @@ class ClientTest extends TestCase /** * @group integration - * @expectedException \RetailCrm\Exception\CurlException */ - public function testMakeRequestTimeout() + public function testMakeRequestRepeatOnTimeout() { $client = static::getClient(); - $client->makeRequest('/orders', Client::METHOD_GET, array(), 1, false, true); + $response = $client->makeRequest('/orders', Client::METHOD_GET, array(), 1, false, true); + $this->assertGreaterThanOrEqual(1, $client->retry); } } From 700b87fba00263f759e4eb4a7367bcc238a0f398 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Wed, 23 Dec 2015 10:23:10 +0300 Subject: [PATCH 04/12] get retry protected --- lib/RetailCrm/Http/Client.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index c0814c4..5a3d50d 100644 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -15,7 +15,7 @@ class Client protected $url; protected $defaultParameters; - public $retry; + protected $retry; public function __construct($url, array $defaultParameters = array()) { @@ -111,4 +111,9 @@ class Client return new ApiResponse($statusCode, $responseBody); } + + public function getRetry() + { + return $this->retry; + } } From 5d37588ab269d78ab65e92acc940d858a442f525 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Sat, 9 Jan 2016 12:02:19 +0300 Subject: [PATCH 05/12] .gitignore for IDE --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ace3cdf..620af19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -vendor/ -bin/ +/vendor +/bin composer.lock phpunit.xml .idea @@ -8,3 +8,4 @@ phpunit.xml .buildpath .project .swp +/nbproject \ No newline at end of file From 0b512fa0310b5a39b9c9a8d700c9db288cfe1de8 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Sat, 9 Jan 2016 15:23:50 +0300 Subject: [PATCH 06/12] packs & telephony methods --- lib/RetailCrm/ApiClient.php | 1011 ++++++++++------- lib/RetailCrm/Http/Client.php | 2 +- phpunit.xml.dist | 9 +- tests/RetailCrm/Tests/ApiClientOrdersTest.php | 21 - 4 files changed, 590 insertions(+), 453 deletions(-) diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index a1bba14..6bdf66d 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -1,15 +1,15 @@ client = new Client($url, array('apiKey' => $apiKey)); + $this->client = new Client($url, array( + 'apiKey' => $apiKey + )); $this->siteCode = $site; } + /** + * Returns filtered orders list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @return ApiResponse + */ + public function ordersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (sizeof($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest('/orders', Client::METHOD_GET, $parameters); + } + /** * Create a order * - * @param array $order - * @param string $site (default: null) + * @param array $order + * @param string $site (default: null) + * * @return ApiResponse */ public function ordersCreate(array $order, $site = null) { - if (!sizeof($order)) { + if (! sizeof($order)) { throw new \InvalidArgumentException('Parameter `order` must contains a data'); } @@ -58,86 +88,36 @@ class ApiClient } /** - * Edit a order + * Save order IDs' (id and externalId) association in the CRM + * + * @param array $ids * - * @param array $order - * @param string $site (default: null) * @return ApiResponse */ - public function ordersEdit(array $order, $by = 'externalId', $site = null) + public function ordersFixExternalIds(array $ids) { - if (!sizeof($order)) { - throw new \InvalidArgumentException('Parameter `order` must contains a data'); + if (! sizeof($ids)) { + throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair'); } - $this->checkIdParameter($by); - - if (!isset($order[$by])) { - throw new \InvalidArgumentException(sprintf('Order array must contain the "%s" parameter.', $by)); - } - - return $this->client->makeRequest( - "/orders/" . $order[$by] . "/edit", - Client::METHOD_POST, - $this->fillSite($site, array( - 'order' => json_encode($order), - 'by' => $by, - )) - ); - } - - /** - * Upload array of the orders - * - * @param array $orders - * @param string $site (default: null) - * @return ApiResponse - */ - public function ordersUpload(array $orders, $site = null) - { - if (!sizeof($orders)) { - throw new \InvalidArgumentException('Parameter `orders` must contains array of the orders'); - } - - return $this->client->makeRequest("/orders/upload", Client::METHOD_POST, $this->fillSite($site, array( - 'orders' => json_encode($orders), - ))); - } - - /** - * Get order by id or externalId - * - * @param string $id - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * @return ApiResponse - */ - public function ordersGet($id, $by = 'externalId', $site = null) - { - $this->checkIdParameter($by); - - return $this->client->makeRequest("/orders/$id", Client::METHOD_GET, $this->fillSite($site, array( - 'by' => $by - ))); + return $this->client->makeRequest("/orders/fix-external-ids", Client::METHOD_POST, array( + 'orders' => json_encode($ids) + )); } /** * Returns a orders history * - * @param \DateTime $startDate (default: null) - * @param \DateTime $endDate (default: null) - * @param int $limit (default: 100) - * @param int $offset (default: 0) - * @param bool $skipMyChanges (default: true) + * @param \DateTime $startDate (default: null) + * @param \DateTime $endDate (default: null) + * @param int $limit (default: 100) + * @param int $offset (default: 0) + * @param bool $skipMyChanges (default: true) + * * @return ApiResponse */ - public function ordersHistory( - \DateTime $startDate = null, - \DateTime $endDate = null, - $limit = 100, - $offset = 0, - $skipMyChanges = true - ) { + public function ordersHistory(\DateTime $startDate = null, \DateTime $endDate = null, $limit = 100, $offset = 0, $skipMyChanges = true) + { $parameters = array(); if ($startDate) { @@ -159,36 +139,12 @@ class ApiClient return $this->client->makeRequest('/orders/history', Client::METHOD_GET, $parameters); } - /** - * Returns filtered orders list - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * @return ApiResponse - */ - public function ordersList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (sizeof($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest('/orders', Client::METHOD_GET, $parameters); - } - /** * Returns statuses of the orders * - * @param array $ids (default: array()) - * @param array $externalIds (default: array()) + * @param array $ids (default: array()) + * @param array $externalIds (default: array()) + * * @return ApiResponse */ public function ordersStatuses(array $ids = array(), array $externalIds = array()) @@ -206,129 +162,64 @@ class ApiClient } /** - * Save order IDs' (id and externalId) association in the CRM + * Upload array of the orders + * + * @param array $orders + * @param string $site (default: null) * - * @param array $ids * @return ApiResponse */ - public function ordersFixExternalIds(array $ids) + public function ordersUpload(array $orders, $site = null) { - if (!sizeof($ids)) { - throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair'); + if (! sizeof($orders)) { + throw new \InvalidArgumentException('Parameter `orders` must contains array of the orders'); } - return $this->client->makeRequest("/orders/fix-external-ids", Client::METHOD_POST, array( - 'orders' => json_encode($ids), - )); - } - - /** - * Get orders assembly history - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * @return ApiResponse - */ - public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (sizeof($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest('/orders/packs/history', Client::METHOD_GET, $parameters); - } - - /** - * Create a customer - * - * @param array $customer - * @param string $site (default: null) - * @return ApiResponse - */ - public function customersCreate(array $customer, $site = null) - { - if (!sizeof($customer)) { - throw new \InvalidArgumentException('Parameter `customer` must contains a data'); - } - - return $this->client->makeRequest("/customers/create", Client::METHOD_POST, $this->fillSite($site, array( - 'customer' => json_encode($customer) + return $this->client->makeRequest("/orders/upload", Client::METHOD_POST, $this->fillSite($site, array( + 'orders' => json_encode($orders) ))); } /** - * Edit a customer + * Get order by id or externalId + * + * @param string $id + * @param string $by (default: 'externalId') + * @param string $site (default: null) * - * @param array $customer - * @param string $by (default: 'externalId') - * @param string $site (default: null) * @return ApiResponse */ - public function customersEdit(array $customer, $by = 'externalId', $site = null) + public function ordersGet($id, $by = 'externalId', $site = null) { - if (!sizeof($customer)) { - throw new \InvalidArgumentException('Parameter `customer` must contains a data'); - } - $this->checkIdParameter($by); - if (!isset($customer[$by])) { - throw new \InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by)); - } - - return $this->client->makeRequest( - "/customers/" . $customer[$by] . "/edit", - Client::METHOD_POST, - $this->fillSite( - $site, - array( - 'customer' => json_encode($customer), - 'by' => $by - ) - ) - ); - } - - /** - * Upload array of the customers - * - * @param array $customers - * @param string $site (default: null) - * @return ApiResponse - */ - public function customersUpload(array $customers, $site = null) - { - if (!sizeof($customers)) { - throw new \InvalidArgumentException('Parameter `customers` must contains array of the customers'); - } - - return $this->client->makeRequest("/customers/upload", Client::METHOD_POST, $this->fillSite($site, array( - 'customers' => json_encode($customers), + return $this->client->makeRequest("/orders/$id", Client::METHOD_GET, $this->fillSite($site, array( + 'by' => $by ))); } /** - * Get customer by id or externalId + * Edit a order + * + * @param array $order + * @param string $site (default: null) * - * @param string $id - * @param string $by (default: 'externalId') - * @param string $site (default: null) * @return ApiResponse */ - public function customersGet($id, $by = 'externalId', $site = null) + public function ordersEdit(array $order, $by = 'externalId', $site = null) { + if (! sizeof($order)) { + throw new \InvalidArgumentException('Parameter `order` must contains a data'); + } + $this->checkIdParameter($by); - return $this->client->makeRequest("/customers/$id", Client::METHOD_GET, $this->fillSite($site, array( + if (! isset($order[$by])) { + throw new \InvalidArgumentException(sprintf('Order array must contain the "%s" parameter.', $by)); + } + + return $this->client->makeRequest("/orders/" . $order[$by] . "/edit", Client::METHOD_POST, $this->fillSite($site, array( + 'order' => json_encode($order), 'by' => $by ))); } @@ -336,9 +227,10 @@ class ApiClient /** * Returns filtered customers list * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * * @return ApiResponse */ public function customersList(array $filter = array(), $page = null, $limit = null) @@ -358,30 +250,239 @@ class ApiClient return $this->client->makeRequest('/customers', Client::METHOD_GET, $parameters); } + /** + * Create a customer + * + * @param array $customer + * @param string $site (default: null) + * + * @return ApiResponse + */ + public function customersCreate(array $customer, $site = null) + { + if (! sizeof($customer)) { + throw new \InvalidArgumentException('Parameter `customer` must contains a data'); + } + + return $this->client->makeRequest("/customers/create", Client::METHOD_POST, $this->fillSite($site, array( + 'customer' => json_encode($customer) + ))); + } + /** * Save customer IDs' (id and externalId) association in the CRM * - * @param array $ids + * @param array $ids + * * @return ApiResponse */ public function customersFixExternalIds(array $ids) { - if (!sizeof($ids)) { + if (! sizeof($ids)) { throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair'); } return $this->client->makeRequest("/customers/fix-external-ids", Client::METHOD_POST, array( - 'customers' => json_encode($ids), + 'customers' => json_encode($ids) )); } + /** + * Upload array of the customers + * + * @param array $customers + * @param string $site (default: null) + * + * @return ApiResponse + */ + public function customersUpload(array $customers, $site = null) + { + if (! sizeof($customers)) { + throw new \InvalidArgumentException('Parameter `customers` must contains array of the customers'); + } + + return $this->client->makeRequest("/customers/upload", Client::METHOD_POST, $this->fillSite($site, array( + 'customers' => json_encode($customers) + ))); + } + + /** + * Get customer by id or externalId + * + * @param string $id + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @return ApiResponse + */ + public function customersGet($id, $by = 'externalId', $site = null) + { + $this->checkIdParameter($by); + + return $this->client->makeRequest("/customers/$id", Client::METHOD_GET, $this->fillSite($site, array( + 'by' => $by + ))); + } + + /** + * Edit a customer + * + * @param array $customer + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @return ApiResponse + */ + public function customersEdit(array $customer, $by = 'externalId', $site = null) + { + if (! sizeof($customer)) { + throw new \InvalidArgumentException('Parameter `customer` must contains a data'); + } + + $this->checkIdParameter($by); + + if (! isset($customer[$by])) { + throw new \InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by)); + } + + return $this->client->makeRequest("/customers/" . $customer[$by] . "/edit", Client::METHOD_POST, $this->fillSite($site, array( + 'customer' => json_encode($customer), + 'by' => $by + ))); + } + + /** + * Get orders assembly list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @return ApiResponse + */ + public function ordersPacksList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (sizeof($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest('/orders/packs', Client::METHOD_GET, $parameters); + } + + /** + * Create orders assembly + * + * @param array $pack + * @param string $site (default: null) + * + * @return ApiResponse + */ + public function ordersPacksCreate(array $pack, $site = null) + { + if (! sizeof($pack)) { + throw new \InvalidArgumentException('Parameter `pack` must contains a data'); + } + + return $this->client->makeRequest("/orders/packs/create", Client::METHOD_POST, $this->fillSite($site, array( + 'pack' => json_encode($pack) + ))); + } + + /** + * Get orders assembly history + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @return ApiResponse + */ + public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (sizeof($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest('/orders/packs/history', Client::METHOD_GET, $parameters); + } + + /** + * Get orders assembly by id + * + * @param string $id + * + * @return ApiResponse + */ + public function ordersPacksGet($id) + { + if (empty($id)) { + throw new \InvalidArgumentException('Parameter `id` must be set'); + } + + return $this->client->makeRequest("/orders/packs/$id", Client::METHOD_GET); + } + + /** + * Delete orders assembly by id + * + * @param string $id + * + * @return ApiResponse + */ + public function ordersPacksDelete($id) + { + if (empty($id)) { + throw new \InvalidArgumentException('Parameter `id` must be set'); + } + + return $this->client->makeRequest("/orders/packs/$id/delete", Client::METHOD_POST); + } + + /** + * Edit orders assembly + * + * @param array $pack + * @param string $site (default: null) + * + * @return ApiResponse + */ + public function ordersPacksEdit(array $pack, $site = null) + { + if (! sizeof($pack) || empty($pack['id'])) { + throw new \InvalidArgumentException('Parameter `pack` must contains a data & pack `id` must be set'); + } + + $id = $pack['id']; + + return $this->client->makeRequest("/orders/packs/$id/edit", Client::METHOD_POST, $this->fillSite($site, array( + 'pack' => json_encode($pack) + ))); + } + /** * Get purchace prices & stock balance * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * @param string $site (default: null) + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * @param string $site (default: null) + * * @return ApiResponse */ public function storeInventories(array $filter = array(), $page = null, $limit = null, $site = null) @@ -404,21 +505,30 @@ class ApiClient /** * Upload store inventories * - * @param array $offers - * @param string $site (default: null) + * @param array $offers + * @param string $site (default: null) + * * @return ApiResponse */ public function storeInventoriesUpload(array $offers, $site = null) { - if (!sizeof($offers)) { + if (! sizeof($offers)) { throw new \InvalidArgumentException('Parameter `offers` must contains array of the offers'); } - return $this->client->makeRequest( - "/store/inventories/upload", - Client::METHOD_POST, - $this->fillSite($site, array('offers' => json_encode($offers))) - ); + return $this->client->makeRequest("/store/inventories/upload", Client::METHOD_POST, $this->fillSite($site, array( + 'offers' => json_encode($offers) + ))); + } + + /** + * Returns available county list + * + * @return ApiResponse + */ + public function countriesList() + { + return $this->client->makeRequest('/reference/countries', Client::METHOD_GET); } /** @@ -431,6 +541,24 @@ class ApiClient return $this->client->makeRequest('/reference/delivery-services', Client::METHOD_GET); } + /** + * Edit deliveryService + * + * @param array $data delivery service data + * + * @return ApiResponse + */ + public function deliveryServicesEdit(array $data) + { + if (! isset($data['code'])) { + throw new \InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest('/reference/delivery-services/' . $data['code'] . '/edit', Client::METHOD_POST, array( + 'deliveryService' => json_encode($data) + )); + } + /** * Returns deliveryTypes list * @@ -441,6 +569,24 @@ class ApiClient return $this->client->makeRequest('/reference/delivery-types', Client::METHOD_GET); } + /** + * Edit deliveryType + * + * @param array $data delivery type data + * + * @return ApiResponse + */ + public function deliveryTypesEdit(array $data) + { + if (! isset($data['code'])) { + throw new \InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest('/reference/delivery-types/' . $data['code'] . '/edit', Client::METHOD_POST, array( + 'deliveryType' => json_encode($data) + )); + } + /** * Returns orderMethods list * @@ -451,6 +597,24 @@ class ApiClient return $this->client->makeRequest('/reference/order-methods', Client::METHOD_GET); } + /** + * Edit orderMethod + * + * @param array $data order method data + * + * @return ApiResponse + */ + public function orderMethodsEdit(array $data) + { + if (! isset($data['code'])) { + throw new \InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest('/reference/order-methods/' . $data['code'] . '/edit', Client::METHOD_POST, array( + 'orderMethod' => json_encode($data) + )); + } + /** * Returns orderTypes list * @@ -461,6 +625,24 @@ class ApiClient return $this->client->makeRequest('/reference/order-types', Client::METHOD_GET); } + /** + * Edit orderType + * + * @param array $data order type data + * + * @return ApiResponse + */ + public function orderTypesEdit(array $data) + { + if (! isset($data['code'])) { + throw new \InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest('/reference/order-types/' . $data['code'] . '/edit', Client::METHOD_POST, array( + 'orderType' => json_encode($data) + )); + } + /** * Returns paymentStatuses list * @@ -471,6 +653,24 @@ class ApiClient return $this->client->makeRequest('/reference/payment-statuses', Client::METHOD_GET); } + /** + * Edit paymentStatus + * + * @param array $data payment status data + * + * @return ApiResponse + */ + public function paymentStatusesEdit(array $data) + { + if (! isset($data['code'])) { + throw new \InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest('/reference/payment-statuses/' . $data['code'] . '/edit', Client::METHOD_POST, array( + 'paymentStatus' => json_encode($data) + )); + } + /** * Returns paymentTypes list * @@ -481,6 +681,24 @@ class ApiClient return $this->client->makeRequest('/reference/payment-types', Client::METHOD_GET); } + /** + * Edit paymentType + * + * @param array $data payment type data + * + * @return ApiResponse + */ + public function paymentTypesEdit(array $data) + { + if (! isset($data['code'])) { + throw new \InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest('/reference/payment-types/' . $data['code'] . '/edit', Client::METHOD_POST, array( + 'paymentType' => json_encode($data) + )); + } + /** * Returns productStatuses list * @@ -491,6 +709,52 @@ class ApiClient return $this->client->makeRequest('/reference/product-statuses', Client::METHOD_GET); } + /** + * Edit productStatus + * + * @param array $data product status data + * + * @return ApiResponse + */ + public function productStatusesEdit(array $data) + { + if (! isset($data['code'])) { + throw new \InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest('/reference/product-statuses/' . $data['code'] . '/edit', Client::METHOD_POST, array( + 'productStatus' => json_encode($data) + )); + } + + /** + * Returns sites list + * + * @return ApiResponse + */ + public function sitesList() + { + return $this->client->makeRequest('/reference/sites', Client::METHOD_GET); + } + + /** + * Edit site + * + * @param array $data site data + * + * @return ApiResponse + */ + public function sitesEdit(array $data) + { + if (! isset($data['code'])) { + throw new \InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest('/reference/sites/' . $data['code'] . '/edit', Client::METHOD_POST, array( + 'site' => json_encode($data) + )); + } + /** * Returns statusGroups list * @@ -512,13 +776,21 @@ class ApiClient } /** - * Returns sites list + * Edit order status + * + * @param array $data status data * * @return ApiResponse */ - public function sitesList() + public function statusesEdit(array $data) { - return $this->client->makeRequest('/reference/sites', Client::METHOD_GET); + if (! isset($data['code'])) { + throw new \InvalidArgumentException('Data must contain "code" parameter.'); + } + + return $this->client->makeRequest('/reference/statuses/' . $data['code'] . '/edit', Client::METHOD_POST, array( + 'status' => json_encode($data) + )); } /** @@ -531,218 +803,26 @@ class ApiClient return $this->client->makeRequest('/reference/stores', Client::METHOD_GET); } - /** - * Edit deliveryService - * - * @param array $data delivery service data - * @return ApiResponse - */ - public function deliveryServicesEdit(array $data) - { - if (!isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/delivery-services/' . $data['code'] . '/edit', - Client::METHOD_POST, - array( - 'deliveryService' => json_encode($data) - ) - ); - } - - /** - * Edit deliveryType - * - * @param array $data delivery type data - * @return ApiResponse - */ - public function deliveryTypesEdit(array $data) - { - if (!isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/delivery-types/' . $data['code'] . '/edit', - Client::METHOD_POST, - array( - 'deliveryType' => json_encode($data) - ) - ); - } - - /** - * Edit orderMethod - * - * @param array $data order method data - * @return ApiResponse - */ - public function orderMethodsEdit(array $data) - { - if (!isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/order-methods/' . $data['code'] . '/edit', - Client::METHOD_POST, - array( - 'orderMethod' => json_encode($data) - ) - ); - } - - /** - * Edit orderType - * - * @param array $data order type data - * @return ApiResponse - */ - public function orderTypesEdit(array $data) - { - if (!isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/order-types/' . $data['code'] . '/edit', - Client::METHOD_POST, - array( - 'orderType' => json_encode($data) - ) - ); - } - - /** - * Edit paymentStatus - * - * @param array $data payment status data - * @return ApiResponse - */ - public function paymentStatusesEdit(array $data) - { - if (!isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/payment-statuses/' . $data['code'] . '/edit', - Client::METHOD_POST, - array( - 'paymentStatus' => json_encode($data) - ) - ); - } - - /** - * Edit paymentType - * - * @param array $data payment type data - * @return ApiResponse - */ - public function paymentTypesEdit(array $data) - { - if (!isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/payment-types/' . $data['code'] . '/edit', - Client::METHOD_POST, - array( - 'paymentType' => json_encode($data) - ) - ); - } - - /** - * Edit productStatus - * - * @param array $data product status data - * @return ApiResponse - */ - public function productStatusesEdit(array $data) - { - if (!isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/product-statuses/' . $data['code'] . '/edit', - Client::METHOD_POST, - array( - 'productStatus' => json_encode($data) - ) - ); - } - - /** - * Edit order status - * - * @param array $data status data - * @return ApiResponse - */ - public function statusesEdit(array $data) - { - if (!isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/statuses/' . $data['code'] . '/edit', - Client::METHOD_POST, - array( - 'status' => json_encode($data) - ) - ); - } - - /** - * Edit site - * - * @param array $data site data - * @return ApiResponse - */ - public function sitesEdit(array $data) - { - if (!isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/sites/' . $data['code'] . '/edit', - Client::METHOD_POST, - array( - 'site' => json_encode($data) - ) - ); - } - /** * Edit store * * @param array $data site data + * * @return ApiResponse */ public function storesEdit(array $data) { - if (!isset($data['code'])) { + if (! isset($data['code'])) { throw new \InvalidArgumentException('Data must contain "code" parameter.'); } - if (!isset($data['name'])) { + if (! isset($data['name'])) { throw new \InvalidArgumentException('Data must contain "name" parameter.'); } - return $this->client->makeRequest( - '/reference/stores/' . $data['code'] . '/edit', - Client::METHOD_POST, - array( - 'store' => json_encode($data) - ) - ); + return $this->client->makeRequest('/reference/stores/' . $data['code'] . '/edit', Client::METHOD_POST, array( + 'store' => json_encode($data) + )); } /** @@ -755,6 +835,75 @@ class ApiClient return $this->client->makeRequest('/statistic/update', Client::METHOD_GET); } + /** + * Call event + * + * @param string $phone + * @param string $type + * @param string $code + * @param string $status + * + * @return ApiResponse + */ + public function telephonyCallEvent($phone, $type, $code, $status) + { + if (! isset($phone)) { + throw new \InvalidArgumentException('Phone number must be set'); + } + + $parameters['phone'] = $phone; + + if (! isset($type)) { + throw new \InvalidArgumentException('Type must be set (in|out|hangup)'); + } + + $parameters['type'] = $type; + + if (! isset($code)) { + throw new \InvalidArgumentException('Code must be set'); + } + + $parameters['code'] = $code; + $parameters['hangupStatus'] = $status; + + return $this->client->makeRequest('/telephony/call/event', Client::METHOD_POST, $parameters); + } + + /** + * Upload calls + * + * @param array $calls + * + * @return ApiResponse + */ + public function telephonyCallsUpload(array $calls) + { + if (! sizeof($calls)) { + throw new \InvalidArgumentException('Parameter `calls` must contains array of the calls'); + } + + return $this->client->makeRequest("/telephony/calls/upload", Client::METHOD_POST, array( + 'calls' => json_encode($calls) + )); + } + + /** + * Get call manager + * + * @return ApiResponse + */ + public function telephonyCallManager($phone, $details) + { + if (!isset($phone)) { + throw new \InvalidArgumentException('Phone number must be set'); + } + + $parameters['phone'] = $phone; + $parameters['details'] = isset($details) ? $details : 0; + + return $this->client->makeRequest('/telephony/manager', Client::METHOD_GET, $parameters); + } + /** * Return current site * @@ -768,7 +917,8 @@ class ApiClient /** * Set site * - * @param string $site + * @param string $site + * * @return void */ public function setSite($site) @@ -779,18 +929,18 @@ class ApiClient /** * Check ID parameter * - * @param string $by + * @param string $by + * * @return bool */ protected function checkIdParameter($by) { - $allowedForBy = array('externalId', 'id'); - if (!in_array($by, $allowedForBy)) { - throw new \InvalidArgumentException(sprintf( - 'Value "%s" for parameter "by" is not valid. Allowed values are %s.', - $by, - implode(', ', $allowedForBy) - )); + $allowedForBy = array( + 'externalId', + 'id' + ); + if (! in_array($by, $allowedForBy)) { + throw new \InvalidArgumentException(sprintf('Value "%s" for parameter "by" is not valid. Allowed values are %s.', $by, implode(', ', $allowedForBy))); } return true; @@ -799,8 +949,9 @@ class ApiClient /** * Fill params by site value * - * @param string $site - * @param array $params + * @param string $site + * @param array $params + * * @return array */ protected function fillSite($site, array $params) diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index 5a3d50d..8d011fb 100644 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -61,7 +61,7 @@ class Client $url = $this->url . $path; if (self::METHOD_GET === $method && sizeof($parameters)) { - $url .= '?' . http_build_query($parameters); + $url .= '?' . http_build_query($parameters, '', '&'); } $ch = curl_init(); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 651ae70..652f4e7 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,12 @@ - + diff --git a/tests/RetailCrm/Tests/ApiClientOrdersTest.php b/tests/RetailCrm/Tests/ApiClientOrdersTest.php index 1c1fc32..8d1254d 100644 --- a/tests/RetailCrm/Tests/ApiClientOrdersTest.php +++ b/tests/RetailCrm/Tests/ApiClientOrdersTest.php @@ -199,27 +199,6 @@ class ApiClientOrdersTest extends TestCase 'API returns generatedAt in orders history' ); } - - /** - * @group integration - */ - public function testOrdersPacksHistory() - { - $client = static::getApiClient(); - - $response = $client->ordersPacksHistory(); - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertEquals(200, $response->getStatusCode()); - $this->assertTrue($response->success); - $this->assertTrue( - isset($response['history']), - 'API returns orders assembly history' - ); - $this->assertTrue( - isset($response['generatedAt']), - 'API returns generatedAt in orders assembly history' - ); - } /** * @group integration From 9fdb2f0ada46be265b8a5c9dd0330f87dccf5da4 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Sat, 9 Jan 2016 15:24:12 +0300 Subject: [PATCH 07/12] packs tests --- phpunit.xsd | 251 +++++++++++++++++++ tests/RetailCrm/Tests/ApiClientPacksTest.php | 29 +++ 2 files changed, 280 insertions(+) create mode 100644 phpunit.xsd create mode 100644 tests/RetailCrm/Tests/ApiClientPacksTest.php diff --git a/phpunit.xsd b/phpunit.xsd new file mode 100644 index 0000000..0cfae85 --- /dev/null +++ b/phpunit.xsd @@ -0,0 +1,251 @@ + + + + + This Schema file defines the rules by which the XML configuration file of PHPUnit 3.7 may be structured. + + + + + + Root Element + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The main type specifying the document structure + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/RetailCrm/Tests/ApiClientPacksTest.php b/tests/RetailCrm/Tests/ApiClientPacksTest.php new file mode 100644 index 0000000..abc73ed --- /dev/null +++ b/tests/RetailCrm/Tests/ApiClientPacksTest.php @@ -0,0 +1,29 @@ +ordersPacksHistory(); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + $this->assertTrue( + isset($response['history']), + 'API returns orders assembly history' + ); + $this->assertTrue( + isset($response['generatedAt']), + 'API returns generatedAt in orders assembly history' + ); + } +} \ No newline at end of file From 9d3e2817fe5cf8b93ae8c62d98a80931e871692b Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Wed, 9 Mar 2016 02:31:29 +0300 Subject: [PATCH 08/12] PSR --- lib/RetailCrm/ApiClient.php | 587 ++++++++++++------ lib/RetailCrm/Exception/CurlException.php | 11 + .../Exception/InvalidJsonException.php | 11 + lib/RetailCrm/Http/Client.php | 68 +- lib/RetailCrm/Response/ApiResponse.php | 14 + 5 files changed, 486 insertions(+), 205 deletions(-) diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index 6bdf66d..cb546cd 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -1,11 +1,20 @@ + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion3 */ class ApiClient { @@ -22,9 +31,9 @@ class ApiClient /** * Client creating * - * @param string $url - * @param string $apiKey - * @param string $siteCode + * @param string $url api url + * @param string $apiKey api key + * @param string $site site code * * @return mixed */ @@ -36,9 +45,7 @@ class ApiClient $url = $url . 'api/' . self::VERSION; - $this->client = new Client($url, array( - 'apiKey' => $apiKey - )); + $this->client = new Client($url, array('apiKey' => $apiKey)); $this->siteCode = $site; } @@ -46,8 +53,8 @@ class ApiClient * Returns filtered orders list * * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) + * @param int $page (default: null) + * @param int $limit (default: null) * * @return ApiResponse */ @@ -65,59 +72,77 @@ class ApiClient $parameters['limit'] = (int) $limit; } - return $this->client->makeRequest('/orders', Client::METHOD_GET, $parameters); + return $this->client->makeRequest( + '/orders', + Client::METHOD_GET, + $parameters + ); } /** * Create a order * - * @param array $order - * @param string $site (default: null) + * @param array $order order data + * @param string $site (default: null) * * @return ApiResponse */ public function ordersCreate(array $order, $site = null) { if (! sizeof($order)) { - throw new \InvalidArgumentException('Parameter `order` must contains a data'); + throw new \InvalidArgumentException( + 'Parameter `order` must contains a data' + ); } - return $this->client->makeRequest("/orders/create", Client::METHOD_POST, $this->fillSite($site, array( - 'order' => json_encode($order) - ))); + return $this->client->makeRequest( + "/orders/create", + Client::METHOD_POST, + $this->fillSite($site, array('order' => json_encode($order))) + ); } /** * Save order IDs' (id and externalId) association in the CRM * - * @param array $ids + * @param array $ids order identificators * * @return ApiResponse */ public function ordersFixExternalIds(array $ids) { if (! sizeof($ids)) { - throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair'); + throw new \InvalidArgumentException( + 'Method parameter must contains at least one IDs pair' + ); } - return $this->client->makeRequest("/orders/fix-external-ids", Client::METHOD_POST, array( - 'orders' => json_encode($ids) - )); + return $this->client->makeRequest( + "/orders/fix-external-ids", + Client::METHOD_POST, + array('orders' => json_encode($ids) + ) + ); } /** * Returns a orders history * - * @param \DateTime $startDate (default: null) - * @param \DateTime $endDate (default: null) - * @param int $limit (default: 100) - * @param int $offset (default: 0) - * @param bool $skipMyChanges (default: true) + * @param \DateTime $startDate (default: null) + * @param \DateTime $endDate (default: null) + * @param int $limit (default: 100) + * @param int $offset (default: 0) + * @param bool $skipMyChanges (default: true) * * @return ApiResponse */ - public function ordersHistory(\DateTime $startDate = null, \DateTime $endDate = null, $limit = 100, $offset = 0, $skipMyChanges = true) - { + public function ordersHistory( + \DateTime $startDate = null, + \DateTime $endDate = null, + $limit = 100, + $offset = 0, + $skipMyChanges = true + ) { $parameters = array(); if ($startDate) { @@ -136,19 +161,25 @@ class ApiClient $parameters['skipMyChanges'] = (bool) $skipMyChanges; } - return $this->client->makeRequest('/orders/history', Client::METHOD_GET, $parameters); + return $this->client->makeRequest( + '/orders/history', + Client::METHOD_GET, + $parameters + ); } /** * Returns statuses of the orders * - * @param array $ids (default: array()) + * @param array $ids (default: array()) * @param array $externalIds (default: array()) * * @return ApiResponse */ - public function ordersStatuses(array $ids = array(), array $externalIds = array()) - { + public function ordersStatuses( + array $ids = array(), + array $externalIds = array() + ) { $parameters = array(); if (sizeof($ids)) { @@ -158,33 +189,41 @@ class ApiClient $parameters['externalIds'] = $externalIds; } - return $this->client->makeRequest('/orders/statuses', Client::METHOD_GET, $parameters); + return $this->client->makeRequest( + '/orders/statuses', + Client::METHOD_GET, + $parameters + ); } /** * Upload array of the orders * - * @param array $orders - * @param string $site (default: null) + * @param array $orders array of orders + * @param string $site (default: null) * * @return ApiResponse */ public function ordersUpload(array $orders, $site = null) { if (! sizeof($orders)) { - throw new \InvalidArgumentException('Parameter `orders` must contains array of the orders'); + throw new \InvalidArgumentException( + 'Parameter `orders` must contains array of the orders' + ); } - return $this->client->makeRequest("/orders/upload", Client::METHOD_POST, $this->fillSite($site, array( - 'orders' => json_encode($orders) - ))); + return $this->client->makeRequest( + "/orders/upload", + Client::METHOD_POST, + $this->fillSite($site, array('orders' => json_encode($orders))) + ); } /** * Get order by id or externalId * - * @param string $id - * @param string $by (default: 'externalId') + * @param string $id order identificator + * @param string $by (default: 'externalId') * @param string $site (default: null) * * @return ApiResponse @@ -193,48 +232,62 @@ class ApiClient { $this->checkIdParameter($by); - return $this->client->makeRequest("/orders/$id", Client::METHOD_GET, $this->fillSite($site, array( - 'by' => $by - ))); + return $this->client->makeRequest( + "/orders/$id", + Client::METHOD_GET, + $this->fillSite($site, array('by' => $by)) + ); } /** * Edit a order * - * @param array $order - * @param string $site (default: null) + * @param array $order order data + * @param string $by (default: 'externalId') + * @param string $site (default: null) * * @return ApiResponse */ public function ordersEdit(array $order, $by = 'externalId', $site = null) { if (! sizeof($order)) { - throw new \InvalidArgumentException('Parameter `order` must contains a data'); + throw new \InvalidArgumentException( + 'Parameter `order` must contains a data' + ); } $this->checkIdParameter($by); if (! isset($order[$by])) { - throw new \InvalidArgumentException(sprintf('Order array must contain the "%s" parameter.', $by)); + throw new \InvalidArgumentException( + sprintf('Order array must contain the "%s" parameter.', $by) + ); } - return $this->client->makeRequest("/orders/" . $order[$by] . "/edit", Client::METHOD_POST, $this->fillSite($site, array( - 'order' => json_encode($order), - 'by' => $by - ))); + return $this->client->makeRequest( + "/orders/" . $order[$by] . "/edit", + Client::METHOD_POST, + $this->fillSite( + $site, + array('order' => json_encode($order), 'by' => $by) + ) + ); } /** * Returns filtered customers list * * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) + * @param int $page (default: null) + * @param int $limit (default: null) * * @return ApiResponse */ - public function customersList(array $filter = array(), $page = null, $limit = null) - { + public function customersList( + array $filter = array(), + $page = null, + $limit = null + ) { $parameters = array(); if (sizeof($filter)) { @@ -247,70 +300,86 @@ class ApiClient $parameters['limit'] = (int) $limit; } - return $this->client->makeRequest('/customers', Client::METHOD_GET, $parameters); + return $this->client->makeRequest( + '/customers', + Client::METHOD_GET, + $parameters + ); } /** * Create a customer * - * @param array $customer - * @param string $site (default: null) + * @param array $customer customer data + * @param string $site (default: null) * * @return ApiResponse */ public function customersCreate(array $customer, $site = null) { if (! sizeof($customer)) { - throw new \InvalidArgumentException('Parameter `customer` must contains a data'); + throw new \InvalidArgumentException( + 'Parameter `customer` must contains a data' + ); } - return $this->client->makeRequest("/customers/create", Client::METHOD_POST, $this->fillSite($site, array( - 'customer' => json_encode($customer) - ))); + return $this->client->makeRequest( + "/customers/create", + Client::METHOD_POST, + $this->fillSite($site, array('customer' => json_encode($customer))) + ); } /** * Save customer IDs' (id and externalId) association in the CRM * - * @param array $ids + * @param array $ids ids mapping * * @return ApiResponse */ public function customersFixExternalIds(array $ids) { if (! sizeof($ids)) { - throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair'); + throw new \InvalidArgumentException( + 'Method parameter must contains at least one IDs pair' + ); } - return $this->client->makeRequest("/customers/fix-external-ids", Client::METHOD_POST, array( - 'customers' => json_encode($ids) - )); + return $this->client->makeRequest( + "/customers/fix-external-ids", + Client::METHOD_POST, + array('customers' => json_encode($ids)) + ); } /** * Upload array of the customers * - * @param array $customers - * @param string $site (default: null) + * @param array $customers array of customers + * @param string $site (default: null) * * @return ApiResponse */ public function customersUpload(array $customers, $site = null) { if (! sizeof($customers)) { - throw new \InvalidArgumentException('Parameter `customers` must contains array of the customers'); + throw new \InvalidArgumentException( + 'Parameter `customers` must contains array of the customers' + ); } - return $this->client->makeRequest("/customers/upload", Client::METHOD_POST, $this->fillSite($site, array( - 'customers' => json_encode($customers) - ))); + return $this->client->makeRequest( + "/customers/upload", + Client::METHOD_POST, + $this->fillSite($site, array('customers' => json_encode($customers))) + ); } /** * Get customer by id or externalId * - * @param string $id - * @param string $by (default: 'externalId') + * @param string $id customer identificator + * @param string $by (default: 'externalId') * @param string $site (default: null) * * @return ApiResponse @@ -319,49 +388,62 @@ class ApiClient { $this->checkIdParameter($by); - return $this->client->makeRequest("/customers/$id", Client::METHOD_GET, $this->fillSite($site, array( - 'by' => $by - ))); + return $this->client->makeRequest( + "/customers/$id", + Client::METHOD_GET, + $this->fillSite($site, array('by' => $by)) + ); } /** * Edit a customer * - * @param array $customer - * @param string $by (default: 'externalId') - * @param string $site (default: null) + * @param array $customer customer data + * @param string $by (default: 'externalId') + * @param string $site (default: null) * * @return ApiResponse */ public function customersEdit(array $customer, $by = 'externalId', $site = null) { if (! sizeof($customer)) { - throw new \InvalidArgumentException('Parameter `customer` must contains a data'); + throw new \InvalidArgumentException( + 'Parameter `customer` must contains a data' + ); } $this->checkIdParameter($by); if (! isset($customer[$by])) { - throw new \InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by)); + throw new \InvalidArgumentException( + sprintf('Customer array must contain the "%s" parameter.', $by) + ); } - return $this->client->makeRequest("/customers/" . $customer[$by] . "/edit", Client::METHOD_POST, $this->fillSite($site, array( - 'customer' => json_encode($customer), - 'by' => $by - ))); + return $this->client->makeRequest( + "/customers/" . $customer[$by] . "/edit", + Client::METHOD_POST, + $this->fillSite( + $site, + array('customer' => json_encode($customer), 'by' => $by) + ) + ); } /** * Get orders assembly list * * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) + * @param int $page (default: null) + * @param int $limit (default: null) * * @return ApiResponse */ - public function ordersPacksList(array $filter = array(), $page = null, $limit = null) - { + public function ordersPacksList( + array $filter = array(), + $page = null, + $limit = null + ) { $parameters = array(); if (sizeof($filter)) { @@ -374,13 +456,17 @@ class ApiClient $parameters['limit'] = (int) $limit; } - return $this->client->makeRequest('/orders/packs', Client::METHOD_GET, $parameters); + return $this->client->makeRequest( + '/orders/packs', + Client::METHOD_GET, + $parameters + ); } /** * Create orders assembly * - * @param array $pack + * @param array $pack pack data * @param string $site (default: null) * * @return ApiResponse @@ -388,25 +474,32 @@ class ApiClient public function ordersPacksCreate(array $pack, $site = null) { if (! sizeof($pack)) { - throw new \InvalidArgumentException('Parameter `pack` must contains a data'); + throw new \InvalidArgumentException( + 'Parameter `pack` must contains a data' + ); } - return $this->client->makeRequest("/orders/packs/create", Client::METHOD_POST, $this->fillSite($site, array( - 'pack' => json_encode($pack) - ))); + return $this->client->makeRequest( + "/orders/packs/create", + Client::METHOD_POST, + $this->fillSite($site, array('pack' => json_encode($pack))) + ); } /** * Get orders assembly history * * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) + * @param int $page (default: null) + * @param int $limit (default: null) * * @return ApiResponse */ - public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) - { + public function ordersPacksHistory( + array $filter = array(), + $page = null, + $limit = null + ) { $parameters = array(); if (sizeof($filter)) { @@ -419,13 +512,17 @@ class ApiClient $parameters['limit'] = (int) $limit; } - return $this->client->makeRequest('/orders/packs/history', Client::METHOD_GET, $parameters); + return $this->client->makeRequest( + '/orders/packs/history', + Client::METHOD_GET, + $parameters + ); } /** * Get orders assembly by id * - * @param string $id + * @param string $id pack identificator * * @return ApiResponse */ @@ -441,7 +538,7 @@ class ApiClient /** * Delete orders assembly by id * - * @param string $id + * @param string $id pack identificator * * @return ApiResponse */ @@ -451,13 +548,16 @@ class ApiClient throw new \InvalidArgumentException('Parameter `id` must be set'); } - return $this->client->makeRequest("/orders/packs/$id/delete", Client::METHOD_POST); + return $this->client->makeRequest( + "/orders/packs/$id/delete", + Client::METHOD_POST + ); } /** * Edit orders assembly * - * @param array $pack + * @param array $pack pack data * @param string $site (default: null) * * @return ApiResponse @@ -465,28 +565,36 @@ class ApiClient public function ordersPacksEdit(array $pack, $site = null) { if (! sizeof($pack) || empty($pack['id'])) { - throw new \InvalidArgumentException('Parameter `pack` must contains a data & pack `id` must be set'); + throw new \InvalidArgumentException( + 'Parameter `pack` must contains a data & pack `id` must be set' + ); } $id = $pack['id']; - return $this->client->makeRequest("/orders/packs/$id/edit", Client::METHOD_POST, $this->fillSite($site, array( - 'pack' => json_encode($pack) - ))); + return $this->client->makeRequest( + "/orders/packs/$id/edit", + Client::METHOD_POST, + $this->fillSite($site, array('pack' => json_encode($pack))) + ); } /** * Get purchace prices & stock balance * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * @param string $site (default: null) + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * @param string $site (default: null) * * @return ApiResponse */ - public function storeInventories(array $filter = array(), $page = null, $limit = null, $site = null) - { + public function storeInventories( + array $filter = array(), + $page = null, + $limit = null, + $site = null + ) { $parameters = array(); if (sizeof($filter)) { @@ -499,26 +607,34 @@ class ApiClient $parameters['limit'] = (int) $limit; } - return $this->client->makeRequest('/store/inventories', Client::METHOD_GET, $this->fillSite($site, $parameters)); + return $this->client->makeRequest( + '/store/inventories', + Client::METHOD_GET, + $this->fillSite($site, $parameters) + ); } /** * Upload store inventories * - * @param array $offers - * @param string $site (default: null) + * @param array $offers offers data + * @param string $site (default: null) * * @return ApiResponse */ public function storeInventoriesUpload(array $offers, $site = null) { if (! sizeof($offers)) { - throw new \InvalidArgumentException('Parameter `offers` must contains array of the offers'); + throw new \InvalidArgumentException( + 'Parameter `offers` must contains array of the offers' + ); } - return $this->client->makeRequest("/store/inventories/upload", Client::METHOD_POST, $this->fillSite($site, array( - 'offers' => json_encode($offers) - ))); + return $this->client->makeRequest( + "/store/inventories/upload", + Client::METHOD_POST, + $this->fillSite($site, array('offers' => json_encode($offers))) + ); } /** @@ -528,7 +644,10 @@ class ApiClient */ public function countriesList() { - return $this->client->makeRequest('/reference/countries', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/countries', + Client::METHOD_GET + ); } /** @@ -538,7 +657,10 @@ class ApiClient */ public function deliveryServicesList() { - return $this->client->makeRequest('/reference/delivery-services', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/delivery-services', + Client::METHOD_GET + ); } /** @@ -551,12 +673,16 @@ class ApiClient public function deliveryServicesEdit(array $data) { if (! isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); } - return $this->client->makeRequest('/reference/delivery-services/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'deliveryService' => json_encode($data) - )); + return $this->client->makeRequest( + '/reference/delivery-services/' . $data['code'] . '/edit', + Client::METHOD_POST, + array('deliveryService' => json_encode($data)) + ); } /** @@ -566,7 +692,10 @@ class ApiClient */ public function deliveryTypesList() { - return $this->client->makeRequest('/reference/delivery-types', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/delivery-types', + Client::METHOD_GET + ); } /** @@ -579,12 +708,16 @@ class ApiClient public function deliveryTypesEdit(array $data) { if (! isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); } - return $this->client->makeRequest('/reference/delivery-types/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'deliveryType' => json_encode($data) - )); + return $this->client->makeRequest( + '/reference/delivery-types/' . $data['code'] . '/edit', + Client::METHOD_POST, + array('deliveryType' => json_encode($data)) + ); } /** @@ -594,7 +727,9 @@ class ApiClient */ public function orderMethodsList() { - return $this->client->makeRequest('/reference/order-methods', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/order-methods', Client::METHOD_GET + ); } /** @@ -607,12 +742,16 @@ class ApiClient public function orderMethodsEdit(array $data) { if (! isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); } - return $this->client->makeRequest('/reference/order-methods/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'orderMethod' => json_encode($data) - )); + return $this->client->makeRequest( + '/reference/order-methods/' . $data['code'] . '/edit', + Client::METHOD_POST, + array('orderMethod' => json_encode($data)) + ); } /** @@ -622,7 +761,10 @@ class ApiClient */ public function orderTypesList() { - return $this->client->makeRequest('/reference/order-types', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/order-types', + Client::METHOD_GET + ); } /** @@ -635,12 +777,16 @@ class ApiClient public function orderTypesEdit(array $data) { if (! isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); } - return $this->client->makeRequest('/reference/order-types/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'orderType' => json_encode($data) - )); + return $this->client->makeRequest( + '/reference/order-types/' . $data['code'] . '/edit', + Client::METHOD_POST, + array('orderType' => json_encode($data)) + ); } /** @@ -650,7 +796,10 @@ class ApiClient */ public function paymentStatusesList() { - return $this->client->makeRequest('/reference/payment-statuses', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/payment-statuses', + Client::METHOD_GET + ); } /** @@ -663,12 +812,16 @@ class ApiClient public function paymentStatusesEdit(array $data) { if (! isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); } - return $this->client->makeRequest('/reference/payment-statuses/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'paymentStatus' => json_encode($data) - )); + return $this->client->makeRequest( + '/reference/payment-statuses/' . $data['code'] . '/edit', + Client::METHOD_POST, + array('paymentStatus' => json_encode($data)) + ); } /** @@ -678,7 +831,10 @@ class ApiClient */ public function paymentTypesList() { - return $this->client->makeRequest('/reference/payment-types', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/payment-types', + Client::METHOD_GET + ); } /** @@ -691,12 +847,16 @@ class ApiClient public function paymentTypesEdit(array $data) { if (! isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); } - return $this->client->makeRequest('/reference/payment-types/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'paymentType' => json_encode($data) - )); + return $this->client->makeRequest( + '/reference/payment-types/' . $data['code'] . '/edit', + Client::METHOD_POST, + array('paymentType' => json_encode($data)) + ); } /** @@ -706,7 +866,10 @@ class ApiClient */ public function productStatusesList() { - return $this->client->makeRequest('/reference/product-statuses', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/product-statuses', + Client::METHOD_GET + ); } /** @@ -719,12 +882,16 @@ class ApiClient public function productStatusesEdit(array $data) { if (! isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); } - return $this->client->makeRequest('/reference/product-statuses/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'productStatus' => json_encode($data) - )); + return $this->client->makeRequest( + '/reference/product-statuses/' . $data['code'] . '/edit', + Client::METHOD_POST, + array('productStatus' => json_encode($data)) + ); } /** @@ -747,12 +914,16 @@ class ApiClient public function sitesEdit(array $data) { if (! isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); } - return $this->client->makeRequest('/reference/sites/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'site' => json_encode($data) - )); + return $this->client->makeRequest( + '/reference/sites/' . $data['code'] . '/edit', + Client::METHOD_POST, + array('site' => json_encode($data)) + ); } /** @@ -762,7 +933,10 @@ class ApiClient */ public function statusGroupsList() { - return $this->client->makeRequest('/reference/status-groups', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/status-groups', + Client::METHOD_GET + ); } /** @@ -785,12 +959,16 @@ class ApiClient public function statusesEdit(array $data) { if (! isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); } - return $this->client->makeRequest('/reference/statuses/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'status' => json_encode($data) - )); + return $this->client->makeRequest( + '/reference/statuses/' . $data['code'] . '/edit', + Client::METHOD_POST, + array('status' => json_encode($data)) + ); } /** @@ -813,16 +991,22 @@ class ApiClient public function storesEdit(array $data) { if (! isset($data['code'])) { - throw new \InvalidArgumentException('Data must contain "code" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); } if (! isset($data['name'])) { - throw new \InvalidArgumentException('Data must contain "name" parameter.'); + throw new \InvalidArgumentException( + 'Data must contain "name" parameter.' + ); } - return $this->client->makeRequest('/reference/stores/' . $data['code'] . '/edit', Client::METHOD_POST, array( - 'store' => json_encode($data) - )); + return $this->client->makeRequest( + '/reference/stores/' . $data['code'] . '/edit', + Client::METHOD_POST, + array('store' => json_encode($data)) + ); } /** @@ -838,10 +1022,10 @@ class ApiClient /** * Call event * - * @param string $phone - * @param string $type - * @param string $code - * @param string $status + * @param string $phone phone number + * @param string $type call type + * @param string $code additional phone code + * @param string $status call status * * @return ApiResponse */ @@ -866,30 +1050,41 @@ class ApiClient $parameters['code'] = $code; $parameters['hangupStatus'] = $status; - return $this->client->makeRequest('/telephony/call/event', Client::METHOD_POST, $parameters); + return $this->client->makeRequest( + '/telephony/call/event', + Client::METHOD_POST, + $parameters + ); } /** * Upload calls * - * @param array $calls + * @param array $calls calls data * * @return ApiResponse */ public function telephonyCallsUpload(array $calls) { if (! sizeof($calls)) { - throw new \InvalidArgumentException('Parameter `calls` must contains array of the calls'); + throw new \InvalidArgumentException( + 'Parameter `calls` must contains array of the calls' + ); } - return $this->client->makeRequest("/telephony/calls/upload", Client::METHOD_POST, array( - 'calls' => json_encode($calls) - )); + return $this->client->makeRequest( + "/telephony/calls/upload", + Client::METHOD_POST, + array('calls' => json_encode($calls)) + ); } /** * Get call manager * + * @param string $phone phone number + * @param bool $details detailed information + * * @return ApiResponse */ public function telephonyCallManager($phone, $details) @@ -901,7 +1096,10 @@ class ApiClient $parameters['phone'] = $phone; $parameters['details'] = isset($details) ? $details : 0; - return $this->client->makeRequest('/telephony/manager', Client::METHOD_GET, $parameters); + return $this->client->makeRequest( + '/telephony/manager', + Client::METHOD_GET, $parameters + ); } /** @@ -917,7 +1115,7 @@ class ApiClient /** * Set site * - * @param string $site + * @param string $site site code * * @return void */ @@ -929,7 +1127,7 @@ class ApiClient /** * Check ID parameter * - * @param string $by + * @param string $by identify by * * @return bool */ @@ -939,8 +1137,15 @@ class ApiClient 'externalId', 'id' ); - if (! in_array($by, $allowedForBy)) { - throw new \InvalidArgumentException(sprintf('Value "%s" for parameter "by" is not valid. Allowed values are %s.', $by, implode(', ', $allowedForBy))); + + if (!in_array($by, $allowedForBy)) { + throw new \InvalidArgumentException( + sprintf( + 'Value "%s" for parameter "by" is not valid. Allowed values are %s.', + $by, + implode(', ', $allowedForBy) + ) + ); } return true; @@ -949,8 +1154,8 @@ class ApiClient /** * Fill params by site value * - * @param string $site - * @param array $params + * @param string $site site code + * @param array $params input parameters * * @return array */ diff --git a/lib/RetailCrm/Exception/CurlException.php b/lib/RetailCrm/Exception/CurlException.php index 08a0924..d3b8e1a 100644 --- a/lib/RetailCrm/Exception/CurlException.php +++ b/lib/RetailCrm/Exception/CurlException.php @@ -2,6 +2,17 @@ namespace RetailCrm\Exception; +/** + * PHP version 5.3 + * + * Class CurlException + * + * @category RetailCrm + * @package RetailCrm + * @author RetailCrm + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion3 + */ class CurlException extends \RuntimeException { } diff --git a/lib/RetailCrm/Exception/InvalidJsonException.php b/lib/RetailCrm/Exception/InvalidJsonException.php index 979d12e..1a06536 100644 --- a/lib/RetailCrm/Exception/InvalidJsonException.php +++ b/lib/RetailCrm/Exception/InvalidJsonException.php @@ -2,6 +2,17 @@ namespace RetailCrm\Exception; +/** + * PHP version 5.3 + * + * Class InvalidJsonException + * + * @category RetailCrm + * @package RetailCrm + * @author RetailCrm + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion3 + */ class InvalidJsonException extends \DomainException { } diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index 8d011fb..27e65a4 100644 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -6,7 +6,15 @@ use RetailCrm\Exception\CurlException; use RetailCrm\Response\ApiResponse; /** + * PHP version 5.3 + * * HTTP client + * + * @category RetailCrm + * @package RetailCrm + * @author RetailCrm + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion3 */ class Client { @@ -17,26 +25,47 @@ class Client protected $defaultParameters; protected $retry; + /** + * Client constructor. + * + * @param string $url api url + * @param array $defaultParameters array of parameters + */ public function __construct($url, array $defaultParameters = array()) { if (false === stripos($url, 'https://')) { - throw new \InvalidArgumentException('API schema requires HTTPS protocol'); + throw new \InvalidArgumentException( + 'API schema requires HTTPS protocol' + ); } $this->url = $url; $this->defaultParameters = $defaultParameters; $this->retry = 0; + $this->curlErrors = array( + CURLE_COULDNT_RESOLVE_PROXY, + CURLE_COULDNT_RESOLVE_HOST, + CURLE_COULDNT_CONNECT, + CURLE_OPERATION_TIMEOUTED, + CURLE_HTTP_POST_ERROR, + CURLE_SSL_CONNECT_ERROR, + CURLE_SEND_ERROR, + CURLE_RECV_ERROR + ); } /** * Make HTTP request * - * @param string $path - * @param string $method (default: 'GET') - * @param array $parameters (default: array()) - * @param int $timeout - * @param bool $verify - * @param bool $debug + * @param string $path request url + * @param string $method (default: 'GET') + * @param array $parameters (default: array()) + * @param int $timeout (default: 30) + * @param bool $verify (default: false) + * @param bool $debug (default: false) + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) + * * @return ApiResponse */ public function makeRequest( @@ -49,11 +78,13 @@ class Client ) { $allowedMethods = array(self::METHOD_GET, self::METHOD_POST); if (!in_array($method, $allowedMethods)) { - throw new \InvalidArgumentException(sprintf( - 'Method "%s" is not valid. Allowed methods are %s', - $method, - implode(', ', $allowedMethods) - )); + throw new \InvalidArgumentException( + sprintf( + 'Method "%s" is not valid. Allowed methods are %s', + $method, + implode(', ', $allowedMethods) + ) + ); } $parameters = array_merge($this->defaultParameters, $parameters); @@ -76,7 +107,11 @@ class Client curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout); } else { - curl_setopt($ch, CURLOPT_TIMEOUT_MS, (int) $timeout + ($this->retry * 2000)); + curl_setopt( + $ch, + CURLOPT_TIMEOUT_MS, + (int) $timeout + ($this->retry * 2000) + ); } if (self::METHOD_POST === $method) { @@ -91,7 +126,7 @@ class Client curl_close($ch); - if ($errno && in_array($errno, array(6, 7, 28, 34, 35)) && $this->retry < 3) { + if ($errno && in_array($errno, $this->curlErrors) && $this->retry < 3) { $errno = null; $error = null; $this->retry += 1; @@ -112,6 +147,11 @@ class Client return new ApiResponse($statusCode, $responseBody); } + /** + * Retry connect + * + * @return int + */ public function getRetry() { return $this->retry; diff --git a/lib/RetailCrm/Response/ApiResponse.php b/lib/RetailCrm/Response/ApiResponse.php index 3997a72..5ae57d3 100644 --- a/lib/RetailCrm/Response/ApiResponse.php +++ b/lib/RetailCrm/Response/ApiResponse.php @@ -5,7 +5,15 @@ namespace RetailCrm\Response; use RetailCrm\Exception\InvalidJsonException; /** + * PHP version 5.3 + * * Response from retailCRM API + * + * @category RetailCrm + * @package RetailCrm + * @author RetailCrm + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion3 */ class ApiResponse implements \ArrayAccess { @@ -15,6 +23,12 @@ class ApiResponse implements \ArrayAccess // response assoc array protected $response; + /** + * ApiResponse constructor. + * + * @param int $statusCode HTTP status code + * @param mixed $responseBody HTTP body + */ public function __construct($statusCode, $responseBody = null) { $this->statusCode = (int) $statusCode; From 31b1d9d0f1bc3323aa12ae2c328906521531408d Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Wed, 9 Mar 2016 02:34:13 +0300 Subject: [PATCH 09/12] prepare multilanguage readme --- README.md | 10 ++--- README.ru.md | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 README.ru.md diff --git a/README.md b/README.md index 72d2023..fb67845 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PHP-клиент для retailCRM API -PHP-клиент для работы с [retailCRM API](http://www.retailcrm.ru/docs/Разработчики/Разработчики#api). +PHP-клиент для работы с [retailCRM API](http://www.retailcrm.ru/docs/Developers/ApiVersion3). Рекомендуем обращаться к [документации](http://retailcrm.github.io/api-client-php) по библиотеке, в частности по классу [RetailCrm\ApiClient](http://retailcrm.github.io/api-client-php/class-RetailCrm.ApiClient.html). @@ -44,16 +44,16 @@ try { if ($response->isSuccessful()) { echo $response->order['totalSumm']; // или $response['order']['totalSumm']; - // или + // или // $order = $response->getOrder(); // $order['totalSumm']; } else { echo sprintf( - "Ошибка получения информации о заказа: [Статус HTTP-ответа %s] %s", + "Ошибка получения информации о заказа: [Статус HTTP-ответа %s] %s", $response->getStatusCode(), $response->getErrorMsg() ); - + // получить детализацию ошибок //if (isset($response['errors'])) { // print_r($response['errors']); @@ -91,7 +91,7 @@ if ($response->isSuccessful() && 201 === $response->getStatusCode()) { // или $response->getId(); } else { echo sprintf( - "Ошибка создания заказа: [Статус HTTP-ответа %s] %s", + "Ошибка создания заказа: [Статус HTTP-ответа %s] %s", $response->getStatusCode(), $response->getErrorMsg() ); diff --git a/README.ru.md b/README.ru.md new file mode 100644 index 0000000..fb67845 --- /dev/null +++ b/README.ru.md @@ -0,0 +1,104 @@ +# PHP-клиент для retailCRM API + +PHP-клиент для работы с [retailCRM API](http://www.retailcrm.ru/docs/Developers/ApiVersion3). + +Рекомендуем обращаться к [документации](http://retailcrm.github.io/api-client-php) по библиотеке, в частности по классу [RetailCrm\ApiClient](http://retailcrm.github.io/api-client-php/class-RetailCrm.ApiClient.html). + +## Обязательные требования + +* PHP версии 5.3 и выше +* PHP-расширение cURL + +## Установка + +1) Установите [composer](https://getcomposer.org/download/) + +2) Выполните в папке проекта: +```bash +composer require retailcrm/api-client-php ~3.0.0 +``` + +В конфиг `composer.json` вашего проекта будет добавлена библиотека `retailcrm/api-client-php`, которая установится в папку `vendor/`. При отсутствии файла конфига или папки с вендорами они будут созданы. + +В случае, если до этого в вашем проекте не использовался `composer`, подключите файл автозагрузки вендоров. Для этого укажите в коде проекта: +```php +require 'path/to/vendor/autoload.php'; +``` + +## Примеры использования + +### Получение информации о заказе +```php +$client = new \RetailCrm\ApiClient( + 'https://demo.retailcrm.ru', + 'T9DMPvuNt7FQJMszHUdG8Fkt6xHsqngH' +); + + +try { + $response = $client->ordersGet('M-2342'); +} catch (\RetailCrm\Exception\CurlException $e) { + echo "Сетевые проблемы. Ошибка подключения к retailCRM: " . $e->getMessage(); +} + +if ($response->isSuccessful()) { + echo $response->order['totalSumm']; + // или $response['order']['totalSumm']; + // или + // $order = $response->getOrder(); + // $order['totalSumm']; +} else { + echo sprintf( + "Ошибка получения информации о заказа: [Статус HTTP-ответа %s] %s", + $response->getStatusCode(), + $response->getErrorMsg() + ); + + // получить детализацию ошибок + //if (isset($response['errors'])) { + // print_r($response['errors']); + //} +} +``` + +### Создание заказа +```php + +$client = new \RetailCrm\ApiClient( + 'https://demo.retailcrm.ru', + 'T9DMPvuNt7FQJMszHUdG8Fkt6xHsqngH' +); + +try { + $response = $client->ordersCreate(array( + 'externalId' => 'some-shop-order-id', + 'firstName' => 'Vasily', + 'lastName' => 'Pupkin', + 'items' => array( + //... + ), + 'delivery' => array( + 'code' => 'russian-post', + ) + )); +} catch (\RetailCrm\Exception\CurlException $e) { + echo "Сетевые проблемы. Ошибка подключения к retailCRM: " . $e->getMessage(); +} + +if ($response->isSuccessful() && 201 === $response->getStatusCode()) { + echo 'Заказ успешно создан. ID заказа в retailCRM = ' . $response->id; + // или $response['id']; + // или $response->getId(); +} else { + echo sprintf( + "Ошибка создания заказа: [Статус HTTP-ответа %s] %s", + $response->getStatusCode(), + $response->getErrorMsg() + ); + + // получить детализацию ошибок + //if (isset($response['errors'])) { + // print_r($response['errors']); + //} +} +``` From ecb26bc1ff8aef731296d3a1647a7df7b2828a58 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Sat, 12 Mar 2016 01:54:33 +0300 Subject: [PATCH 10/12] PSR-2, refactoring, tests --- .gitignore | 3 +- README.md | 50 ++- composer.json | 13 +- lib/RetailCrm/ApiClient.php | 355 ++++++++++++++---- lib/RetailCrm/Http/Client.php | 74 ++-- lib/RetailCrm/Response/ApiResponse.php | 17 + phpunit.xml.dist | 3 + tests/RetailCrm/Test/TestCase.php | 11 +- tests/RetailCrm/Tests/ApiClientPacksTest.php | 64 +++- tests/RetailCrm/Tests/ApiClientStoreTest.php | 30 +- tests/RetailCrm/Tests/Http/ClientTest.php | 10 - .../Tests/Response/ApiResponseTest.php | 2 +- 12 files changed, 458 insertions(+), 174 deletions(-) diff --git a/.gitignore b/.gitignore index 620af19..0e22d4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /vendor /bin composer.lock +composer.phar phpunit.xml .idea .DS_Store @@ -8,4 +9,4 @@ phpunit.xml .buildpath .project .swp -/nbproject \ No newline at end of file +/nbproject diff --git a/README.md b/README.md index fb67845..aa065e6 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,31 @@ -# PHP-клиент для retailCRM API +# retailCRM API PHP client -PHP-клиент для работы с [retailCRM API](http://www.retailcrm.ru/docs/Developers/ApiVersion3). +PHP-client for [retailCRM API](http://www.retailcrm.ru/docs/Developers/ApiVersion3). -Рекомендуем обращаться к [документации](http://retailcrm.github.io/api-client-php) по библиотеке, в частности по классу [RetailCrm\ApiClient](http://retailcrm.github.io/api-client-php/class-RetailCrm.ApiClient.html). +Use [API documentation](http://retailcrm.github.io/api-client-php) -## Обязательные требования +## Requirements -* PHP версии 5.3 и выше -* PHP-расширение cURL +* PHP 5.3 and above +* PHP's cURL support -## Установка +## Install -1) Установите [composer](https://getcomposer.org/download/) +1) Get [composer](https://getcomposer.org/download/) -2) Выполните в папке проекта: +2) Run into your project directory: ```bash composer require retailcrm/api-client-php ~3.0.0 ``` -В конфиг `composer.json` вашего проекта будет добавлена библиотека `retailcrm/api-client-php`, которая установится в папку `vendor/`. При отсутствии файла конфига или папки с вендорами они будут созданы. - -В случае, если до этого в вашем проекте не использовался `composer`, подключите файл автозагрузки вендоров. Для этого укажите в коде проекта: +If you have not used `composer` before, include autoloader into your project. ```php require 'path/to/vendor/autoload.php'; ``` -## Примеры использования +## Usage -### Получение информации о заказе +### Get order ```php $client = new \RetailCrm\ApiClient( 'https://demo.retailcrm.ru', @@ -38,30 +36,30 @@ $client = new \RetailCrm\ApiClient( try { $response = $client->ordersGet('M-2342'); } catch (\RetailCrm\Exception\CurlException $e) { - echo "Сетевые проблемы. Ошибка подключения к retailCRM: " . $e->getMessage(); + echo "Connection error: " . $e->getMessage(); } if ($response->isSuccessful()) { echo $response->order['totalSumm']; - // или $response['order']['totalSumm']; - // или + // or $response['order']['totalSumm']; + // or // $order = $response->getOrder(); // $order['totalSumm']; } else { echo sprintf( - "Ошибка получения информации о заказа: [Статус HTTP-ответа %s] %s", + "Error: [HTTP-code %s] %s", $response->getStatusCode(), $response->getErrorMsg() ); - // получить детализацию ошибок + // error details //if (isset($response['errors'])) { // print_r($response['errors']); //} } ``` -### Создание заказа +### Create order ```php $client = new \RetailCrm\ApiClient( @@ -82,21 +80,21 @@ try { ) )); } catch (\RetailCrm\Exception\CurlException $e) { - echo "Сетевые проблемы. Ошибка подключения к retailCRM: " . $e->getMessage(); + echo "Connection error: " . $e->getMessage(); } if ($response->isSuccessful() && 201 === $response->getStatusCode()) { - echo 'Заказ успешно создан. ID заказа в retailCRM = ' . $response->id; - // или $response['id']; - // или $response->getId(); + echo 'Order successfully created. Order ID into retailCRM = ' . $response->id; + // or $response['id']; + // or $response->getId(); } else { echo sprintf( - "Ошибка создания заказа: [Статус HTTP-ответа %s] %s", + "Error: [HTTP-code %s] %s", $response->getStatusCode(), $response->getErrorMsg() ); - // получить детализацию ошибок + // error details //if (isset($response['errors'])) { // print_r($response['errors']); //} diff --git a/composer.json b/composer.json index 8280092..d933d6d 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,7 @@ "type": "library", "keywords": ["API", "retailCRM", "REST"], "homepage": "http://www.retailcrm.ru/", + "license": "MIT", "authors": [ { "name": "retailCRM", @@ -12,15 +13,17 @@ ], "require": { "php": ">=5.3.0", - "ext-curl": "*" + "ext-curl": "*", + "phpunit/php-code-coverage": "3.3.0", + "phpunit/php-invoker": "1.1.4" }, "require-dev": { - "phpunit/phpunit": "3.7.*", - "phpmd/phpmd": "2.1.*", + "phpunit/phpunit": "5.2.*", + "phpmd/phpmd": "2.4.*", "sebastian/phpcpd": "2.0.*", "sebastian/phpdcd": "1.0.*", - "squizlabs/php_codesniffer": "dev-master", - "apigen/apigen": "~4.0@dev" + "squizlabs/php_codesniffer": "2.5.*", + "apigen/apigen": "4.1.*" }, "support": { "email": "support@intarocrm.ru" diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index cb546cd..1adfe30 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -35,11 +35,13 @@ class ApiClient * @param string $apiKey api key * @param string $site site code * + * @throws \InvalidArgumentException + * * @return mixed */ public function __construct($url, $apiKey, $site = null) { - if ('/' != substr($url, strlen($url) - 1, 1)) { + if ('/' !== substr($url, strlen($url) - 1, 1)) { $url .= '/'; } @@ -56,13 +58,17 @@ class ApiClient * @param int $page (default: null) * @param int $limit (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersList(array $filter = array(), $page = null, $limit = null) { $parameters = array(); - if (sizeof($filter)) { + if (count($filter)) { $parameters['filter'] = $filter; } if (null !== $page) { @@ -85,18 +91,22 @@ class ApiClient * @param array $order order data * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersCreate(array $order, $site = null) { - if (! sizeof($order)) { + if (!count($order)) { throw new \InvalidArgumentException( 'Parameter `order` must contains a data' ); } return $this->client->makeRequest( - "/orders/create", + '/orders/create', Client::METHOD_POST, $this->fillSite($site, array('order' => json_encode($order))) ); @@ -107,18 +117,22 @@ class ApiClient * * @param array $ids order identificators * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersFixExternalIds(array $ids) { - if (! sizeof($ids)) { + if (! count($ids)) { throw new \InvalidArgumentException( 'Method parameter must contains at least one IDs pair' ); } return $this->client->makeRequest( - "/orders/fix-external-ids", + '/orders/fix-external-ids', Client::METHOD_POST, array('orders' => json_encode($ids) ) @@ -134,6 +148,10 @@ class ApiClient * @param int $offset (default: 0) * @param bool $skipMyChanges (default: true) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersHistory( @@ -174,6 +192,10 @@ class ApiClient * @param array $ids (default: array()) * @param array $externalIds (default: array()) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersStatuses( @@ -182,10 +204,10 @@ class ApiClient ) { $parameters = array(); - if (sizeof($ids)) { + if (count($ids)) { $parameters['ids'] = $ids; } - if (sizeof($externalIds)) { + if (count($externalIds)) { $parameters['externalIds'] = $externalIds; } @@ -202,18 +224,22 @@ class ApiClient * @param array $orders array of orders * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersUpload(array $orders, $site = null) { - if (! sizeof($orders)) { + if (!count($orders)) { throw new \InvalidArgumentException( 'Parameter `orders` must contains array of the orders' ); } return $this->client->makeRequest( - "/orders/upload", + '/orders/upload', Client::METHOD_POST, $this->fillSite($site, array('orders' => json_encode($orders))) ); @@ -226,6 +252,10 @@ class ApiClient * @param string $by (default: 'externalId') * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersGet($id, $by = 'externalId', $site = null) @@ -246,11 +276,15 @@ class ApiClient * @param string $by (default: 'externalId') * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersEdit(array $order, $by = 'externalId', $site = null) { - if (! sizeof($order)) { + if (!count($order)) { throw new \InvalidArgumentException( 'Parameter `order` must contains a data' ); @@ -258,14 +292,14 @@ class ApiClient $this->checkIdParameter($by); - if (! isset($order[$by])) { + if (!array_key_exists($by, $order)) { throw new \InvalidArgumentException( sprintf('Order array must contain the "%s" parameter.', $by) ); } return $this->client->makeRequest( - "/orders/" . $order[$by] . "/edit", + sprintf('/orders/%s/edit', $order[$by]), Client::METHOD_POST, $this->fillSite( $site, @@ -281,6 +315,10 @@ class ApiClient * @param int $page (default: null) * @param int $limit (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function customersList( @@ -290,7 +328,7 @@ class ApiClient ) { $parameters = array(); - if (sizeof($filter)) { + if (count($filter)) { $parameters['filter'] = $filter; } if (null !== $page) { @@ -313,18 +351,22 @@ class ApiClient * @param array $customer customer data * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function customersCreate(array $customer, $site = null) { - if (! sizeof($customer)) { + if (! count($customer)) { throw new \InvalidArgumentException( 'Parameter `customer` must contains a data' ); } return $this->client->makeRequest( - "/customers/create", + '/customers/create', Client::METHOD_POST, $this->fillSite($site, array('customer' => json_encode($customer))) ); @@ -335,18 +377,22 @@ class ApiClient * * @param array $ids ids mapping * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function customersFixExternalIds(array $ids) { - if (! sizeof($ids)) { + if (! count($ids)) { throw new \InvalidArgumentException( 'Method parameter must contains at least one IDs pair' ); } return $this->client->makeRequest( - "/customers/fix-external-ids", + '/customers/fix-external-ids', Client::METHOD_POST, array('customers' => json_encode($ids)) ); @@ -358,18 +404,22 @@ class ApiClient * @param array $customers array of customers * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function customersUpload(array $customers, $site = null) { - if (! sizeof($customers)) { + if (! count($customers)) { throw new \InvalidArgumentException( 'Parameter `customers` must contains array of the customers' ); } return $this->client->makeRequest( - "/customers/upload", + '/customers/upload', Client::METHOD_POST, $this->fillSite($site, array('customers' => json_encode($customers))) ); @@ -382,6 +432,10 @@ class ApiClient * @param string $by (default: 'externalId') * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function customersGet($id, $by = 'externalId', $site = null) @@ -402,11 +456,15 @@ class ApiClient * @param string $by (default: 'externalId') * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function customersEdit(array $customer, $by = 'externalId', $site = null) { - if (! sizeof($customer)) { + if (!count($customer)) { throw new \InvalidArgumentException( 'Parameter `customer` must contains a data' ); @@ -414,14 +472,14 @@ class ApiClient $this->checkIdParameter($by); - if (! isset($customer[$by])) { + if (!array_key_exists($by, $customer)) { throw new \InvalidArgumentException( sprintf('Customer array must contain the "%s" parameter.', $by) ); } return $this->client->makeRequest( - "/customers/" . $customer[$by] . "/edit", + sprintf('/customers/%s/edit', $customer[$by]), Client::METHOD_POST, $this->fillSite( $site, @@ -437,6 +495,10 @@ class ApiClient * @param int $page (default: null) * @param int $limit (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersPacksList( @@ -446,7 +508,7 @@ class ApiClient ) { $parameters = array(); - if (sizeof($filter)) { + if (count($filter)) { $parameters['filter'] = $filter; } if (null !== $page) { @@ -469,18 +531,22 @@ class ApiClient * @param array $pack pack data * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersPacksCreate(array $pack, $site = null) { - if (! sizeof($pack)) { + if (!count($pack)) { throw new \InvalidArgumentException( 'Parameter `pack` must contains a data' ); } return $this->client->makeRequest( - "/orders/packs/create", + '/orders/packs/create', Client::METHOD_POST, $this->fillSite($site, array('pack' => json_encode($pack))) ); @@ -493,6 +559,10 @@ class ApiClient * @param int $page (default: null) * @param int $limit (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersPacksHistory( @@ -502,7 +572,7 @@ class ApiClient ) { $parameters = array(); - if (sizeof($filter)) { + if (count($filter)) { $parameters['filter'] = $filter; } if (null !== $page) { @@ -524,6 +594,10 @@ class ApiClient * * @param string $id pack identificator * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersPacksGet($id) @@ -532,7 +606,10 @@ class ApiClient throw new \InvalidArgumentException('Parameter `id` must be set'); } - return $this->client->makeRequest("/orders/packs/$id", Client::METHOD_GET); + return $this->client->makeRequest( + "/orders/packs/$id", + Client::METHOD_GET + ); } /** @@ -540,6 +617,10 @@ class ApiClient * * @param string $id pack identificator * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersPacksDelete($id) @@ -549,7 +630,7 @@ class ApiClient } return $this->client->makeRequest( - "/orders/packs/$id/delete", + sprintf('/orders/packs/%s/delete', $id), Client::METHOD_POST ); } @@ -560,20 +641,22 @@ class ApiClient * @param array $pack pack data * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function ordersPacksEdit(array $pack, $site = null) { - if (! sizeof($pack) || empty($pack['id'])) { + if (!count($pack) || empty($pack['id'])) { throw new \InvalidArgumentException( 'Parameter `pack` must contains a data & pack `id` must be set' ); } - $id = $pack['id']; - return $this->client->makeRequest( - "/orders/packs/$id/edit", + sprintf('/orders/packs/%s/edit', $pack['id']), Client::METHOD_POST, $this->fillSite($site, array('pack' => json_encode($pack))) ); @@ -585,19 +668,21 @@ class ApiClient * @param array $filter (default: array()) * @param int $page (default: null) * @param int $limit (default: null) - * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException * * @return ApiResponse */ public function storeInventories( array $filter = array(), $page = null, - $limit = null, - $site = null + $limit = null ) { $parameters = array(); - if (sizeof($filter)) { + if (count($filter)) { $parameters['filter'] = $filter; } if (null !== $page) { @@ -610,7 +695,7 @@ class ApiClient return $this->client->makeRequest( '/store/inventories', Client::METHOD_GET, - $this->fillSite($site, $parameters) + $parameters ); } @@ -620,18 +705,22 @@ class ApiClient * @param array $offers offers data * @param string $site (default: null) * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function storeInventoriesUpload(array $offers, $site = null) { - if (! sizeof($offers)) { + if (!count($offers)) { throw new \InvalidArgumentException( 'Parameter `offers` must contains array of the offers' ); } return $this->client->makeRequest( - "/store/inventories/upload", + '/store/inventories/upload', Client::METHOD_POST, $this->fillSite($site, array('offers' => json_encode($offers))) ); @@ -640,6 +729,10 @@ class ApiClient /** * Returns available county list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function countriesList() @@ -653,6 +746,10 @@ class ApiClient /** * Returns deliveryServices list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function deliveryServicesList() @@ -668,18 +765,22 @@ class ApiClient * * @param array $data delivery service data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function deliveryServicesEdit(array $data) { - if (! isset($data['code'])) { + if (!array_key_exists('code', $data)) { throw new \InvalidArgumentException( 'Data must contain "code" parameter.' ); } return $this->client->makeRequest( - '/reference/delivery-services/' . $data['code'] . '/edit', + sprintf('/reference/delivery-services/%s/edit', $data['code']), Client::METHOD_POST, array('deliveryService' => json_encode($data)) ); @@ -688,6 +789,10 @@ class ApiClient /** * Returns deliveryTypes list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function deliveryTypesList() @@ -703,18 +808,22 @@ class ApiClient * * @param array $data delivery type data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function deliveryTypesEdit(array $data) { - if (! isset($data['code'])) { + if (!array_key_exists('code', $data)) { throw new \InvalidArgumentException( 'Data must contain "code" parameter.' ); } return $this->client->makeRequest( - '/reference/delivery-types/' . $data['code'] . '/edit', + sprintf('/reference/delivery-types/%s/edit', $data['code']), Client::METHOD_POST, array('deliveryType' => json_encode($data)) ); @@ -723,12 +832,17 @@ class ApiClient /** * Returns orderMethods list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function orderMethodsList() { return $this->client->makeRequest( - '/reference/order-methods', Client::METHOD_GET + '/reference/order-methods', + Client::METHOD_GET ); } @@ -737,18 +851,22 @@ class ApiClient * * @param array $data order method data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function orderMethodsEdit(array $data) { - if (! isset($data['code'])) { + if (!array_key_exists('code', $data)) { throw new \InvalidArgumentException( 'Data must contain "code" parameter.' ); } return $this->client->makeRequest( - '/reference/order-methods/' . $data['code'] . '/edit', + sprintf('/reference/order-methods/%s/edit', $data['code']), Client::METHOD_POST, array('orderMethod' => json_encode($data)) ); @@ -757,6 +875,10 @@ class ApiClient /** * Returns orderTypes list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function orderTypesList() @@ -772,18 +894,22 @@ class ApiClient * * @param array $data order type data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function orderTypesEdit(array $data) { - if (! isset($data['code'])) { + if (!array_key_exists('code', $data)) { throw new \InvalidArgumentException( 'Data must contain "code" parameter.' ); } return $this->client->makeRequest( - '/reference/order-types/' . $data['code'] . '/edit', + sprintf('/reference/order-types/%s/edit', $data['code']), Client::METHOD_POST, array('orderType' => json_encode($data)) ); @@ -792,6 +918,10 @@ class ApiClient /** * Returns paymentStatuses list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function paymentStatusesList() @@ -807,18 +937,22 @@ class ApiClient * * @param array $data payment status data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function paymentStatusesEdit(array $data) { - if (! isset($data['code'])) { + if (!array_key_exists('code', $data)) { throw new \InvalidArgumentException( 'Data must contain "code" parameter.' ); } return $this->client->makeRequest( - '/reference/payment-statuses/' . $data['code'] . '/edit', + sprintf('/reference/payment-statuses/%s/edit', $data['code']), Client::METHOD_POST, array('paymentStatus' => json_encode($data)) ); @@ -827,6 +961,10 @@ class ApiClient /** * Returns paymentTypes list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function paymentTypesList() @@ -842,18 +980,22 @@ class ApiClient * * @param array $data payment type data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function paymentTypesEdit(array $data) { - if (! isset($data['code'])) { + if (!array_key_exists('code', $data)) { throw new \InvalidArgumentException( 'Data must contain "code" parameter.' ); } return $this->client->makeRequest( - '/reference/payment-types/' . $data['code'] . '/edit', + sprintf('/reference/payment-types/%s/edit', $data['code']), Client::METHOD_POST, array('paymentType' => json_encode($data)) ); @@ -862,6 +1004,10 @@ class ApiClient /** * Returns productStatuses list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function productStatusesList() @@ -877,18 +1023,22 @@ class ApiClient * * @param array $data product status data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function productStatusesEdit(array $data) { - if (! isset($data['code'])) { + if (!array_key_exists('code', $data)) { throw new \InvalidArgumentException( 'Data must contain "code" parameter.' ); } return $this->client->makeRequest( - '/reference/product-statuses/' . $data['code'] . '/edit', + sprintf('/reference/product-statuses/%s/edit', $data['code']), Client::METHOD_POST, array('productStatus' => json_encode($data)) ); @@ -897,11 +1047,18 @@ class ApiClient /** * Returns sites list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function sitesList() { - return $this->client->makeRequest('/reference/sites', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/sites', + Client::METHOD_GET + ); } /** @@ -909,18 +1066,22 @@ class ApiClient * * @param array $data site data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function sitesEdit(array $data) { - if (! isset($data['code'])) { + if (!array_key_exists('code', $data)) { throw new \InvalidArgumentException( 'Data must contain "code" parameter.' ); } return $this->client->makeRequest( - '/reference/sites/' . $data['code'] . '/edit', + sprintf('/reference/sites/%s/edit', $data['code']), Client::METHOD_POST, array('site' => json_encode($data)) ); @@ -929,6 +1090,10 @@ class ApiClient /** * Returns statusGroups list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function statusGroupsList() @@ -942,11 +1107,18 @@ class ApiClient /** * Returns statuses list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function statusesList() { - return $this->client->makeRequest('/reference/statuses', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/statuses', + Client::METHOD_GET + ); } /** @@ -954,18 +1126,22 @@ class ApiClient * * @param array $data status data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function statusesEdit(array $data) { - if (! isset($data['code'])) { + if (!array_key_exists('code', $data)) { throw new \InvalidArgumentException( 'Data must contain "code" parameter.' ); } return $this->client->makeRequest( - '/reference/statuses/' . $data['code'] . '/edit', + sprintf('/reference/statuses/%s/edit', $data['code']), Client::METHOD_POST, array('status' => json_encode($data)) ); @@ -974,11 +1150,18 @@ class ApiClient /** * Returns stores list * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function storesList() { - return $this->client->makeRequest('/reference/stores', Client::METHOD_GET); + return $this->client->makeRequest( + '/reference/stores', + Client::METHOD_GET + ); } /** @@ -986,24 +1169,28 @@ class ApiClient * * @param array $data site data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function storesEdit(array $data) { - if (! isset($data['code'])) { + if (!array_key_exists('code', $data)) { throw new \InvalidArgumentException( 'Data must contain "code" parameter.' ); } - if (! isset($data['name'])) { + if (!array_key_exists('name', $data)) { throw new \InvalidArgumentException( 'Data must contain "name" parameter.' ); } return $this->client->makeRequest( - '/reference/stores/' . $data['code'] . '/edit', + sprintf('/reference/stores/%s/edit', $data['code']), Client::METHOD_POST, array('store' => json_encode($data)) ); @@ -1012,11 +1199,18 @@ class ApiClient /** * Update CRM basic statistic * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function statisticUpdate() { - return $this->client->makeRequest('/statistic/update', Client::METHOD_GET); + return $this->client->makeRequest( + '/statistic/update', + Client::METHOD_GET + ); } /** @@ -1027,23 +1221,27 @@ class ApiClient * @param string $code additional phone code * @param string $status call status * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function telephonyCallEvent($phone, $type, $code, $status) { - if (! isset($phone)) { + if (!isset($phone)) { throw new \InvalidArgumentException('Phone number must be set'); } $parameters['phone'] = $phone; - if (! isset($type)) { + if (!isset($type)) { throw new \InvalidArgumentException('Type must be set (in|out|hangup)'); } $parameters['type'] = $type; - if (! isset($code)) { + if (!isset($code)) { throw new \InvalidArgumentException('Code must be set'); } @@ -1062,18 +1260,22 @@ class ApiClient * * @param array $calls calls data * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function telephonyCallsUpload(array $calls) { - if (! sizeof($calls)) { + if (!count($calls)) { throw new \InvalidArgumentException( 'Parameter `calls` must contains array of the calls' ); } return $this->client->makeRequest( - "/telephony/calls/upload", + '/telephony/calls/upload', Client::METHOD_POST, array('calls' => json_encode($calls)) ); @@ -1085,6 +1287,10 @@ class ApiClient * @param string $phone phone number * @param bool $details detailed information * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * * @return ApiResponse */ public function telephonyCallManager($phone, $details) @@ -1098,7 +1304,8 @@ class ApiClient return $this->client->makeRequest( '/telephony/manager', - Client::METHOD_GET, $parameters + Client::METHOD_GET, + $parameters ); } @@ -1129,6 +1336,8 @@ class ApiClient * * @param string $by identify by * + * @throws \InvalidArgumentException + * * @return bool */ protected function checkIdParameter($by) @@ -1138,7 +1347,7 @@ class ApiClient 'id' ); - if (!in_array($by, $allowedForBy)) { + if (!in_array($by, $allowedForBy, false)) { throw new \InvalidArgumentException( sprintf( 'Value "%s" for parameter "by" is not valid. Allowed values are %s.', diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index 27e65a4..eafb079 100644 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -3,6 +3,7 @@ namespace RetailCrm\Http; use RetailCrm\Exception\CurlException; +use RetailCrm\Exception\InvalidJsonException; use RetailCrm\Response\ApiResponse; /** @@ -30,6 +31,8 @@ class Client * * @param string $url api url * @param array $defaultParameters array of parameters + * + * @throws \InvalidArgumentException */ public function __construct($url, array $defaultParameters = array()) { @@ -60,24 +63,23 @@ class Client * @param string $path request url * @param string $method (default: 'GET') * @param array $parameters (default: array()) - * @param int $timeout (default: 30) - * @param bool $verify (default: false) - * @param bool $debug (default: false) * * @SuppressWarnings(PHPMD.ExcessiveParameterList) * + * @throws \InvalidArgumentException + * @throws CurlException + * @throws InvalidJsonException + * * @return ApiResponse */ public function makeRequest( $path, $method, - array $parameters = array(), - $timeout = 30, - $verify = false, - $debug = false + array $parameters = array() ) { $allowedMethods = array(self::METHOD_GET, self::METHOD_POST); - if (!in_array($method, $allowedMethods)) { + + if (!in_array($method, $allowedMethods, false)) { throw new \InvalidArgumentException( sprintf( 'Method "%s" is not valid. Allowed methods are %s', @@ -91,53 +93,37 @@ class Client $url = $this->url . $path; - if (self::METHOD_GET === $method && sizeof($parameters)) { + if (self::METHOD_GET === $method && count($parameters)) { $url .= '?' . http_build_query($parameters, '', '&'); } - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); - curl_setopt($ch, CURLOPT_FAILONERROR, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify); - - if (!$debug) { - curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout); - } else { - curl_setopt( - $ch, - CURLOPT_TIMEOUT_MS, - (int) $timeout + ($this->retry * 2000) - ); - } + $curlHandler = curl_init(); + curl_setopt($curlHandler, CURLOPT_URL, $url); + curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($curlHandler, CURLOPT_FAILONERROR, false); + curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($curlHandler, CURLOPT_TIMEOUT, 30); + curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 30); if (self::METHOD_POST === $method) { - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); + curl_setopt($curlHandler, CURLOPT_POST, true); + curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters); } - $responseBody = curl_exec($ch); - $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - $errno = curl_errno($ch); - $error = curl_error($ch); + $responseBody = curl_exec($curlHandler); + $statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); + $errno = curl_errno($curlHandler); + $error = curl_error($curlHandler); - curl_close($ch); + curl_close($curlHandler); - if ($errno && in_array($errno, $this->curlErrors) && $this->retry < 3) { + if ($errno && in_array($errno, $this->curlErrors, false) && $this->retry < 3) { $errno = null; $error = null; - $this->retry += 1; - $this->makeRequest( - $path, - $method, - $parameters, - $timeout, - $verify, - $debug - ); + ++$this->retry; + $this->makeRequest($path, $method, $parameters); } if ($errno) { diff --git a/lib/RetailCrm/Response/ApiResponse.php b/lib/RetailCrm/Response/ApiResponse.php index 5ae57d3..8d97cd7 100644 --- a/lib/RetailCrm/Response/ApiResponse.php +++ b/lib/RetailCrm/Response/ApiResponse.php @@ -28,6 +28,8 @@ class ApiResponse implements \ArrayAccess * * @param int $statusCode HTTP status code * @param mixed $responseBody HTTP body + * + * @throws InvalidJsonException */ public function __construct($statusCode, $responseBody = null) { @@ -71,6 +73,10 @@ class ApiResponse implements \ArrayAccess * Allow to access for the property throw class method * * @param string $name + * @param $arguments + * + * @throws \InvalidArgumentException + * * @return mixed */ public function __call($name, $arguments) @@ -89,6 +95,9 @@ class ApiResponse implements \ArrayAccess * Allow to access for the property throw object property * * @param string $name + * + * @throws \InvalidArgumentException + * * @return mixed */ public function __get($name) @@ -103,6 +112,8 @@ class ApiResponse implements \ArrayAccess /** * @param mixed $offset * @param mixed $value + * + * @throws \BadMethodCallException */ public function offsetSet($offset, $value) { @@ -111,6 +122,8 @@ class ApiResponse implements \ArrayAccess /** * @param mixed $offset + * + * @throws \BadMethodCallException */ public function offsetUnset($offset) { @@ -119,6 +132,7 @@ class ApiResponse implements \ArrayAccess /** * @param mixed $offset + * * @return bool */ public function offsetExists($offset) @@ -128,6 +142,9 @@ class ApiResponse implements \ArrayAccess /** * @param mixed $offset + * + * @throws \InvalidArgumentException + * * @return mixed */ public function offsetGet($offset) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 652f4e7..6097b7b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -13,6 +13,9 @@ + + + diff --git a/tests/RetailCrm/Test/TestCase.php b/tests/RetailCrm/Test/TestCase.php index 5eafabb..57d3067 100644 --- a/tests/RetailCrm/Test/TestCase.php +++ b/tests/RetailCrm/Test/TestCase.php @@ -27,15 +27,20 @@ class TestCase extends \PHPUnit_Framework_TestCase /** * Return Client object * - * @param string $url (default: null) - * @param string $apiKey (default: null) + * @param string $url (default: null) + * @param array $defaultParameters (default: array()) + * * @return Client */ public static function getClient($url = null, $defaultParameters = array()) { return new Client( $url ?: $_SERVER['CRM_URL'] . '/api/' . ApiClient::VERSION, - array('apiKey' => isset($defaultParameters['apiKey']) ? $defaultParameters['apiKey'] : $_SERVER['CRM_API_KEY']) + array( + 'apiKey' => array_key_exists('apiKey', $defaultParameters) + ? $defaultParameters['apiKey'] + : $_SERVER['CRM_API_KEY'] + ) ); } } diff --git a/tests/RetailCrm/Tests/ApiClientPacksTest.php b/tests/RetailCrm/Tests/ApiClientPacksTest.php index abc73ed..cc51764 100644 --- a/tests/RetailCrm/Tests/ApiClientPacksTest.php +++ b/tests/RetailCrm/Tests/ApiClientPacksTest.php @@ -26,4 +26,66 @@ class ApiClientPacksTest extends TestCase 'API returns generatedAt in orders assembly history' ); } -} \ No newline at end of file + + /** + * @group integration + */ + public function testOrdersPacksCreate() + { + $client = static::getApiClient(); + $pack = array( + 'itemId' => $_SERVER['CRM_PACK_ITEM'], + 'quantity' => $_SERVER['CRM_PACK_QUANTITY'], + 'store' => $_SERVER['CRM_STORE'] + ); + + $response = $client->ordersPacksCreate($pack); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(201, $response->getStatusCode()); + $this->assertTrue($response->success); + } + + /** + * @group integration + */ + public function testOrdersPacksCreateFailed() + { + $client = static::getApiClient(); + $pack = array( + 'itemId' => 12, + 'store' => $_SERVER['CRM_STORE'], + 'quantity' => 2 + ); + + $response = $client->ordersPacksCreate($pack); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(400, $response->getStatusCode()); + $this->assertFalse($response->success); + } + + /** + * @group integration + */ + public function testOrdersPacksGet() + { + $client = static::getApiClient(); + + $response = $client->ordersPacksGet(1); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + } + + /** + * @group integration + */ + public function testOrdersPacksDelete() + { + $client = static::getApiClient(); + + $response = $client->ordersPacksDelete(1); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + } +} diff --git a/tests/RetailCrm/Tests/ApiClientStoreTest.php b/tests/RetailCrm/Tests/ApiClientStoreTest.php index de354b0..32b323c 100644 --- a/tests/RetailCrm/Tests/ApiClientStoreTest.php +++ b/tests/RetailCrm/Tests/ApiClientStoreTest.php @@ -4,6 +4,10 @@ namespace RetailCrm\Tests; use RetailCrm\Test\TestCase; +/** + * Class ApiClientStoreTest + * @package RetailCrm\Tests + */ class ApiClientStoreTest extends TestCase { /** @@ -30,8 +34,7 @@ class ApiClientStoreTest extends TestCase public function testStoreInventoriesUploadExceptionEmpty() { $client = static::getApiClient(); - - $response = $client->storeInventoriesUpload(array()); + $client->storeInventoriesUpload(array()); } /** @@ -47,20 +50,27 @@ class ApiClientStoreTest extends TestCase $response = $client->storeInventoriesUpload(array( array( 'externalId' => $externalIdA, - 'available' => 10, - 'purchasePrice' => 1700 + 'stores' => array( + array( + 'code' => $_SERVER['CRM_STORE'], + 'available' => 10, + 'purchasePrice' => 1700 + ) + ) ), array( 'externalId' => $externalIdB, - 'available' => 20, - 'purchasePrice' => 1500 + 'stores' => array( + array( + 'code' => $_SERVER['CRM_STORE'], + 'available' => 20, + 'purchasePrice' => 1500 + ) + ) ), )); $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertTrue( - $response->isSuccessful(), - 'Got offer' - ); + $this->assertTrue($response->isSuccessful()); } /** diff --git a/tests/RetailCrm/Tests/Http/ClientTest.php b/tests/RetailCrm/Tests/Http/ClientTest.php index 61e614c..bbecdf8 100644 --- a/tests/RetailCrm/Tests/Http/ClientTest.php +++ b/tests/RetailCrm/Tests/Http/ClientTest.php @@ -58,14 +58,4 @@ class ClientTest extends TestCase $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); $this->assertEquals(200, $response->getStatusCode()); } - - /** - * @group integration - */ - public function testMakeRequestRepeatOnTimeout() - { - $client = static::getClient(); - $response = $client->makeRequest('/orders', Client::METHOD_GET, array(), 1, false, true); - $this->assertGreaterThanOrEqual(1, $client->retry); - } } diff --git a/tests/RetailCrm/Tests/Response/ApiResponseTest.php b/tests/RetailCrm/Tests/Response/ApiResponseTest.php index fa70176..74e0fab 100644 --- a/tests/RetailCrm/Tests/Response/ApiResponseTest.php +++ b/tests/RetailCrm/Tests/Response/ApiResponseTest.php @@ -29,7 +29,7 @@ class ApiResponseTest extends TestCase /** * @group unit - * @expectedException RetailCrm\Exception\InvalidJsonException + * @expectedException \RetailCrm\Exception\InvalidJsonException */ public function testJsonInvalid() { From 4881a416be7a18522c77d852f42a7fec6b56d197 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Sat, 12 Mar 2016 02:01:43 +0300 Subject: [PATCH 11/12] fix require section --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index d933d6d..999437a 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,12 @@ ], "require": { "php": ">=5.3.0", - "ext-curl": "*", - "phpunit/php-code-coverage": "3.3.0", - "phpunit/php-invoker": "1.1.4" + "ext-curl": "*" }, "require-dev": { - "phpunit/phpunit": "5.2.*", + "phpunit/phpunit": "5.2.*",, + "phpunit/php-code-coverage": "3.3.0", + "phpunit/php-invoker": "1.1.4" "phpmd/phpmd": "2.4.*", "sebastian/phpcpd": "2.0.*", "sebastian/phpdcd": "1.0.*", From 1387fbca3825a513a03863f73116a678d1a1f806 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Sat, 12 Mar 2016 02:03:50 +0300 Subject: [PATCH 12/12] fix readme --- README.md | 2 +- README.ru.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aa065e6..994dea7 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Use [API documentation](http://retailcrm.github.io/api-client-php) 2) Run into your project directory: ```bash -composer require retailcrm/api-client-php ~3.0.0 +composer require retailcrm/api-client-php ~3.0.0 --no-dev ``` If you have not used `composer` before, include autoloader into your project. diff --git a/README.ru.md b/README.ru.md index fb67845..37ab91e 100644 --- a/README.ru.md +++ b/README.ru.md @@ -15,7 +15,7 @@ PHP-клиент для работы с [retailCRM API](http://www.retailcrm.ru/ 2) Выполните в папке проекта: ```bash -composer require retailcrm/api-client-php ~3.0.0 +composer require retailcrm/api-client-php ~3.0.0 --no-dev ``` В конфиг `composer.json` вашего проекта будет добавлена библиотека `retailcrm/api-client-php`, которая установится в папку `vendor/`. При отсутствии файла конфига или папки с вендорами они будут созданы.