1
0
mirror of synced 2024-11-25 06:46:07 +03:00

added throwing NotFoundException

This commit is contained in:
Артаханов Андрей 2020-12-29 12:18:32 +03:00
parent 0a0bb0e461
commit db549ab357
2 changed files with 60 additions and 14 deletions

View File

@ -0,0 +1,24 @@
<?php
/**
* PHP version 5.4
*
* Class LimitException
*
* @category RetailCrm
* @package RetailCrm
*/
namespace RetailCrm\Exception;
/**
* PHP version 5.4
*
* Class NotFoundException
*
* @category RetailCrm
* @package RetailCrm
*/
class NotFoundException extends \DomainException
{
}

View File

@ -14,6 +14,7 @@ namespace RetailCrm\Http;
use RetailCrm\Exception\CurlException; use RetailCrm\Exception\CurlException;
use RetailCrm\Exception\InvalidJsonException; use RetailCrm\Exception\InvalidJsonException;
use RetailCrm\Exception\LimitException; use RetailCrm\Exception\LimitException;
use RetailCrm\Exception\NotFoundException;
use RetailCrm\Response\ApiResponse; use RetailCrm\Response\ApiResponse;
/** /**
@ -126,20 +127,7 @@ class Client
} }
$responseBody = curl_exec($curlHandler); $responseBody = curl_exec($curlHandler);
$statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); $statusCode = $this->checkResponse($curlHandler, $method);
if ($statusCode == 503) {
throw new LimitException("Service temporary unavailable");
}
$errno = curl_errno($curlHandler);
$error = curl_error($curlHandler);
curl_close($curlHandler);
if ($errno) {
throw new CurlException($error, $errno);
}
return new ApiResponse($statusCode, $responseBody); return new ApiResponse($statusCode, $responseBody);
} }
@ -178,4 +166,38 @@ class Client
{ {
$this->options = $options; $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;
}
} }