models for request serialization, tests placeholder, psr-4 autoload
This commit is contained in:
parent
85f5afaec4
commit
ceead80f75
15 changed files with 360 additions and 580 deletions
300
composer.lock
generated
300
composer.lock
generated
|
@ -1608,57 +1608,6 @@
|
|||
],
|
||||
"time": "2014-09-09T13:34:57+00:00"
|
||||
},
|
||||
{
|
||||
"name": "jean85/pretty-package-versions",
|
||||
"version": "1.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/Jean85/pretty-package-versions.git",
|
||||
"reference": "75c7effcf3f77501d0e0caa75111aff4daa0dd48"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/Jean85/pretty-package-versions/zipball/75c7effcf3f77501d0e0caa75111aff4daa0dd48",
|
||||
"reference": "75c7effcf3f77501d0e0caa75111aff4daa0dd48",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ocramius/package-versions": "^1.2.0",
|
||||
"php": "^7.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^6.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Jean85\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Alessandro Lai",
|
||||
"email": "alessandro.lai85@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "A wrapper for ocramius/package-versions to get pretty versions strings",
|
||||
"keywords": [
|
||||
"composer",
|
||||
"package",
|
||||
"release",
|
||||
"versions"
|
||||
],
|
||||
"time": "2018-06-13T13:22:40+00:00"
|
||||
},
|
||||
{
|
||||
"name": "jms/metadata",
|
||||
"version": "1.7.0",
|
||||
|
@ -1833,6 +1782,157 @@
|
|||
],
|
||||
"time": "2018-07-25T13:58:54+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpcollection/phpcollection",
|
||||
"version": "0.5.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/schmittjoh/php-collection.git",
|
||||
"reference": "f2bcff45c0da7c27991bbc1f90f47c4b7fb434a6"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/schmittjoh/php-collection/zipball/f2bcff45c0da7c27991bbc1f90f47c4b7fb434a6",
|
||||
"reference": "f2bcff45c0da7c27991bbc1f90f47c4b7fb434a6",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"phpoption/phpoption": "1.*"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "0.4-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"PhpCollection": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"Apache2"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Johannes M. Schmitt",
|
||||
"email": "schmittjoh@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "General-Purpose Collection Library for PHP",
|
||||
"keywords": [
|
||||
"collection",
|
||||
"list",
|
||||
"map",
|
||||
"sequence",
|
||||
"set"
|
||||
],
|
||||
"time": "2015-05-17T12:39:23+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpoption/phpoption",
|
||||
"version": "1.5.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/schmittjoh/php-option.git",
|
||||
"reference": "94e644f7d2051a5f0fcf77d81605f152eecff0ed"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/schmittjoh/php-option/zipball/94e644f7d2051a5f0fcf77d81605f152eecff0ed",
|
||||
"reference": "94e644f7d2051a5f0fcf77d81605f152eecff0ed",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "4.7.*"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.3-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"PhpOption\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"Apache2"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Johannes M. Schmitt",
|
||||
"email": "schmittjoh@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "Option Type for PHP",
|
||||
"keywords": [
|
||||
"language",
|
||||
"option",
|
||||
"php",
|
||||
"type"
|
||||
],
|
||||
"time": "2015-07-25T16:39:46+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [
|
||||
{
|
||||
"name": "jean85/pretty-package-versions",
|
||||
"version": "1.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/Jean85/pretty-package-versions.git",
|
||||
"reference": "75c7effcf3f77501d0e0caa75111aff4daa0dd48"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/Jean85/pretty-package-versions/zipball/75c7effcf3f77501d0e0caa75111aff4daa0dd48",
|
||||
"reference": "75c7effcf3f77501d0e0caa75111aff4daa0dd48",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ocramius/package-versions": "^1.2.0",
|
||||
"php": "^7.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^6.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Jean85\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Alessandro Lai",
|
||||
"email": "alessandro.lai85@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "A wrapper for ocramius/package-versions to get pretty versions strings",
|
||||
"keywords": [
|
||||
"composer",
|
||||
"package",
|
||||
"release",
|
||||
"versions"
|
||||
],
|
||||
"time": "2018-06-13T13:22:40+00:00"
|
||||
},
|
||||
{
|
||||
"name": "myclabs/deep-copy",
|
||||
"version": "1.6.1",
|
||||
|
@ -2690,54 +2790,6 @@
|
|||
],
|
||||
"time": "2018-02-15T16:58:55+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpcollection/phpcollection",
|
||||
"version": "0.5.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/schmittjoh/php-collection.git",
|
||||
"reference": "f2bcff45c0da7c27991bbc1f90f47c4b7fb434a6"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/schmittjoh/php-collection/zipball/f2bcff45c0da7c27991bbc1f90f47c4b7fb434a6",
|
||||
"reference": "f2bcff45c0da7c27991bbc1f90f47c4b7fb434a6",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"phpoption/phpoption": "1.*"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "0.4-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"PhpCollection": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"Apache2"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Johannes M. Schmitt",
|
||||
"email": "schmittjoh@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "General-Purpose Collection Library for PHP",
|
||||
"keywords": [
|
||||
"collection",
|
||||
"list",
|
||||
"map",
|
||||
"sequence",
|
||||
"set"
|
||||
],
|
||||
"time": "2015-05-17T12:39:23+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpdocumentor/reflection-common",
|
||||
"version": "1.0.1",
|
||||
|
@ -2956,56 +3008,6 @@
|
|||
],
|
||||
"time": "2017-01-20T14:41:10+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpoption/phpoption",
|
||||
"version": "1.5.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/schmittjoh/php-option.git",
|
||||
"reference": "94e644f7d2051a5f0fcf77d81605f152eecff0ed"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/schmittjoh/php-option/zipball/94e644f7d2051a5f0fcf77d81605f152eecff0ed",
|
||||
"reference": "94e644f7d2051a5f0fcf77d81605f152eecff0ed",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "4.7.*"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.3-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"PhpOption\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"Apache2"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Johannes M. Schmitt",
|
||||
"email": "schmittjoh@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "Option Type for PHP",
|
||||
"keywords": [
|
||||
"language",
|
||||
"option",
|
||||
"php",
|
||||
"type"
|
||||
],
|
||||
"time": "2015-07-25T16:39:46+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpspec/prophecy",
|
||||
"version": "1.8.1",
|
||||
|
|
|
@ -3,27 +3,27 @@
|
|||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* RequestHelper
|
||||
* Request Helper
|
||||
*
|
||||
* @package RetailCrm\Helper
|
||||
* @package RetailCrm\Mg\Bot
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Helper;
|
||||
namespace RetailCrm\Mg\Bot;
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* RequestHelper class
|
||||
*
|
||||
* @package RetailCrm\Helper
|
||||
* @package RetailCrm\Mg\Bot
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class RequestHelper
|
||||
class IntegrationHelper
|
||||
{
|
||||
/**
|
||||
* Prepare module data
|
|
@ -5,17 +5,17 @@
|
|||
*
|
||||
* Request
|
||||
*
|
||||
* @package RetailCrm\Bot
|
||||
* @package RetailCrm\Mg\Bot
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Bot;
|
||||
namespace RetailCrm\Mg\Bot;
|
||||
|
||||
use RetailCrm\Bot\Exception\CurlException;
|
||||
use RetailCrm\Bot\Exception\InvalidJsonException;
|
||||
use RetailCrm\Bot\Exception\LimitException;
|
||||
use JMS\Serializer\SerializerBuilder;
|
||||
use RetailCrm\Common\Exception\CurlException;
|
||||
use RetailCrm\Common\Exception\LimitException;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
|
||||
|
@ -24,7 +24,7 @@ use InvalidArgumentException;
|
|||
*
|
||||
* Request class
|
||||
*
|
||||
* @package RetailCrm\Bot
|
||||
* @package RetailCrm\Mg\Bot
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
|
@ -36,12 +36,13 @@ class Request
|
|||
const METHOD_PUT = 'PUT';
|
||||
const METHOD_DELETE = 'DELETE';
|
||||
|
||||
const SERIALIZE_ARRAY = 0;
|
||||
const SERIALIZE_JSON = 1;
|
||||
const S_ARRAY = 0;
|
||||
const S_JSON = 1;
|
||||
|
||||
protected $url;
|
||||
protected $token;
|
||||
private $debug;
|
||||
private $allowedMethods;
|
||||
|
||||
/**
|
||||
* Client constructor.
|
||||
|
@ -59,44 +60,26 @@ class Request
|
|||
$this->url = $url;
|
||||
$this->token = $token;
|
||||
$this->debug = $debug;
|
||||
$this->allowedMethods = [self::METHOD_GET, self::METHOD_POST, self::METHOD_PUT, self::METHOD_DELETE];
|
||||
}
|
||||
|
||||
/**
|
||||
* Make HTTP request
|
||||
*
|
||||
* @param string $path request url
|
||||
* @param string $method (default: 'GET')
|
||||
* @param array $parameters (default: array())
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws \Exception
|
||||
* @throws CurlException
|
||||
* @throws InvalidJsonException
|
||||
* @param string $path request url
|
||||
* @param string $method (default: 'GET')
|
||||
* @param mixed $request (default: null)
|
||||
* @param int $serializeTo
|
||||
*
|
||||
* @return Response
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function makeRequest(
|
||||
$path,
|
||||
$method,
|
||||
array $parameters = []
|
||||
) {
|
||||
$allowedMethods = [self::METHOD_GET, self::METHOD_POST, self::METHOD_PUT, self::METHOD_DELETE];
|
||||
public function makeRequest($path, $method, $request = null, $serializeTo = self::S_JSON)
|
||||
{
|
||||
$this->validateMethod($method);
|
||||
|
||||
if (!in_array($method, $allowedMethods, false)) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf(
|
||||
'Method "%s" is not valid. Allowed methods are %s',
|
||||
$method,
|
||||
implode(', ', $allowedMethods)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$url = $this->url . $path;
|
||||
|
||||
if (self::METHOD_GET === $method && count($parameters)) {
|
||||
$url .= '?' . http_build_query($parameters, '', '&');
|
||||
}
|
||||
$parameters = $this->serialize($request, $serializeTo);
|
||||
$url = $this->buildUrl($path, $method, $parameters);
|
||||
|
||||
$curlHandler = curl_init();
|
||||
curl_setopt($curlHandler, CURLOPT_URL, $url);
|
||||
|
@ -116,7 +99,7 @@ class Request
|
|||
|
||||
if (in_array($method, [self::METHOD_POST, self::METHOD_PUT, self::METHOD_DELETE])) {
|
||||
curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, $method);
|
||||
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, json_encode($parameters));
|
||||
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters);
|
||||
}
|
||||
|
||||
$responseBody = curl_exec($curlHandler);
|
||||
|
@ -149,4 +132,83 @@ class Request
|
|||
|
||||
return new Response($statusCode, $responseBody);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check trailing slash into url
|
||||
*
|
||||
* @param string $url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function normalizeUrl($url)
|
||||
{
|
||||
if ('/' !== $url[strlen($url) - 1]) {
|
||||
$url .= '/';
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate HTTP method
|
||||
*
|
||||
* @param string $method
|
||||
*/
|
||||
private function validateMethod($method)
|
||||
{
|
||||
if (!in_array($method, $this->allowedMethods, false)) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf(
|
||||
'Method "%s" is not valid. Allowed methods are %s',
|
||||
$method,
|
||||
implode(', ', $this->allowedMethods)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize given object to JSON or Array
|
||||
*
|
||||
* @param object $request
|
||||
* @param int $serialize
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
private function serialize($request, $serialize)
|
||||
{
|
||||
$serialized = null;
|
||||
|
||||
switch ($serialize) {
|
||||
case self::S_ARRAY:
|
||||
$serialized = (array)$request;
|
||||
break;
|
||||
case self::S_JSON:
|
||||
$serializer = SerializerBuilder::create()->build();
|
||||
$serialized = $serializer->serialize($request, 'json');
|
||||
}
|
||||
|
||||
return $serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build request url
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function buildUrl($path, $method, $parameters)
|
||||
{
|
||||
$url = $this->url . $path;
|
||||
|
||||
if (self::METHOD_GET === $method && count($parameters)) {
|
||||
$queryString = http_build_query($parameters, '', '&');
|
||||
$url = sprintf("%s?%s", $url, $queryString);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
|
@ -5,29 +5,30 @@
|
|||
*
|
||||
* Request
|
||||
*
|
||||
* @package RetailCrm\Bot
|
||||
* @package RetailCrm\Mg\Bot
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Bot;
|
||||
namespace RetailCrm\Mg\Bot;
|
||||
|
||||
use ArrayAccess;
|
||||
use BadMethodCallException;
|
||||
use InvalidArgumentException;
|
||||
use RetailCrm\Bot\Exception\InvalidJsonException;
|
||||
use RetailCrm\Common\Exception\InvalidJsonException;
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Request class
|
||||
*
|
||||
* @package RetailCrm\Bot
|
||||
* @package RetailCrm\Mg\Bot
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class Response implements \ArrayAccess
|
||||
class Response implements ArrayAccess
|
||||
{
|
||||
// HTTP response status code
|
||||
protected $statusCode;
|
||||
|
@ -48,7 +49,7 @@ class Response implements \ArrayAccess
|
|||
*/
|
||||
public function __construct($statusCode, $responseBody = null)
|
||||
{
|
||||
$this->statusCode = (int) $statusCode;
|
||||
$this->statusCode = $statusCode;
|
||||
$this->raw = $responseBody;
|
||||
$this->response = self::parseJSON($responseBody);
|
||||
}
|
|
@ -5,13 +5,13 @@
|
|||
*
|
||||
* CurlException
|
||||
*
|
||||
* @package RetailCrm\Bot\Exception
|
||||
* @package RetailCrm\Common\Exception
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Bot\Exception;
|
||||
namespace RetailCrm\Common\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
|
@ -20,7 +20,7 @@ use RuntimeException;
|
|||
*
|
||||
* Class CurlException
|
||||
*
|
||||
* @package RetailCrm\Bot\Exception
|
||||
* @package RetailCrm\Common\Exception
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
|
@ -18,7 +18,7 @@ use DomainException;
|
|||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Class LimitException
|
||||
* Class CurlException
|
||||
*
|
||||
* @package RetailCrm\Common\Exception
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
|
|
|
@ -1,121 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Client
|
||||
*
|
||||
* @package RetailCrm\Bot
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Bot;
|
||||
|
||||
use RetailCrm\Bot\Exception\CurlException;
|
||||
use RetailCrm\Bot\Exception\InvalidJsonException;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Client class
|
||||
*
|
||||
* @package RetailCrm\Bot
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class Client
|
||||
{
|
||||
const VERSION = 'v1';
|
||||
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* Init
|
||||
*
|
||||
* @param string $url api url
|
||||
* @param string $token api key
|
||||
* @param bool $debug debug flag
|
||||
*/
|
||||
public function __construct($url, $token, $debug = false)
|
||||
{
|
||||
if ('/' !== $url[strlen($url) - 1]) {
|
||||
$url .= '/';
|
||||
}
|
||||
|
||||
$url = $url . 'api/bot/' . self::VERSION;
|
||||
$this->client = new Request($url, $token, $debug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns filtered bots list
|
||||
*
|
||||
* @param array $parameters (default: array())
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws CurlException
|
||||
* @throws InvalidJsonException
|
||||
* @throws Exception
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function bots(array $parameters = [])
|
||||
{
|
||||
return $this->client->makeRequest('/bots', 'GET', $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns filtered channels list
|
||||
*
|
||||
* @param array $parameters (default: array())
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws CurlException
|
||||
* @throws InvalidJsonException
|
||||
* @throws Exception
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function channels(array $parameters = [])
|
||||
{
|
||||
return $this->client->makeRequest('/channels', 'GET', $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns filtered chats list
|
||||
*
|
||||
* @param array $parameters (default: array())
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws CurlException
|
||||
* @throws InvalidJsonException
|
||||
* @throws Exception
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function chats(array $parameters = [])
|
||||
{
|
||||
return $this->client->makeRequest('/chats', 'GET', $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns filtered customers list
|
||||
*
|
||||
* @param array $parameters (default: array())
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws CurlException
|
||||
* @throws InvalidJsonException
|
||||
* @throws Exception
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function customers(array $parameters = [])
|
||||
{
|
||||
return $this->client->makeRequest('/customers', 'GET', $parameters);
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* CurlException
|
||||
*
|
||||
* @package RetailCrm\Bot\Exception
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Bot\Exception;
|
||||
|
||||
use DomainException;
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Class InvalidJsonException
|
||||
*
|
||||
* @package RetailCrm\Bot\Exception
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class InvalidJsonException extends DomainException
|
||||
{
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* CurlException
|
||||
*
|
||||
* @package RetailCrm\Bot\Exception
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Bot\Exception;
|
||||
|
||||
use DomainException;
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Class CurlException
|
||||
*
|
||||
* @package RetailCrm\Bot\Exception
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class LimitException extends DomainException
|
||||
{
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Channels Request
|
||||
*
|
||||
* @package RetailCrm\RetailCrm\Bot\Model\Request
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\RetailCrm\Bot\Model\Request;
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* ChannelsRequest class
|
||||
*
|
||||
* @package RetailCrm\RetailCrm\Bot\Model\Request
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class ChannelsRequest
|
||||
{
|
||||
public $id;
|
||||
|
||||
public $types;
|
||||
|
||||
public $active;
|
||||
|
||||
public $since;
|
||||
|
||||
public $until;
|
||||
}
|
|
@ -14,9 +14,6 @@
|
|||
namespace RetailCrm\Mg\Bot\Test;
|
||||
|
||||
use PHPUnit\Framework\TestCase as BaseCase;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use RetailCrm\Mg\Bot\Client;
|
||||
|
||||
/**
|
||||
|
@ -31,120 +28,27 @@ use RetailCrm\Mg\Bot\Client;
|
|||
class TestCase extends BaseCase
|
||||
{
|
||||
/**
|
||||
* Return bot api client object
|
||||
* Return ApiClient object
|
||||
*
|
||||
* @param string $url (default: null)
|
||||
* @param string $key (default: null)
|
||||
* @param bool $debug (default: false)
|
||||
* @param array $response (default: null)
|
||||
* @param string $url (default: null)
|
||||
* @param string $key (default: null)
|
||||
* @param bool $debug (default: false)
|
||||
*
|
||||
* @return Client
|
||||
*/
|
||||
public static function getApiClient(
|
||||
$url = null,
|
||||
$key = null,
|
||||
$debug = false,
|
||||
...$response
|
||||
$debug = false
|
||||
) {
|
||||
$configUrl = getenv('MG_BOT_URL');
|
||||
$configKey = getenv('MG_BOT_KEY');
|
||||
$configDbg = getenv('MG_BOT_DBG');
|
||||
$mock = new MockHandler($response ?: []);
|
||||
$configUrl = getenv('MG_BOT_URL') ?: $_SERVER['MG_BOT_URL'];
|
||||
$configKey = getenv('MG_BOT_KEY') ?: $_SERVER['MG_BOT_KEY'];
|
||||
$configDbg = getenv('MG_BOT_DBG') ?: $_SERVER['MG_BOT_DBG'];
|
||||
|
||||
return new Client(
|
||||
$url ?: $configUrl,
|
||||
$key ?: $configKey,
|
||||
$debug ?: $configDbg,
|
||||
empty($response) ? null : HandlerStack::create($mock)
|
||||
$debug ?: $configDbg
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns mocked GuzzleHttp response
|
||||
*
|
||||
* @param string|null $body
|
||||
* @param int $statusCode
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function getResponse(string $body = null, int $statusCode = 200)
|
||||
{
|
||||
return new Response(
|
||||
$statusCode,
|
||||
array_filter(
|
||||
[
|
||||
'Server' => 'openresty/1.13.6.2',
|
||||
'Date' => gmdate("D, d M Y H:m:s \G\M\T"),
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'Content-Length' => is_null($body) ? null : strlen($body),
|
||||
'Connection' => 'keep-alive',
|
||||
'Access-Control-Allow-Credentials' => 'true',
|
||||
'Access-Control-Allow-Headers' => 'X-Requested-With, Content-Type, X-Api-Key, X-Client-Token',
|
||||
'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS'
|
||||
]
|
||||
),
|
||||
$body
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate and return mocked response.
|
||||
* Response data should be stored in Resources directory as json file.
|
||||
* Only file name (without extension or any other data) should be provided,
|
||||
* e.g. `getJsonResponse('bots', 200)`
|
||||
*
|
||||
* @param string $jsonFile
|
||||
* @param int $statusCode
|
||||
*
|
||||
* @return Response|null
|
||||
*/
|
||||
public function getJsonResponse(string $jsonFile, int $statusCode = 200)
|
||||
{
|
||||
$fileName = realpath(
|
||||
join(
|
||||
DIRECTORY_SEPARATOR,
|
||||
[__DIR__, '..', '..', 'Resources', \sprintf('%s.json', $jsonFile)]
|
||||
)
|
||||
);
|
||||
|
||||
if (file_exists($fileName)) {
|
||||
$json = file_get_contents($fileName);
|
||||
json_decode($json, true);
|
||||
|
||||
if (json_last_error() == JSON_ERROR_NONE) {
|
||||
return $this->getResponse($json, $statusCode);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $statusCode
|
||||
* @param array ...$errors
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function getErrorsResponse(int $statusCode = 400, ...$errors)
|
||||
{
|
||||
$json = ['errors' => []];
|
||||
|
||||
foreach ($errors as $error) {
|
||||
$json['errors'][] = is_string($error) ? $error : null;
|
||||
}
|
||||
|
||||
return $this->getResponse(json_encode(array_filter($json)), $statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate and return empty response
|
||||
*
|
||||
* @param int $statusCode
|
||||
*
|
||||
* @return Response|null
|
||||
*/
|
||||
public function getEmptyResponse(int $statusCode = 200)
|
||||
{
|
||||
return $this->getResponse(null, $statusCode);
|
||||
}
|
||||
}
|
||||
|
|
0
tests/Bot/Tests/.gitkeep
Normal file
0
tests/Bot/Tests/.gitkeep
Normal file
84
tests/Bot/Tests/ClientTest.php
Normal file
84
tests/Bot/Tests/ClientTest.php
Normal file
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Client Test
|
||||
*
|
||||
* @package RetailCrm\Mg\Bot\Tests
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Mg\Bot\Tests;
|
||||
|
||||
use RetailCrm\Mg\Bot\Client;
|
||||
use RetailCrm\Mg\Bot\Test\TestCase;
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Class ClientTest
|
||||
*
|
||||
* @package RetailCrm\Mg\Bot\Tests
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class ClientTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @group("list")
|
||||
*/
|
||||
public function testChannels()
|
||||
{
|
||||
self::assertTrue(1 == 1);
|
||||
}
|
||||
|
||||
public function testChats()
|
||||
{
|
||||
self::assertTrue(1 == 1);
|
||||
}
|
||||
|
||||
public function testMembers()
|
||||
{
|
||||
self::assertTrue(1 == 1);
|
||||
}
|
||||
|
||||
public function testMessages()
|
||||
{
|
||||
self::assertTrue(1 == 1);
|
||||
}
|
||||
|
||||
public function testCommands()
|
||||
{
|
||||
self::assertTrue(1 == 1);
|
||||
}
|
||||
|
||||
public function testBots()
|
||||
{
|
||||
self::assertTrue(1 == 1);
|
||||
}
|
||||
|
||||
public function testUsers()
|
||||
{
|
||||
self::assertTrue(1 == 1);
|
||||
}
|
||||
|
||||
public function testDialogs()
|
||||
{
|
||||
self::assertTrue(1 == 1);
|
||||
}
|
||||
|
||||
public function test__construct()
|
||||
{
|
||||
self::assertTrue(1 == 1);
|
||||
}
|
||||
|
||||
public function testCustomers()
|
||||
{
|
||||
self::assertTrue(1 == 1);
|
||||
}
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHP version 5.4
|
||||
*
|
||||
* Test case class
|
||||
*
|
||||
* @category RetailCrm
|
||||
* @package Test
|
||||
* @author RetailCrm <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion5
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Test;
|
||||
|
||||
use PHPUnit\Framework\TestCase as BaseCase;
|
||||
use RetailCrm\Bot\Client;
|
||||
|
||||
/**
|
||||
* Class TestCase
|
||||
*
|
||||
* @category RetailCrm
|
||||
* @package Test
|
||||
* @author RetailCrm <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion5
|
||||
*/
|
||||
class TestCase extends BaseCase
|
||||
{
|
||||
/**
|
||||
* Return ApiClient object
|
||||
*
|
||||
* @param string $url (default: null)
|
||||
* @param string $key (default: null)
|
||||
* @param bool $debug (default: false)
|
||||
*
|
||||
* @return Client
|
||||
*/
|
||||
public static function getApiClient(
|
||||
$url = null,
|
||||
$key = null,
|
||||
$debug = false
|
||||
) {
|
||||
$configUrl = getenv('MG_BOT_URL') ?: $_SERVER['MG_BOT_URL'];
|
||||
$configKey = getenv('MG_BOT_KEY') ?: $_SERVER['MG_BOT_KEY'];
|
||||
$configDbg = getenv('MG_BOT_DBG') ?: $_SERVER['MG_BOT_DBG'];
|
||||
|
||||
return new Client(
|
||||
$url ?: $configUrl,
|
||||
$key ?: $configKey,
|
||||
$debug ?: $configDbg
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue