From 33f34b991e79fdf10910011703e423210c436268 Mon Sep 17 00:00:00 2001 From: Ilyas Salikhov Date: Thu, 27 Nov 2014 11:23:44 +0300 Subject: [PATCH] Added sites methods, Added site parameter to order/customer methods --- apigen.neon | 2 +- composer.json | 2 +- lib/RetailCrm/ApiClient.php | 150 +++++++++++++++--- phpunit.xml.dist | 1 + tests/RetailCrm/Test/TestCase.php | 9 +- .../Tests/ApiClientReferenceTest.php | 32 ++++ 6 files changed, 166 insertions(+), 30 deletions(-) diff --git a/apigen.neon b/apigen.neon index fffc08c..965b4d0 100644 --- a/apigen.neon +++ b/apigen.neon @@ -32,5 +32,5 @@ php: false tree: true deprecated: true todo: true -destination: docs/api +destination: ../api-client-php.pages/ download: false diff --git a/composer.json b/composer.json index 0b9dbd6..8280092 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index 591c493..e77740a 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -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; + } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ac37b0e..651ae70 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,6 +5,7 @@ + diff --git a/tests/RetailCrm/Test/TestCase.php b/tests/RetailCrm/Test/TestCase.php index c004a66..cb58cce 100644 --- a/tests/RetailCrm/Test/TestCase.php +++ b/tests/RetailCrm/Test/TestCase.php @@ -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) + ); } } \ No newline at end of file diff --git a/tests/RetailCrm/Tests/ApiClientReferenceTest.php b/tests/RetailCrm/Tests/ApiClientReferenceTest.php index 95d0b72..1f10988 100644 --- a/tests/RetailCrm/Tests/ApiClientReferenceTest.php +++ b/tests/RetailCrm/Tests/ApiClientReferenceTest.php @@ -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'), ); }