From db549ab357f0be3f2e4e8db04d2d6406437f6207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B0=D1=85=D0=B0=D0=BD=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?= Date: Tue, 29 Dec 2020 12:18:32 +0300 Subject: [PATCH] added throwing NotFoundException --- lib/RetailCrm/Exception/NotFoundException.php | 24 +++++++++ lib/RetailCrm/Http/Client.php | 50 +++++++++++++------ 2 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 lib/RetailCrm/Exception/NotFoundException.php diff --git a/lib/RetailCrm/Exception/NotFoundException.php b/lib/RetailCrm/Exception/NotFoundException.php new file mode 100644 index 0000000..cec6311 --- /dev/null +++ b/lib/RetailCrm/Exception/NotFoundException.php @@ -0,0 +1,24 @@ +checkResponse($curlHandler, $method); return new ApiResponse($statusCode, $responseBody); } @@ -178,4 +166,38 @@ class Client { $this->options = $options; } + + /** + * @param $curlHandler + * @param $method + * + * @return mixed + */ + private function checkResponse($curlHandler, $method) + { + $statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); + $contentType = curl_getinfo($curlHandler, CURLINFO_CONTENT_TYPE); + + if (503 === $statusCode) { + throw new LimitException("Service temporary unavailable"); + } + + if ( + 404 === $statusCode + || ('GET' !== $method && 405 === $statusCode && false !== stripos($contentType, 'text/html')) + ) { + throw new NotFoundException("Account does not exist"); + } + + $errno = curl_errno($curlHandler); + $error = curl_error($curlHandler); + + curl_close($curlHandler); + + if ($errno) { + throw new CurlException($error, $errno); + } + + return $statusCode; + } }