Added sites methods, Added site parameter to order/customer methods
This commit is contained in:
parent
1b5af03644
commit
33f34b991e
@ -32,5 +32,5 @@ php: false
|
||||
tree: true
|
||||
deprecated: true
|
||||
todo: true
|
||||
destination: docs/api
|
||||
destination: ../api-client-php.pages/
|
||||
download: false
|
||||
|
@ -20,7 +20,7 @@
|
||||
"sebastian/phpcpd": "2.0.*",
|
||||
"sebastian/phpdcd": "1.0.*",
|
||||
"squizlabs/php_codesniffer": "dev-master",
|
||||
"apigen/apigen": "dev-master"
|
||||
"apigen/apigen": "~4.0@dev"
|
||||
},
|
||||
"support": {
|
||||
"email": "support@intarocrm.ru"
|
||||
|
@ -14,14 +14,20 @@ class ApiClient
|
||||
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* Site code
|
||||
*/
|
||||
protected $siteCode;
|
||||
|
||||
/**
|
||||
* Client creating
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $apiKey
|
||||
* @param string $siteCode
|
||||
* @return mixed
|
||||
*/
|
||||
public function __construct($url, $apiKey)
|
||||
public function __construct($url, $apiKey, $site = null)
|
||||
{
|
||||
if ('/' != substr($url, strlen($url) - 1, 1)) {
|
||||
$url .= '/';
|
||||
@ -30,32 +36,35 @@ class ApiClient
|
||||
$url = $url . 'api/' . self::VERSION;
|
||||
|
||||
$this->client = new Client($url, array('apiKey' => $apiKey));
|
||||
$this->siteCode = $site;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a order
|
||||
*
|
||||
* @param array $order
|
||||
* @param string $site (default: null)
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function ordersCreate(array $order)
|
||||
public function ordersCreate(array $order, $site = null)
|
||||
{
|
||||
if (!sizeof($order)) {
|
||||
throw new \InvalidArgumentException('Parameter `order` must contains a data');
|
||||
}
|
||||
|
||||
return $this->client->makeRequest("/orders/create", Client::METHOD_POST, array(
|
||||
return $this->client->makeRequest("/orders/create", Client::METHOD_POST, $this->fillSite($site, array(
|
||||
'order' => json_encode($order)
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a order
|
||||
*
|
||||
* @param array $order
|
||||
* @param string $site (default: null)
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function ordersEdit(array $order, $by = 'externalId')
|
||||
public function ordersEdit(array $order, $by = 'externalId', $site = null)
|
||||
{
|
||||
if (!sizeof($order)) {
|
||||
throw new \InvalidArgumentException('Parameter `order` must contains a data');
|
||||
@ -67,27 +76,32 @@ class ApiClient
|
||||
throw new \InvalidArgumentException(sprintf('Order array must contain the "%s" parameter.', $by));
|
||||
}
|
||||
|
||||
return $this->client->makeRequest("/orders/" . $order[$by] . "/edit", Client::METHOD_POST, array(
|
||||
'order' => json_encode($order),
|
||||
'by' => $by,
|
||||
));
|
||||
return $this->client->makeRequest(
|
||||
"/orders/" . $order[$by] . "/edit",
|
||||
Client::METHOD_POST,
|
||||
$this->fillSite($site, array(
|
||||
'order' => json_encode($order),
|
||||
'by' => $by,
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload array of the orders
|
||||
*
|
||||
* @param array $orders
|
||||
* @param string $site (default: null)
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function ordersUpload(array $orders)
|
||||
public function ordersUpload(array $orders, $site = null)
|
||||
{
|
||||
if (!sizeof($orders)) {
|
||||
throw new \InvalidArgumentException('Parameter `orders` must contains array of the orders');
|
||||
}
|
||||
|
||||
return $this->client->makeRequest("/orders/upload", Client::METHOD_POST, array(
|
||||
return $this->client->makeRequest("/orders/upload", Client::METHOD_POST, $this->fillSite($site, array(
|
||||
'orders' => json_encode($orders),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,13 +109,16 @@ class ApiClient
|
||||
*
|
||||
* @param string $id
|
||||
* @param string $by (default: 'externalId')
|
||||
* @param string $site (default: null)
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function ordersGet($id, $by = 'externalId')
|
||||
public function ordersGet($id, $by = 'externalId', $site = null)
|
||||
{
|
||||
$this->checkIdParameter($by);
|
||||
|
||||
return $this->client->makeRequest("/orders/$id", Client::METHOD_GET, array('by' => $by));
|
||||
return $this->client->makeRequest("/orders/$id", Client::METHOD_GET, $this->fillSite($site, array(
|
||||
'by' => $by
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -209,26 +226,29 @@ class ApiClient
|
||||
* Create a customer
|
||||
*
|
||||
* @param array $customer
|
||||
* @param string $site (default: null)
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function customersCreate(array $customer)
|
||||
public function customersCreate(array $customer, $site = null)
|
||||
{
|
||||
if (!sizeof($customer)) {
|
||||
throw new \InvalidArgumentException('Parameter `customer` must contains a data');
|
||||
}
|
||||
|
||||
return $this->client->makeRequest("/customers/create", Client::METHOD_POST, array(
|
||||
return $this->client->makeRequest("/customers/create", Client::METHOD_POST, $this->fillSite($site, array(
|
||||
'customer' => json_encode($customer)
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a customer
|
||||
*
|
||||
* @param array $customer
|
||||
* @param string $by (default: 'externalId')
|
||||
* @param string $site (default: null)
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function customersEdit(array $customer, $by = 'externalId')
|
||||
public function customersEdit(array $customer, $by = 'externalId', $site = null)
|
||||
{
|
||||
if (!sizeof($customer)) {
|
||||
throw new \InvalidArgumentException('Parameter `customer` must contains a data');
|
||||
@ -240,9 +260,13 @@ class ApiClient
|
||||
throw new \InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by));
|
||||
}
|
||||
|
||||
return $this->client->makeRequest("/customers/" . $customer[$by] . "/edit", Client::METHOD_POST, array(
|
||||
'customer' => json_encode($customer),
|
||||
'by' => $by,
|
||||
return $this->client->makeRequest(
|
||||
"/customers/" . $customer[$by] . "/edit",
|
||||
Client::METHOD_POST,
|
||||
$this->fillSite($site, array(
|
||||
'customer' => json_encode($customer),
|
||||
'by' => $by,
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
@ -250,17 +274,18 @@ class ApiClient
|
||||
* Upload array of the customers
|
||||
*
|
||||
* @param array $customers
|
||||
* @param string $site (default: null)
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function customersUpload(array $customers)
|
||||
public function customersUpload(array $customers, $site = null)
|
||||
{
|
||||
if (!sizeof($customers)) {
|
||||
throw new \InvalidArgumentException('Parameter `customers` must contains array of the customers');
|
||||
}
|
||||
|
||||
return $this->client->makeRequest("/customers/upload", Client::METHOD_POST, array(
|
||||
return $this->client->makeRequest("/customers/upload", Client::METHOD_POST, $this->fillSite($site, array(
|
||||
'customers' => json_encode($customers),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -268,13 +293,16 @@ class ApiClient
|
||||
*
|
||||
* @param string $id
|
||||
* @param string $by (default: 'externalId')
|
||||
* @param string $site (default: null)
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function customersGet($id, $by = 'externalId')
|
||||
public function customersGet($id, $by = 'externalId', $site = null)
|
||||
{
|
||||
$this->checkIdParameter($by);
|
||||
|
||||
return $this->client->makeRequest("/customers/$id", Client::METHOD_GET, array('by' => $by));
|
||||
return $this->client->makeRequest("/customers/$id", Client::METHOD_GET, $this->fillSite($site, array(
|
||||
'by' => $by
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -409,6 +437,16 @@ class ApiClient
|
||||
return $this->client->makeRequest('/reference/statuses', Client::METHOD_GET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns sites list
|
||||
*
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function sitesList()
|
||||
{
|
||||
return $this->client->makeRequest('/reference/sites', Client::METHOD_GET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit deliveryService
|
||||
*
|
||||
@ -577,6 +615,27 @@ class ApiClient
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit site
|
||||
*
|
||||
* @param array $data site data
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function sitesEdit(array $data)
|
||||
{
|
||||
if (!isset($data['code'])) {
|
||||
throw new \InvalidArgumentException('Data must contain "code" parameter.');
|
||||
}
|
||||
|
||||
return $this->client->makeRequest(
|
||||
'/reference/sites/' . $data['code'] . '/edit',
|
||||
Client::METHOD_POST,
|
||||
array(
|
||||
'site' => json_encode($data)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update CRM basic statistic
|
||||
*
|
||||
@ -587,6 +646,27 @@ class ApiClient
|
||||
return $this->client->makeRequest('/statistic/update', Client::METHOD_GET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return current site
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSite()
|
||||
{
|
||||
return $this->siteCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set site
|
||||
*
|
||||
* @param string $site
|
||||
* @return void
|
||||
*/
|
||||
public function setSite($site)
|
||||
{
|
||||
$this->siteCode = $site;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check ID parameter
|
||||
*
|
||||
@ -606,4 +686,22 @@ class ApiClient
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill params by site value
|
||||
*
|
||||
* @param string $site
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
protected function fillSite($site, array $params)
|
||||
{
|
||||
if ($site) {
|
||||
$params['site'] = $site;
|
||||
} elseif ($this->siteCode) {
|
||||
$params['site'] = $this->siteCode;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
<php>
|
||||
<server name="CRM_URL" value="foo" />
|
||||
<server name="CRM_API_KEY" value="bar" />
|
||||
<server name="CRM_SITE" value="zoo" />
|
||||
</php>
|
||||
|
||||
<testsuites>
|
||||
|
@ -11,10 +11,15 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
*
|
||||
* @param string $url (default: null)
|
||||
* @param string $apiKey (default: null)
|
||||
* @param string $site (default: null)
|
||||
* @return ApiClient
|
||||
*/
|
||||
public static function getApiClient($url = null, $apiKey = null)
|
||||
public static function getApiClient($url = null, $apiKey = null, $site = null)
|
||||
{
|
||||
return new ApiClient($url ?: $_SERVER['CRM_URL'], $apiKey ?: $_SERVER['CRM_API_KEY']);
|
||||
return new ApiClient(
|
||||
$url ?: $_SERVER['CRM_URL'],
|
||||
$apiKey ?: $_SERVER['CRM_API_KEY'],
|
||||
$site ?: (isset($_SERVER['CRM_SITE']) ? $_SERVER['CRM_SITE'] : null)
|
||||
);
|
||||
}
|
||||
}
|
@ -67,6 +67,37 @@ class ApiClientReferenceTest extends TestCase
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group integration
|
||||
* @group site
|
||||
*/
|
||||
public function testSiteEditing()
|
||||
{
|
||||
$name = 'sites';
|
||||
$client = static::getApiClient();
|
||||
|
||||
$code = 'dict-' . strtolower($name) . '-' . time();
|
||||
$method = $name . 'Edit';
|
||||
$params = array(
|
||||
'code' => $code,
|
||||
'name' => 'Aaa',
|
||||
);
|
||||
|
||||
$response = $client->$method($params);
|
||||
$this->assertEquals(400, $response->getStatusCode());
|
||||
|
||||
if ($code = $client->getSite()) {
|
||||
$method = $name . 'Edit';
|
||||
$params = array(
|
||||
'code' => $code,
|
||||
'name' => 'Aaa' . time(),
|
||||
);
|
||||
|
||||
$response = $client->$method($params);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
}
|
||||
|
||||
public function getListDictionaries()
|
||||
{
|
||||
return array(
|
||||
@ -79,6 +110,7 @@ class ApiClientReferenceTest extends TestCase
|
||||
array('productStatuses'),
|
||||
array('statusGroups'),
|
||||
array('statuses'),
|
||||
array('sites'),
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user