From de6dbdd87f55b4873c1c72aaef7c56775717f6f2 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Fri, 8 May 2015 18:03:05 +0300 Subject: [PATCH] psr2 & rollback for timeout handle --- lib/RetailCrm/ApiClient.php | 32 ++++++++++++++---------- lib/RetailCrm/Http/Client.php | 46 ++++++++++++++++------------------- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index 8f63fc9..593c63f 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -221,7 +221,7 @@ class ApiClient 'orders' => json_encode($ids), )); } - + /** * Get orders assembly history * @@ -288,11 +288,14 @@ class ApiClient return $this->client->makeRequest( "/customers/" . $customer[$by] . "/edit", Client::METHOD_POST, - $this->fillSite($site, array( - 'customer' => json_encode($customer), - 'by' => $by, + $this->fillSite( + $site, + array( + 'customer' => json_encode($customer), + 'by' => $by + ) ) - )); + ); } /** @@ -375,12 +378,13 @@ class ApiClient /** * Get purchace prices & stock balance * - * @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) + * @param string $site (default: null) * @return ApiResponse */ - public function storeInventories(array $filter = array(), $page = null, $limit = null) + public function storeInventories(array $filter = array(), $page = null, $limit = null, $site = null) { $parameters = array(); @@ -394,7 +398,7 @@ class ApiClient $parameters['limit'] = (int) $limit; } - return $this->client->makeRequest('/store/inventories', Client::METHOD_GET, $parameters); + return $this->client->makeRequest('/store/inventories', Client::METHOD_GET, $this->fillSite($site, $parameters)); } /** @@ -410,9 +414,11 @@ class ApiClient throw new \InvalidArgumentException('Parameter `offers` must contains array of the customers'); } - 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))) + ); } /** diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index b0e66d9..30f3558 100644 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -39,8 +39,14 @@ class Client * @param bool $debug * @return ApiResponse */ - public function makeRequest($path, $method, array $parameters = array(), $timeout = 30, $verify = false, $debug = false) - { + public function makeRequest( + $path, + $method, + array $parameters = array(), + $timeout = 30, + $verify = false, + $debug = false + ) { $allowedMethods = array(self::METHOD_GET, self::METHOD_POST); if (!in_array($method, $allowedMethods)) { throw new \InvalidArgumentException(sprintf( @@ -53,6 +59,7 @@ class Client $parameters = array_merge($this->defaultParameters, $parameters); $path = $this->url . $path; + if (self::METHOD_GET === $method && sizeof($parameters)) { $path .= '?' . http_build_query($parameters); } @@ -72,10 +79,21 @@ class Client curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); } - $responseBody = $this->curlExec($ch, $debug); + $responseBody = curl_exec($ch); $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $errno = curl_errno($ch); $error = curl_error($ch); + + /*if ($errno && in_array($errno, array(6, 7, 28, 34, 35)) && $this->retry < 3) { + $this->retry += 1; + + if ($debug) { + error_log('CURL RETRY #' . $this->retry . PHP_EOL, 4); + } + + $this->makeRequest($path, $method, $parameters, $timeout, $verify, $debug); + }*/ + curl_close($ch); if ($errno) { @@ -84,26 +102,4 @@ class Client return new ApiResponse($statusCode, $responseBody); } - - - /** - * @param resource $ch - * @param boolean $debug - * @return mixed - */ - private function curlExec($ch, $debug) { - $exec = curl_exec($ch); - - if (curl_errno($ch) && in_array(curl_errno($ch), array(6, 7, 28, 34, 35)) && $this->retry < 3) { - $this->retry += 1; - - if ($debug) { - error_log('CURL RETRY #' . $this->retry . PHP_EOL, 4); - } - - $this->curlExec($ch, $debug); - } - - return $exec; - } }