2017-06-22 00:55:08 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHP version 5.4
|
|
|
|
*
|
2017-06-22 16:42:42 +03:00
|
|
|
* Abstract API client
|
2017-06-22 00:55:08 +03:00
|
|
|
*
|
|
|
|
* @category RetailCrm
|
|
|
|
* @package RetailCrm
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace RetailCrm\Client;
|
|
|
|
|
|
|
|
use RetailCrm\Http\Client;
|
2020-08-21 10:24:55 +03:00
|
|
|
use RetailCrm\Http\RequestOptions;
|
2017-06-22 00:55:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PHP version 5.4
|
|
|
|
*
|
2017-06-22 16:42:42 +03:00
|
|
|
* Abstract API client class
|
2017-06-22 00:55:08 +03:00
|
|
|
*
|
|
|
|
* @category RetailCrm
|
|
|
|
* @package RetailCrm
|
|
|
|
*/
|
|
|
|
abstract class AbstractLoader
|
|
|
|
{
|
2020-02-11 10:57:17 +03:00
|
|
|
/** @var string|null */
|
2017-06-22 00:55:08 +03:00
|
|
|
protected $siteCode;
|
2020-02-11 10:57:17 +03:00
|
|
|
|
|
|
|
/** @var \RetailCrm\Http\Client */
|
2017-06-22 00:55:08 +03:00
|
|
|
protected $client;
|
2020-02-11 10:57:17 +03:00
|
|
|
|
|
|
|
/** @var string */
|
2017-09-27 16:59:56 +03:00
|
|
|
protected $crmUrl;
|
2017-06-22 00:55:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init version based client
|
|
|
|
*
|
|
|
|
* @param string $url api url
|
|
|
|
* @param string $apiKey api key
|
|
|
|
* @param string $version api version
|
|
|
|
* @param string $site site code
|
2019-08-30 14:10:52 +03:00
|
|
|
* @param bool $debug debug mode
|
2017-06-22 00:55:08 +03:00
|
|
|
*/
|
2019-08-30 14:10:52 +03:00
|
|
|
public function __construct($url, $apiKey, $version, $site = null, $debug = false)
|
2017-06-22 00:55:08 +03:00
|
|
|
{
|
|
|
|
if ('/' !== $url[strlen($url) - 1]) {
|
|
|
|
$url .= '/';
|
|
|
|
}
|
2019-08-30 14:10:52 +03:00
|
|
|
|
2017-09-27 16:59:56 +03:00
|
|
|
$this->crmUrl = $url;
|
2017-06-22 00:55:08 +03:00
|
|
|
|
|
|
|
if (empty($version) || !in_array($version, ['v3', 'v4', 'v5'])) {
|
|
|
|
throw new \InvalidArgumentException(
|
2018-01-10 11:35:57 +03:00
|
|
|
'Version must be not empty and must be equal one of v3|v4|v5'
|
2017-06-22 00:55:08 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = $url . 'api/' . $version;
|
|
|
|
|
2019-08-30 14:10:52 +03:00
|
|
|
$this->client = new Client($url, ['apiKey' => $apiKey], $debug);
|
2017-06-22 00:55:08 +03:00
|
|
|
$this->siteCode = $site;
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:24:55 +03:00
|
|
|
/**
|
|
|
|
* Set request options
|
|
|
|
*
|
|
|
|
* @param RequestOptions $options
|
|
|
|
*/
|
|
|
|
public function setOptions(RequestOptions $options)
|
|
|
|
{
|
|
|
|
$this->client->setOptions($options);
|
|
|
|
}
|
|
|
|
|
2017-06-22 00:55:08 +03:00
|
|
|
/**
|
|
|
|
* Set site
|
|
|
|
*
|
|
|
|
* @param string $site site code
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setSite($site)
|
|
|
|
{
|
|
|
|
$this->siteCode = $site;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check ID parameter
|
|
|
|
*
|
|
|
|
* @param string $by identify by
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function checkIdParameter($by)
|
|
|
|
{
|
|
|
|
$allowedForBy = [
|
|
|
|
'externalId',
|
|
|
|
'id'
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!in_array($by, $allowedForBy, false)) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
sprintf(
|
|
|
|
'Value "%s" for "by" param is not valid. Allowed values are %s.',
|
|
|
|
$by,
|
|
|
|
implode(', ', $allowedForBy)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill params by site value
|
|
|
|
*
|
|
|
|
* @param string $site site code
|
|
|
|
* @param array $params input parameters
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function fillSite($site, array $params)
|
|
|
|
{
|
|
|
|
if ($site) {
|
|
|
|
$params['site'] = $site;
|
|
|
|
} elseif ($this->siteCode) {
|
|
|
|
$params['site'] = $this->siteCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return current site
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSite()
|
|
|
|
{
|
|
|
|
return $this->siteCode;
|
|
|
|
}
|
|
|
|
|
2017-09-27 16:59:56 +03:00
|
|
|
/**
|
|
|
|
* Getting the list of available api versions
|
|
|
|
*
|
|
|
|
* @return \RetailCrm\Response\ApiResponse
|
|
|
|
*/
|
|
|
|
public function availableVersions()
|
|
|
|
{
|
|
|
|
return $this->client->makeRequest(
|
|
|
|
$this->crmUrl . 'api/api-versions',
|
|
|
|
"GET",
|
|
|
|
[],
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
2017-06-22 00:55:08 +03:00
|
|
|
|
2017-09-27 16:59:56 +03:00
|
|
|
/**
|
|
|
|
* Getting the list of available api methods and stores for current key
|
|
|
|
*
|
|
|
|
* @return \RetailCrm\Response\ApiResponse
|
|
|
|
*/
|
|
|
|
public function credentials()
|
|
|
|
{
|
|
|
|
return $this->client->makeRequest(
|
|
|
|
$this->crmUrl . 'api/credentials',
|
|
|
|
"GET",
|
|
|
|
[],
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
2017-06-22 00:55:08 +03:00
|
|
|
}
|