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

Added sites methods, Added site parameter to order/customer methods

This commit is contained in:
Ilyas Salikhov 2014-11-27 11:23:44 +03:00
parent 1b5af03644
commit 33f34b991e
6 changed files with 166 additions and 30 deletions

View File

@ -32,5 +32,5 @@ php: false
tree: true tree: true
deprecated: true deprecated: true
todo: true todo: true
destination: docs/api destination: ../api-client-php.pages/
download: false download: false

View File

@ -20,7 +20,7 @@
"sebastian/phpcpd": "2.0.*", "sebastian/phpcpd": "2.0.*",
"sebastian/phpdcd": "1.0.*", "sebastian/phpdcd": "1.0.*",
"squizlabs/php_codesniffer": "dev-master", "squizlabs/php_codesniffer": "dev-master",
"apigen/apigen": "dev-master" "apigen/apigen": "~4.0@dev"
}, },
"support": { "support": {
"email": "support@intarocrm.ru" "email": "support@intarocrm.ru"

View File

@ -14,14 +14,20 @@ class ApiClient
protected $client; protected $client;
/**
* Site code
*/
protected $siteCode;
/** /**
* Client creating * Client creating
* *
* @param string $url * @param string $url
* @param string $apiKey * @param string $apiKey
* @param string $siteCode
* @return mixed * @return mixed
*/ */
public function __construct($url, $apiKey) public function __construct($url, $apiKey, $site = null)
{ {
if ('/' != substr($url, strlen($url) - 1, 1)) { if ('/' != substr($url, strlen($url) - 1, 1)) {
$url .= '/'; $url .= '/';
@ -30,32 +36,35 @@ class ApiClient
$url = $url . 'api/' . self::VERSION; $url = $url . 'api/' . self::VERSION;
$this->client = new Client($url, array('apiKey' => $apiKey)); $this->client = new Client($url, array('apiKey' => $apiKey));
$this->siteCode = $site;
} }
/** /**
* Create a order * Create a order
* *
* @param array $order * @param array $order
* @param string $site (default: null)
* @return ApiResponse * @return ApiResponse
*/ */
public function ordersCreate(array $order) public function ordersCreate(array $order, $site = null)
{ {
if (!sizeof($order)) { if (!sizeof($order)) {
throw new \InvalidArgumentException('Parameter `order` must contains a data'); 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) 'order' => json_encode($order)
)); )));
} }
/** /**
* Edit a order * Edit a order
* *
* @param array $order * @param array $order
* @param string $site (default: null)
* @return ApiResponse * @return ApiResponse
*/ */
public function ordersEdit(array $order, $by = 'externalId') public function ordersEdit(array $order, $by = 'externalId', $site = null)
{ {
if (!sizeof($order)) { if (!sizeof($order)) {
throw new \InvalidArgumentException('Parameter `order` must contains a data'); 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)); throw new \InvalidArgumentException(sprintf('Order array must contain the "%s" parameter.', $by));
} }
return $this->client->makeRequest("/orders/" . $order[$by] . "/edit", Client::METHOD_POST, array( return $this->client->makeRequest(
'order' => json_encode($order), "/orders/" . $order[$by] . "/edit",
'by' => $by, Client::METHOD_POST,
)); $this->fillSite($site, array(
'order' => json_encode($order),
'by' => $by,
))
);
} }
/** /**
* Upload array of the orders * Upload array of the orders
* *
* @param array $orders * @param array $orders
* @param string $site (default: null)
* @return ApiResponse * @return ApiResponse
*/ */
public function ordersUpload(array $orders) public function ordersUpload(array $orders, $site = null)
{ {
if (!sizeof($orders)) { if (!sizeof($orders)) {
throw new \InvalidArgumentException('Parameter `orders` must contains array of the 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), 'orders' => json_encode($orders),
)); )));
} }
/** /**
@ -95,13 +109,16 @@ class ApiClient
* *
* @param string $id * @param string $id
* @param string $by (default: 'externalId') * @param string $by (default: 'externalId')
* @param string $site (default: null)
* @return ApiResponse * @return ApiResponse
*/ */
public function ordersGet($id, $by = 'externalId') public function ordersGet($id, $by = 'externalId', $site = null)
{ {
$this->checkIdParameter($by); $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 * Create a customer
* *
* @param array $customer * @param array $customer
* @param string $site (default: null)
* @return ApiResponse * @return ApiResponse
*/ */
public function customersCreate(array $customer) public function customersCreate(array $customer, $site = null)
{ {
if (!sizeof($customer)) { if (!sizeof($customer)) {
throw new \InvalidArgumentException('Parameter `customer` must contains a data'); 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) 'customer' => json_encode($customer)
)); )));
} }
/** /**
* Edit a customer * Edit a customer
* *
* @param array $customer * @param array $customer
* @param string $by (default: 'externalId')
* @param string $site (default: null)
* @return ApiResponse * @return ApiResponse
*/ */
public function customersEdit(array $customer, $by = 'externalId') public function customersEdit(array $customer, $by = 'externalId', $site = null)
{ {
if (!sizeof($customer)) { if (!sizeof($customer)) {
throw new \InvalidArgumentException('Parameter `customer` must contains a data'); 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)); throw new \InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by));
} }
return $this->client->makeRequest("/customers/" . $customer[$by] . "/edit", Client::METHOD_POST, array( return $this->client->makeRequest(
'customer' => json_encode($customer), "/customers/" . $customer[$by] . "/edit",
'by' => $by, Client::METHOD_POST,
$this->fillSite($site, array(
'customer' => json_encode($customer),
'by' => $by,
)
)); ));
} }
@ -250,17 +274,18 @@ class ApiClient
* Upload array of the customers * Upload array of the customers
* *
* @param array $customers * @param array $customers
* @param string $site (default: null)
* @return ApiResponse * @return ApiResponse
*/ */
public function customersUpload(array $customers) public function customersUpload(array $customers, $site = null)
{ {
if (!sizeof($customers)) { if (!sizeof($customers)) {
throw new \InvalidArgumentException('Parameter `customers` must contains array of the 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), 'customers' => json_encode($customers),
)); )));
} }
/** /**
@ -268,13 +293,16 @@ class ApiClient
* *
* @param string $id * @param string $id
* @param string $by (default: 'externalId') * @param string $by (default: 'externalId')
* @param string $site (default: null)
* @return ApiResponse * @return ApiResponse
*/ */
public function customersGet($id, $by = 'externalId') public function customersGet($id, $by = 'externalId', $site = null)
{ {
$this->checkIdParameter($by); $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); 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 * 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 * Update CRM basic statistic
* *
@ -587,6 +646,27 @@ class ApiClient
return $this->client->makeRequest('/statistic/update', Client::METHOD_GET); 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 * Check ID parameter
* *
@ -606,4 +686,22 @@ class ApiClient
return true; 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;
}
} }

View File

@ -5,6 +5,7 @@
<php> <php>
<server name="CRM_URL" value="foo" /> <server name="CRM_URL" value="foo" />
<server name="CRM_API_KEY" value="bar" /> <server name="CRM_API_KEY" value="bar" />
<server name="CRM_SITE" value="zoo" />
</php> </php>
<testsuites> <testsuites>

View File

@ -11,10 +11,15 @@ class TestCase extends \PHPUnit_Framework_TestCase
* *
* @param string $url (default: null) * @param string $url (default: null)
* @param string $apiKey (default: null) * @param string $apiKey (default: null)
* @param string $site (default: null)
* @return ApiClient * @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)
);
} }
} }

View File

@ -67,6 +67,37 @@ class ApiClientReferenceTest extends TestCase
$this->assertEquals(200, $response->getStatusCode()); $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() public function getListDictionaries()
{ {
return array( return array(
@ -79,6 +110,7 @@ class ApiClientReferenceTest extends TestCase
array('productStatuses'), array('productStatuses'),
array('statusGroups'), array('statusGroups'),
array('statuses'), array('statuses'),
array('sites'),
); );
} }