partial api client facade implementation (full implementation is not necessary for now)
This commit is contained in:
parent
033c23122b
commit
8c67ed45c5
12 changed files with 436 additions and 9 deletions
|
@ -253,7 +253,7 @@ class ApiClient
|
|||
);
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Upload array of the customers corporate
|
||||
*
|
||||
* @param array $customers array of customers
|
||||
|
@ -293,7 +293,7 @@ class ApiClient
|
|||
*
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function customersСorporateGet($id, $by = 'externalId', $site = null)
|
||||
public function customersCorporateGet($id, $by = 'externalId', $site = null)
|
||||
{
|
||||
$this->checkIdParameter($by);
|
||||
|
||||
|
|
|
@ -177,4 +177,12 @@ class ApiResponse implements \ArrayAccess
|
|||
|
||||
return $this->response[$offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getResponseBody()
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
|
|
189
intaro.retailcrm/lib/component/apiclient/clientfacade.php
Normal file
189
intaro.retailcrm/lib/component/apiclient/clientfacade.php
Normal file
|
@ -0,0 +1,189 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* @category Integration
|
||||
* @package Intaro\RetailCrm\Component\ApiClient
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license MIT
|
||||
* @link http://retailcrm.ru
|
||||
* @see http://retailcrm.ru/docs
|
||||
*/
|
||||
namespace Intaro\RetailCrm\Component\ApiClient;
|
||||
|
||||
use Intaro\RetailCrm\Component\Json\Deserializer;
|
||||
use Intaro\RetailCrm\Component\Json\Serializer;
|
||||
use Intaro\RetailCrm\Model\Api\Customer;
|
||||
use Intaro\RetailCrm\Model\Api\Response\CustomerChangeResponse;
|
||||
use Intaro\RetailCrm\Model\Api\Response\CustomerCorporateResponse;
|
||||
use Intaro\RetailCrm\Model\Api\Response\CustomerResponse;
|
||||
use Intaro\RetailCrm\Model\Api\Response\CustomersUploadResponse;
|
||||
use RetailCrm\ApiClient;
|
||||
|
||||
/**
|
||||
* Class ClientFacade
|
||||
*
|
||||
* @package Intaro\RetailCrm\Component\ApiClient
|
||||
*/
|
||||
class ClientFacade
|
||||
{
|
||||
/** @var string */
|
||||
public const ID = 'id';
|
||||
|
||||
/** @var string */
|
||||
public const EXTERNAL_ID = 'externalId';
|
||||
|
||||
/** @var \RetailCrm\ApiClient */
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* ClientFacade constructor.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $apiKey
|
||||
* @param string|null $site
|
||||
*/
|
||||
public function __construct(string $url, string $apiKey, string $site = null)
|
||||
{
|
||||
$this->client = new ApiClient($url, $apiKey, $site);
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy call for all methods we don't care about... or didn't implemented yet.
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $arguments
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
if (method_exists($this->client, $name)) {
|
||||
return \call_user_func_array([$this->client, $name], $arguments);
|
||||
}
|
||||
|
||||
throw new \RuntimeException(sprintf('Method "%s" doesn\'t exist.', $name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create customer
|
||||
*
|
||||
* @param \Intaro\RetailCrm\Model\Api\Customer $customer
|
||||
* @param string $site (default: null)
|
||||
*
|
||||
* @return \Intaro\RetailCrm\Model\Api\Response\CustomerChangeResponse|null
|
||||
* @throws \RetailCrm\Exception\CurlException
|
||||
* @throws \RetailCrm\Exception\InvalidJsonException
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function customersCreate(Customer $customer, $site = null): ?CustomerChangeResponse
|
||||
{
|
||||
$response = $this->client->customersCreate(
|
||||
Serializer::serializeArray($customer),
|
||||
$site
|
||||
);
|
||||
|
||||
return Deserializer::deserializeArray($response->getResponseBody(), CustomerChangeResponse::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload array of the customers
|
||||
*
|
||||
* @param array $customers array of customers
|
||||
* @param string $site (default: null)
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws \RetailCrm\Exception\CurlException
|
||||
* @throws \RetailCrm\Exception\InvalidJsonException
|
||||
*
|
||||
* @return \Intaro\RetailCrm\Model\Api\Response\CustomersUploadResponse|null
|
||||
*/
|
||||
public function customersUpload(array $customers, $site = null): ?CustomersUploadResponse
|
||||
{
|
||||
$serializedCustomers = Serializer::serializeArray(
|
||||
$customers,
|
||||
'\Intaro\RetailCrm\Model\Api\Customer[]'
|
||||
);
|
||||
$response = $this->client->customersUpload($serializedCustomers, $site);
|
||||
|
||||
return Deserializer::deserializeArray($response->getResponseBody(), CustomersUploadResponse::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customer by id or externalId
|
||||
*
|
||||
* @param int $id customer identifier
|
||||
* @param string $by (default: 'externalId')
|
||||
* @param string $site (default: null)
|
||||
*
|
||||
* @return \Intaro\RetailCrm\Model\Api\Response\CustomerResponse|null
|
||||
*/
|
||||
public function customerseGet(int $id, string $by = self::EXTERNAL_ID, $site = null): ?CustomerResponse
|
||||
{
|
||||
$response = $this->client->customersGet((string) $id, $by, $site);
|
||||
|
||||
return Deserializer::deserializeArray($response->getResponseBody(), CustomerResponse::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create customers corporate
|
||||
*
|
||||
* @param \Intaro\RetailCrm\Model\Api\Customer $customer
|
||||
* @param string $site (default: null)
|
||||
*
|
||||
* @return \Intaro\RetailCrm\Model\Api\Response\CustomerChangeResponse|null
|
||||
* @throws \RetailCrm\Exception\CurlException
|
||||
* @throws \RetailCrm\Exception\InvalidJsonException
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function customersCorporateCreate(Customer $customer, $site = null): ?CustomerChangeResponse
|
||||
{
|
||||
$response = $this->client->customersCorporateCreate(
|
||||
Serializer::serializeArray($customer),
|
||||
$site
|
||||
);
|
||||
|
||||
return Deserializer::deserializeArray($response->getResponseBody(), CustomerChangeResponse::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload array of the customers corporate
|
||||
*
|
||||
* @param array $customers array of customers
|
||||
* @param string $site (default: null)
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws \RetailCrm\Exception\CurlException
|
||||
* @throws \RetailCrm\Exception\InvalidJsonException
|
||||
*
|
||||
* @return \Intaro\RetailCrm\Model\Api\Response\CustomersUploadResponse|null
|
||||
*/
|
||||
public function customersCorporateUpload(array $customers, $site = null): ?CustomersUploadResponse
|
||||
{
|
||||
$serializedCustomers = Serializer::serializeArray(
|
||||
$customers,
|
||||
'\Intaro\RetailCrm\Model\Api\Customer[]'
|
||||
);
|
||||
$response = $this->client->customersCorporateUpload($serializedCustomers, $site);
|
||||
|
||||
return Deserializer::deserializeArray($response->getResponseBody(), CustomersUploadResponse::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customer corporate by id or externalId
|
||||
*
|
||||
* @param int $id customer identifier
|
||||
* @param string $by (default: 'externalId')
|
||||
* @param string $site (default: null)
|
||||
*
|
||||
* @return \Intaro\RetailCrm\Model\Api\Response\CustomerCorporateResponse|null
|
||||
*/
|
||||
public function customersCorporateGet(int $id, string $by = self::EXTERNAL_ID, $site = null): ?CustomerCorporateResponse
|
||||
{
|
||||
$response = $this->client->customersCorporateGet((string) $id, $by, $site);
|
||||
|
||||
return Deserializer::deserializeArray($response->getResponseBody(), CustomerCorporateResponse::class);
|
||||
}
|
||||
}
|
|
@ -26,13 +26,14 @@ class Serializer
|
|||
use AnnotationReaderTrait;
|
||||
|
||||
/**
|
||||
* @param object $object
|
||||
* @param mixed $object
|
||||
* @param string $type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function serialize($object): string
|
||||
public static function serialize($object, string $type = ''): string
|
||||
{
|
||||
$result = json_encode(static::serializeArray($object));
|
||||
$result = json_encode(static::serializeArray($object, $type));
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
throw new InvalidJsonException(json_last_error_msg(), json_last_error());
|
||||
|
@ -42,13 +43,15 @@ class Serializer
|
|||
}
|
||||
|
||||
/**
|
||||
* @param object $object
|
||||
* @param mixed $object
|
||||
* @param string $type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function serializeArray($object): array
|
||||
public static function serializeArray($object, string $type = ''): array
|
||||
{
|
||||
$result = (array) StrategyFactory::serializeStrategyByType(gettype($object))->serialize($object);
|
||||
$type = empty($type) ? gettype($object) : $type;
|
||||
$result = (array) StrategyFactory::serializeStrategyByType($type)->serialize($object);
|
||||
|
||||
return static::processPostSerialize($object, $result);
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@ namespace Intaro\RetailCrm\Model\Api;
|
|||
|
||||
use Intaro\RetailCrm\Component\Json\Mapping;
|
||||
|
||||
|
||||
/**
|
||||
* Class CodeValueModel
|
||||
*
|
||||
|
|
31
intaro.retailcrm/lib/model/api/response/createresponse.php
Normal file
31
intaro.retailcrm/lib/model/api/response/createresponse.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* @category Integration
|
||||
* @package Intaro\RetailCrm\Model\Api\Response
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license MIT
|
||||
* @link http://retailcrm.ru
|
||||
* @see http://retailcrm.ru/docs
|
||||
*/
|
||||
namespace Intaro\RetailCrm\Model\Api\Response;
|
||||
|
||||
use Intaro\RetailCrm\Component\Json\Mapping;
|
||||
use Intaro\RetailCrm\Model\Api\AbstractApiModel;
|
||||
|
||||
/**
|
||||
* Class CreateResponse
|
||||
*
|
||||
* @package Intaro\RetailCrm\Model\Api
|
||||
*/
|
||||
class CreateResponse extends OperationResponse
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*
|
||||
* @Mapping\Type("int")
|
||||
* @Mapping\SerializedName("id")
|
||||
*/
|
||||
public $id;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* @category Integration
|
||||
* @package Intaro\RetailCrm\Model\Api\Response
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license MIT
|
||||
* @link http://retailcrm.ru
|
||||
* @see http://retailcrm.ru/docs
|
||||
*/
|
||||
namespace Intaro\RetailCrm\Model\Api\Response;
|
||||
|
||||
use Intaro\RetailCrm\Component\Json\Mapping;
|
||||
|
||||
/**
|
||||
* Class CustomerChangeResponse
|
||||
*
|
||||
* @package Intaro\RetailCrm\Model\Api
|
||||
*/
|
||||
class CustomerChangeResponse extends CreateResponse
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @Mapping\Type("string")
|
||||
* @Mapping\SerializedName("state")
|
||||
*/
|
||||
public $state;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* @category Integration
|
||||
* @package Intaro\RetailCrm\Model\Api\Response
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license MIT
|
||||
* @link http://retailcrm.ru
|
||||
* @see http://retailcrm.ru/docs
|
||||
*/
|
||||
namespace Intaro\RetailCrm\Model\Api\Response;
|
||||
|
||||
use Intaro\RetailCrm\Component\Json\Mapping;
|
||||
|
||||
/**
|
||||
* Class CustomerCorporateResponse
|
||||
*
|
||||
* @package Intaro\RetailCrm\Model\Api
|
||||
*/
|
||||
class CustomerCorporateResponse extends OperationResponse
|
||||
{
|
||||
/**
|
||||
* @var \Intaro\RetailCrm\Model\Api\Customer
|
||||
*
|
||||
* @Mapping\Type("int")
|
||||
* @Mapping\SerializedName("customerCorporate")
|
||||
*/
|
||||
public $customerCorporate;
|
||||
}
|
30
intaro.retailcrm/lib/model/api/response/customerresponse.php
Normal file
30
intaro.retailcrm/lib/model/api/response/customerresponse.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* @category Integration
|
||||
* @package Intaro\RetailCrm\Model\Api\Response
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license MIT
|
||||
* @link http://retailcrm.ru
|
||||
* @see http://retailcrm.ru/docs
|
||||
*/
|
||||
namespace Intaro\RetailCrm\Model\Api\Response;
|
||||
|
||||
use Intaro\RetailCrm\Component\Json\Mapping;
|
||||
|
||||
/**
|
||||
* Class CustomerResponse
|
||||
*customerCorporate
|
||||
* @package Intaro\RetailCrm\Model\Api
|
||||
*/
|
||||
class CustomerResponse extends OperationResponse
|
||||
{
|
||||
/**
|
||||
* @var \Intaro\RetailCrm\Model\Api\Customer
|
||||
*
|
||||
* @Mapping\Type("int")
|
||||
* @Mapping\SerializedName("customer")
|
||||
*/
|
||||
public $customer;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* @category Integration
|
||||
* @package Intaro\RetailCrm\Model\Api\Response
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license MIT
|
||||
* @link http://retailcrm.ru
|
||||
* @see http://retailcrm.ru/docs
|
||||
*/
|
||||
namespace Intaro\RetailCrm\Model\Api\Response;
|
||||
|
||||
use Intaro\RetailCrm\Component\Json\Mapping;
|
||||
|
||||
/**
|
||||
* Class CustomersUploadResponse
|
||||
*
|
||||
* @package Intaro\RetailCrm\Model\Api
|
||||
*/
|
||||
class CustomersUploadResponse extends OperationResponse
|
||||
{
|
||||
/**
|
||||
* @var \Intaro\RetailCrm\Model\Api\IdentifiersPair[]
|
||||
*
|
||||
* @Mapping\Type("Intaro\RetailCrm\Model\Api\IdentifiersPair[]")
|
||||
* @Mapping\SerializedName("uploadedCustomers")
|
||||
*/
|
||||
public $uploadedCustomers;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* @category Integration
|
||||
* @package Intaro\RetailCrm\Model\Api\Response
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license MIT
|
||||
* @link http://retailcrm.ru
|
||||
* @see http://retailcrm.ru/docs
|
||||
*/
|
||||
namespace Intaro\RetailCrm\Model\Api\Response;
|
||||
|
||||
use Intaro\RetailCrm\Component\Json\Mapping;
|
||||
use Intaro\RetailCrm\Model\Api\AbstractApiModel;
|
||||
|
||||
/**
|
||||
* Class CreateResponse
|
||||
*
|
||||
* @package Intaro\RetailCrm\Model\Api
|
||||
*/
|
||||
class OperationResponse extends AbstractApiModel
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*
|
||||
* @Mapping\Type("bool")
|
||||
* @Mapping\SerializedName("success")
|
||||
*/
|
||||
public $success;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*
|
||||
* @Mapping\Type("array")
|
||||
* @Mapping\SerializedName("errors")
|
||||
*/
|
||||
public $errors;
|
||||
}
|
38
intaro.retailcrm/lib/model/identifierspair.php
Normal file
38
intaro.retailcrm/lib/model/identifierspair.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* @category Integration
|
||||
* @package Intaro\RetailCrm\Model\Api
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license MIT
|
||||
* @link http://retailcrm.ru
|
||||
* @see http://retailcrm.ru/docs
|
||||
*/
|
||||
namespace Intaro\RetailCrm\Model\Api;
|
||||
|
||||
use Intaro\RetailCrm\Component\Json\Mapping;
|
||||
|
||||
/**
|
||||
* Class IdentifiersPair
|
||||
*
|
||||
* @package Intaro\RetailCrm\Model\Api
|
||||
*/
|
||||
class IdentifiersPair extends AbstractApiModel
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*
|
||||
* @Mapping\Type("int")
|
||||
* @Mapping\SerializedName("id")
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @Mapping\Type("string")
|
||||
* @Mapping\SerializedName("externalId")
|
||||
*/
|
||||
public $externalId;
|
||||
}
|
Loading…
Add table
Reference in a new issue