From 38fa980038e5e1291f60f9fda25ce29e79640332 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Tue, 16 Jul 2019 16:41:56 +0300 Subject: [PATCH] cleanup --- README.md | 20 +++---- src/Bot/Adapter/ResponseAdapter.php | 61 --------------------- src/Bot/Client.php | 4 +- src/Bot/HttpClient.php | 22 ++++---- src/Bot/{Adapter => Model}/ModelAdapter.php | 16 +++--- src/Bot/Model/Response.php | 57 ------------------- src/Exception/CurlException.php | 29 ---------- 7 files changed, 30 insertions(+), 179 deletions(-) delete mode 100644 src/Bot/Adapter/ResponseAdapter.php rename src/Bot/{Adapter => Model}/ModelAdapter.php (74%) delete mode 100644 src/Bot/Model/Response.php delete mode 100644 src/Exception/CurlException.php diff --git a/README.md b/README.md index f0d9788..3da82c3 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ [![PHP from Packagist](https://img.shields.io/packagist/php-v/retailcrm/mg-bot-api-client-php.svg?style=flat-square)](https://packagist.org/packages/retailcrm/mg-bot-api-client-php) -# retailCRM API PHP client +# Message Gateway Bot API PHP client -This is php retailCRM MG Bot API client. +This is php library for retailCRM MG Bot API. ## Requirements @@ -32,6 +32,9 @@ require 'path/to/vendor/autoload.php'; ### Assign dialog ```php +setDialogId(60); $request->setUserId(4); + /* @var \RetailCrm\Mg\Bot\Model\Response\AssignResponse $response */ $response = $client->dialogAssign($request); -} catch (\RetailCrm\Common\Exception\CurlException $exception) { - echo $exception->getMessage(); -} catch (\RetailCrm\Common\Exception\LimitException $exception) { - echo $exception->getMessage(); -} catch (\InvalidArgumentException $exception) { - echo $exception->getMessage(); -} catch (\Exception $exception) { +} catch (Exception\LimitException | Exception\InvalidJsonException | Exception\UnauthorizedException $exception) { echo $exception->getMessage(); } -if ($response->isSuccessful()) { - $response->getPreviousResponsible(); -} +echo $response->getPreviousResponsible(); ``` ### Documentation diff --git a/src/Bot/Adapter/ResponseAdapter.php b/src/Bot/Adapter/ResponseAdapter.php deleted file mode 100644 index 850af07..0000000 --- a/src/Bot/Adapter/ResponseAdapter.php +++ /dev/null @@ -1,61 +0,0 @@ - - * @license https://opensource.org/licenses/MIT MIT License - * @link http://help.retailcrm.pro/docs/Developers - */ - -namespace RetailCrm\Mg\Bot\Adapter; - -use Psr\Http\Message\ResponseInterface; -use RetailCrm\Mg\Bot\Model\Response; - -/** - * Class ResponseAdapter - * - * @package RetailCrm\Mg\Bot - * @author retailCRM - * @license https://opensource.org/licenses/MIT MIT License - * @link http://help.retailcrm.pro/docs/Developers - */ -class ResponseAdapter -{ - /** - * @var \RetailCrm\Mg\Bot\Model\Response $model - */ - private $model; - /** - * @var \Psr\Http\Message\ResponseInterface $interface - */ - private $interface; - - /** - * ResponseAdapter constructor. - * - * @param \Psr\Http\Message\ResponseInterface $interface - */ - public function __construct(ResponseInterface $interface) - { - $this->interface = $interface; - $this->model = new Response(); - } - - /** - * Build response - * - * @return \RetailCrm\Mg\Bot\Model\Response - */ - public function build(): Response - { - $this->model->setStatus($this->interface->getStatusCode()); - $this->model->setBody((string) $this->interface->getBody()); - - return $this->model; - } -} diff --git a/src/Bot/Client.php b/src/Bot/Client.php index a99c8fd..8a7ea9b 100644 --- a/src/Bot/Client.php +++ b/src/Bot/Client.php @@ -16,7 +16,7 @@ namespace RetailCrm\Mg\Bot; use Psr\Http\Message\ResponseInterface; use RetailCrm\Common\Url; use RetailCrm\Common\Serializer; -use RetailCrm\Mg\Bot\Adapter\ModelAdapter; +use RetailCrm\Mg\Bot\Model\ModelAdapter; use RetailCrm\Mg\Bot\Model\Entity\Bot; use RetailCrm\Mg\Bot\Model\Entity\Channel\Channel; use RetailCrm\Mg\Bot\Model\Entity\Chat\Chat; @@ -58,7 +58,7 @@ class Client * * @param string $url MG API URL * @param string $token MG API Key - * @param bool $debug Enable or disable debug mode - will log all requests to STDOUT (default: false) + * @param bool $debug Enable or disable debug mode (default: false) * @param \GuzzleHttp\HandlerStack $handler GuzzleHttp::HandlerStack instance (default: null) */ public function __construct($url, $token, $debug = false, $handler = null) diff --git a/src/Bot/HttpClient.php b/src/Bot/HttpClient.php index 5cf2c47..9711200 100644 --- a/src/Bot/HttpClient.php +++ b/src/Bot/HttpClient.php @@ -23,7 +23,6 @@ use RetailCrm\Common\Exception\NotFoundException; use RetailCrm\Common\Exception\UnauthorizedException; use RetailCrm\Common\Serializer; use RetailCrm\Common\Url; -use RetailCrm\Mg\Bot\Adapter\ResponseAdapter; use RuntimeException; use Symfony\Component\Validator\Validation; use GuzzleHttp\Client; @@ -112,7 +111,7 @@ class HttpClient * @param string $method Request method (default: 'GET') * @param mixed $request Request model (default: null) * - * @return \RetailCrm\Mg\Bot\Model\Response + * @return \Psr\Http\Message\ResponseInterface * @throws \Exception */ public function makeRequest($path, $method, $request = null) @@ -139,7 +138,11 @@ class HttpClient ] ); - if (in_array($method, [self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH, self::METHOD_DELETE]) && is_string($requestBody)) { + if (in_array( + $method, + [self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH, self::METHOD_DELETE] + ) && is_string($requestBody) + ) { $request = $request->withBody(stream_for($requestBody)); } @@ -161,9 +164,7 @@ class HttpClient $this->validateResponse($responseObject); - $adapter = new ResponseAdapter($responseObject); - - return $adapter->build(); + return $responseObject; } /** @@ -248,6 +249,7 @@ class HttpClient * @param \Psr\Http\Message\ResponseInterface $responseObject * * @throws \ErrorException + * @throws \Exception */ private function validateResponse(ResponseInterface $responseObject) { @@ -257,10 +259,6 @@ class HttpClient $errorMessage = !empty($response['errorMsg']) ? $response['errorMsg'] : ''; $errorMessage = !empty($response['errors']) ? $this->getErrors($response['errors']) : $errorMessage; - /** - * responses with 400 & 460 http codes contains extended error data - * therefore they are not handled as exceptions - */ if ($statusCode == 400) { throw new RuntimeException($errorMessage); } @@ -284,6 +282,10 @@ class HttpClient if ($statusCode == 503) { throw new LimitException($errorMessage); } + + if ($statusCode >= 400) { + throw new Exception($errorMessage); + } } /** diff --git a/src/Bot/Adapter/ModelAdapter.php b/src/Bot/Model/ModelAdapter.php similarity index 74% rename from src/Bot/Adapter/ModelAdapter.php rename to src/Bot/Model/ModelAdapter.php index 47826c7..9d07262 100644 --- a/src/Bot/Adapter/ModelAdapter.php +++ b/src/Bot/Model/ModelAdapter.php @@ -11,11 +11,11 @@ * @link http://help.retailcrm.pro/docs/Developers */ -namespace RetailCrm\Mg\Bot\Adapter; +namespace RetailCrm\Mg\Bot\Model; +use Psr\Http\Message\ResponseInterface; use RetailCrm\Common\Exception\InvalidJsonException; use RetailCrm\Common\Serializer; -use RetailCrm\Mg\Bot\Model\Response; /** * Class ModelAdapter @@ -46,25 +46,25 @@ class ModelAdapter /** * Get Response Model * - * @param \RetailCrm\Mg\Bot\Model\Response $response + * @param \Psr\Http\Message\ResponseInterface $response * * @return \RetailCrm\Mg\Bot\Model\ModelInterface */ - public function getResponseModel(Response $response) + public function getResponseModel(ResponseInterface $response) { - return Serializer::deserialize($response->getBody(), $this->classname); + return Serializer::deserialize((string)$response->getBody(), $this->classname); } /** * Get Response List * - * @param \RetailCrm\Mg\Bot\Model\Response $response + * @param \Psr\Http\Message\ResponseInterface $response * * @return array */ - public function getResponseList(Response $response) + public function getResponseList(ResponseInterface $response) { - $array = json_decode($response->getBody(), true); + $array = json_decode((string)$response->getBody(), true); if (json_last_error() != JSON_ERROR_NONE) { throw new InvalidJsonException('Received invalid JSON', 1); diff --git a/src/Bot/Model/Response.php b/src/Bot/Model/Response.php deleted file mode 100644 index 0ad4cc4..0000000 --- a/src/Bot/Model/Response.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @license https://opensource.org/licenses/MIT MIT License - * @link http://help.retailcrm.pro/docs/Developers - */ - -namespace RetailCrm\Mg\Bot\Model; - -/** - * Class Response - * - * @package RetailCrm\Mg\Bot\Model - */ -class Response -{ - private $status; - - private $body; - - /** - * @return mixed - */ - public function getStatus() - { - return $this->status; - } - - /** - * @param mixed $status - */ - public function setStatus($status) - { - $this->status = $status; - } - - /** - * @return mixed - */ - public function getBody() - { - return $this->body; - } - - /** - * @param mixed $body - */ - public function setBody($body) - { - $this->body = $body; - } -} diff --git a/src/Exception/CurlException.php b/src/Exception/CurlException.php deleted file mode 100644 index 0de65da..0000000 --- a/src/Exception/CurlException.php +++ /dev/null @@ -1,29 +0,0 @@ - - * @license https://opensource.org/licenses/MIT MIT License - * @link http://help.retailcrm.pro/docs/Developers - */ - -namespace RetailCrm\Common\Exception; - -use RuntimeException; -use Throwable; - -/** - * Class CurlException - * - * @package RetailCrm\Common\Exception - * @author retailCRM - * @license https://opensource.org/licenses/MIT MIT License - * @link http://help.retailcrm.pro/docs/Developers - */ -class CurlException extends RuntimeException implements Throwable -{ -}