1
0
mirror of synced 2024-11-22 21:06:01 +03:00

ErrorResponse & client now can send request

This commit is contained in:
Pavel 2020-09-29 17:46:00 +03:00
parent fd2241dbe4
commit 21ff82c1b8
9 changed files with 231 additions and 9 deletions

View File

@ -43,7 +43,7 @@
"post-install-cmd": "cghooks add --ignore-lock", "post-install-cmd": "cghooks add --ignore-lock",
"post-update-cmd": "cghooks update", "post-update-cmd": "cghooks update",
"phpunit": "./vendor/bin/phpunit -c phpunit.xml.dist", "phpunit": "./vendor/bin/phpunit -c phpunit.xml.dist",
"phpmd": "./vendor/bin/phpmd src text controversial,design,./phpmd.xml", "phpmd": "./vendor/bin/phpmd src text controversial,./phpmd.xml",
"phpcs": "./vendor/bin/phpcs -p src --runtime-set testVersion 7.3", "phpcs": "./vendor/bin/phpcs -p src --runtime-set testVersion 7.3",
"lint": "composer run-script phpcs && composer run-script phpmd", "lint": "composer run-script phpcs && composer run-script phpmd",
"ci": "composer run-script lint && composer run-script phpunit" "ci": "composer run-script lint && composer run-script phpunit"
@ -51,7 +51,6 @@
"extra": { "extra": {
"hooks": { "hooks": {
"pre-commit": [ "pre-commit": [
"echo => Running code quality tools...",
"composer run-script lint" "composer run-script lint"
] ]
} }

View File

@ -19,11 +19,19 @@
<rule ref="rulesets/codesize.xml"> <rule ref="rulesets/codesize.xml">
<exclude name="TooManyPublicMethods" /> <exclude name="TooManyPublicMethods" />
</rule> </rule>
<rule ref="rulesets/design.xml">
<exclude name="CouplingBetweenObjects" />
</rule>
<rule ref="rulesets/codesize.xml/TooManyPublicMethods"> <rule ref="rulesets/codesize.xml/TooManyPublicMethods">
<properties> <properties>
<property name="maxmethods" value="15" /> <property name="maxmethods" value="15" />
</properties> </properties>
</rule> </rule>
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
<properties>
<property name="maximum" value="15" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/ShortVariable"> <rule ref="rulesets/naming.xml/ShortVariable">
<properties> <properties>
<property name="minimum" value="2" /> <property name="minimum" value="2" />

View File

@ -0,0 +1,70 @@
<?php
/**
* PHP version 7.3
*
* @category TopApiException
* @package RetailCrm\Component\Exception
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Component\Exception;
use Exception;
use RetailCrm\Model\Response\Body\ErrorResponseBody;
use Throwable;
/**
* Class TopApiException
*
* @category TopApiException
* @package RetailCrm\Component\Exception
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class TopApiException extends Exception
{
/**
* @var string $subCode
*/
private $subCode;
/**
* @var string $requestId
*/
private $requestId;
/**
* TopApiException constructor.
*
* @param \RetailCrm\Model\Response\Body\ErrorResponseBody $responseBody
* @param \Throwable|null $previous
*/
public function __construct(ErrorResponseBody $responseBody, Throwable $previous = null)
{
parent::__construct($responseBody->msg, $responseBody->code, $previous);
$this->subCode = $responseBody->subCode;
$this->requestId = $responseBody->requestId;
}
/**
* @return string
*/
public function getSubCode(): string
{
return $this->subCode;
}
/**
* @return string
*/
public function getRequestId(): string
{
return $this->requestId;
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* PHP version 7.3
*
* @category TopClientException
* @package RetailCrm\Component\Exception
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Component\Exception;
use Exception;
/**
* Class TopClientException
*
* @category TopClientException
* @package RetailCrm\Component\Exception
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class TopClientException extends Exception
{
}

View File

@ -12,6 +12,8 @@
*/ */
namespace RetailCrm\Model\Request; namespace RetailCrm\Model\Request;
use RetailCrm\Model\Response\BaseResponse;
/** /**
* Class HttpDnsGetRequest * Class HttpDnsGetRequest
* *
@ -42,6 +44,6 @@ class HttpDnsGetRequest extends BaseRequest
public function getExpectedResponse(): string public function getExpectedResponse(): string
{ {
// TODO: Implement getExpectedResponse() method. // TODO: Implement getExpectedResponse() method.
return ''; return BaseResponse::class;
} }
} }

View File

@ -0,0 +1,36 @@
<?php
/**
* PHP version 7.3
*
* @category BaseResponse
* @package RetailCrm\Model\Response
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Response;
use JMS\Serializer\Annotation as JMS;
/**
* Class BaseResponse
*
* @category BaseResponse
* @package RetailCrm\Model\Response
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class BaseResponse
{
/**
* @var \RetailCrm\Model\Response\Body\ErrorResponseBody
*
* @JMS\Type("RetailCrm\Model\Response\Body\ErrorResponseBody")
* @JMS\SerializedName("error_response")
*/
public $errorResponse;
}

View File

@ -0,0 +1,60 @@
<?php
/**
* PHP version 7.3
*
* @category ErrorResponseBody
* @package RetailCrm\Model\Response\Body
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Response\Body;
use JMS\Serializer\Annotation as JMS;
/**
* Class ErrorResponseBody
*
* @category ErrorResponseBody
* @package RetailCrm\Model\Response\Body
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class ErrorResponseBody
{
/**
* @var int $code
*
* @JMS\Type("int")
* @JMS\SerializedName("code")
*/
public $code;
/**
* @var string $msg
*
* @JMS\Type("string")
* @JMS\SerializedName("msg")
*/
public $msg;
/**
* @var string $subCode
*
* @JMS\Type("string")
* @JMS\SerializedName("sub_code")
*/
public $subCode;
/**
* @var string $requestId
*
* @JMS\Type("string")
* @JMS\SerializedName("request_id")
*/
public $requestId;
}

View File

@ -14,11 +14,14 @@ namespace RetailCrm\TopClient;
use JMS\Serializer\SerializerInterface; use JMS\Serializer\SerializerInterface;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
use RetailCrm\Component\Exception\TopApiException;
use RetailCrm\Component\Exception\TopClientException;
use RetailCrm\Component\ServiceLocator; use RetailCrm\Component\ServiceLocator;
use RetailCrm\Interfaces\AppDataInterface; use RetailCrm\Interfaces\AppDataInterface;
use RetailCrm\Interfaces\AuthenticatorInterface; use RetailCrm\Interfaces\AuthenticatorInterface;
use RetailCrm\Interfaces\RequestFactoryInterface; use RetailCrm\Interfaces\RequestFactoryInterface;
use RetailCrm\Model\Request\BaseRequest; use RetailCrm\Model\Request\BaseRequest;
use RetailCrm\Model\Response\BaseResponse;
use RetailCrm\Traits\ValidatorAwareTrait; use RetailCrm\Traits\ValidatorAwareTrait;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
@ -138,17 +141,32 @@ class Client
/** /**
* @param \RetailCrm\Model\Request\BaseRequest $request * @param \RetailCrm\Model\Request\BaseRequest $request
* *
* @return void * @return \RetailCrm\Model\Response\BaseResponse
* @throws \Psr\Http\Client\ClientExceptionInterface * @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \RetailCrm\Component\Exception\ValidationException * @throws \RetailCrm\Component\Exception\ValidationException
* @throws \RetailCrm\Component\Exception\FactoryException * @throws \RetailCrm\Component\Exception\FactoryException
* * @throws \RetailCrm\Component\Exception\TopClientException
* @todo Implement this method and remove tag below. * @throws \RetailCrm\Component\Exception\TopApiException
* @SuppressWarnings(PHPMD)
*/ */
public function sendRequest(BaseRequest $request) public function sendRequest(BaseRequest $request)
{ {
$httpRequest = $this->requestFactory->fromModel($request, $this->appData, $this->authenticator); $httpRequest = $this->requestFactory->fromModel($request, $this->appData, $this->authenticator);
$response = $this->httpClient->sendRequest($httpRequest); $httpResponse = $this->httpClient->sendRequest($httpRequest);
/** @var BaseResponse $response */
$response = $this->serializer->deserialize(
$httpResponse->getBody()->getContents(),
$request->getExpectedResponse(),
$request->format
);
if (!($response instanceof BaseResponse) && !is_subclass_of($response, BaseResponse::class)) {
throw new TopClientException(sprintf('Invalid response class: %s', get_class($response)));
}
if (null !== $response->errorResponse) {
throw new TopApiException($response->errorResponse);
}
return $response;
} }
} }

View File

@ -32,7 +32,7 @@ class ClientTest extends TestCase
{ {
public function testClientRequest() public function testClientRequest()
{ {
self::markTestSkipped('Not implemented yet'); self::markTestSkipped('Should be mocked!');
$client = ClientBuilder::create() $client = ClientBuilder::create()
->setContainer($this->getContainer()) ->setContainer($this->getContainer())