diff --git a/src/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApi.php b/src/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApi.php index 9dbc24d4..44fa1571 100644 --- a/src/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApi.php +++ b/src/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApi.php @@ -2,20 +2,28 @@ namespace BigCommerce\ApiV3\Api\Channels; +use BigCommerce\ApiV3\Api\Generic\BatchCreateResource; use BigCommerce\ApiV3\Api\Generic\BatchUpdateResource; use BigCommerce\ApiV3\Api\Generic\CreateResource; use BigCommerce\ApiV3\Api\Generic\DeleteResource; use BigCommerce\ApiV3\Api\Generic\GetAllResources; use BigCommerce\ApiV3\Api\Generic\UpdateResource; use BigCommerce\ApiV3\Api\Generic\V3ApiBase; +use BigCommerce\ApiV3\ResourceModels\Channel\ChannelCurrencyAssignment; use BigCommerce\ApiV3\ResponseModels\Channel\ChannelCurrencyAssignmentsResponse; use BigCommerce\ApiV3\ResponseModels\PaginatedResponse; -use BigCommerce\ApiV3\ResponseModels\SingleResourceResponse; /** * Channel Currency Assignments API * + * Example for finding all the currency assignments. Note that the parent id is effectively just a filter. * + * ```php + * $api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']); + * + * $allCurrencyAssignments = $api->channels()->currencyAssignments()->getAll()->getCurrencyAssignments(); + * $channelCurrencyAssignments = $api->channel(1)->currencyAssignments()->getAll()->getCurrencyAssignments(); + * ``` */ class ChannelCurrencyAssignmentsApi extends V3ApiBase { @@ -24,24 +32,38 @@ class ChannelCurrencyAssignmentsApi extends V3ApiBase use UpdateResource; use CreateResource; use BatchUpdateResource; - -// batchcreate? + use BatchCreateResource; private const CURRENCY_ASSIGNMENTS_ENDPOINT = 'channels/currency-assignments'; private const CURRENCY_ASSIGNMENT_ENDPOINT = 'channels/%d/currency-assignments'; - public function create(): ChannelCurrencyAssignmentsResponse + public function create(ChannelCurrencyAssignment $currencyAssignment): ChannelCurrencyAssignmentsResponse + { + return new ChannelCurrencyAssignmentsResponse($this->createResource($currencyAssignment)); + } + + public function update(ChannelCurrencyAssignment $currencyAssignment): ChannelCurrencyAssignmentsResponse { + return new ChannelCurrencyAssignmentsResponse($this->updateResource($currencyAssignment)); } - public function update(): ChannelCurrencyAssignmentsResponse + /** + * @param ChannelCurrencyAssignment[] $resources + * @return ChannelCurrencyAssignmentsResponse + */ + public function batchCreate(array $resources): PaginatedResponse { + return ChannelCurrencyAssignmentsResponse::buildFromMultipleResponses($this->batchCreateResource($resources)); } + /** + * @param ChannelCurrencyAssignment[] $resources + * @return ChannelCurrencyAssignmentsResponse + */ public function batchUpdate(array $resources): ChannelCurrencyAssignmentsResponse { - // TODO: Implement batchUpdate() method. + return ChannelCurrencyAssignmentsResponse::buildFromMultipleResponses($this->batchUpdateResource($resources)); } public function getAll(array $filters = [], int $page = 1, int $limit = 250): ChannelCurrencyAssignmentsResponse @@ -64,7 +86,6 @@ public function multipleResourceUrl(): string { return $this->getParentResourceId() ? $this->singleResourceUrl() : $this->multipleResourcesEndpoint(); } - public function singleResourceUrl(): string { return sprintf(self::CURRENCY_ASSIGNMENT_ENDPOINT, $this->getParentResourceId()); diff --git a/src/BigCommerce/Api/Generic/BatchCreateResource.php b/src/BigCommerce/Api/Generic/BatchCreateResource.php new file mode 100644 index 00000000..03cc7d90 --- /dev/null +++ b/src/BigCommerce/Api/Generic/BatchCreateResource.php @@ -0,0 +1,40 @@ +maxCreateBatchSize()); + $responses = []; + foreach ($chunks as $chunk) { + $responses[] = $this->getClient()->getRestClient()->post( + $this->multipleResourcesEndpoint(), + [ + RequestOptions::JSON => $chunk, + ] + ); + } + + return $responses; + } +} diff --git a/src/BigCommerce/ResponseModels/Channel/ChannelCurrencyAssignmentsResponse.php b/src/BigCommerce/ResponseModels/Channel/ChannelCurrencyAssignmentsResponse.php index 203cee46..14b6531f 100644 --- a/src/BigCommerce/ResponseModels/Channel/ChannelCurrencyAssignmentsResponse.php +++ b/src/BigCommerce/ResponseModels/Channel/ChannelCurrencyAssignmentsResponse.php @@ -3,9 +3,10 @@ namespace BigCommerce\ApiV3\ResponseModels\Channel; use BigCommerce\ApiV3\ResourceModels\Channel\ChannelCurrencyAssignment; +use BigCommerce\ApiV3\ResponseModels\PaginatedBatchableResponse; use BigCommerce\ApiV3\ResponseModels\PaginatedResponse; -class ChannelCurrencyAssignmentsResponse extends PaginatedResponse +class ChannelCurrencyAssignmentsResponse extends PaginatedBatchableResponse { /** * @return ChannelCurrencyAssignment[] diff --git a/tests/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApiTest.php b/tests/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApiTest.php index 9846a01b..961c0b2a 100644 --- a/tests/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApiTest.php +++ b/tests/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApiTest.php @@ -2,6 +2,7 @@ namespace BigCommerce\Tests\Api\Channels; +use BigCommerce\ApiV3\ResourceModels\Channel\ChannelCurrencyAssignment; use BigCommerce\Tests\BigCommerceApiTest; class ChannelCurrencyAssignmentsApiTest extends BigCommerceApiTest @@ -24,4 +25,64 @@ public function testCanGetAllChannelsCurrencyAssignments() $this->assertEquals('channels/currency-assignments', $this->getLastRequestPath()); $this->assertEquals('AUD', $currencyAssignments[0]->default_currency); } + + /** + * @return ChannelCurrencyAssignment[] + */ + private function getCurrencyAssignments(): array + { + $currencyAssignments = [new ChannelCurrencyAssignment(), new ChannelCurrencyAssignment()]; + + $currencyAssignments[0]->channel_id = 1; + $currencyAssignments[0]->enabled_currencies = ['USD', 'AUD']; + $currencyAssignments[0]->default_currency = 'AUD'; + + $currencyAssignments[0]->channel_id = 2; + $currencyAssignments[0]->enabled_currencies = ['USD', 'AUD', 'NZD']; + $currencyAssignments[0]->default_currency = 'NZD'; + + return $currencyAssignments; + } + + private function getCurrencyAssignment(): ChannelCurrencyAssignment + { + $currencyAssignment = new ChannelCurrencyAssignment(); + $currencyAssignment->channel_id = 1; + $currencyAssignment->enabled_currencies = ['AUD', 'NZD', 'CAD']; + $currencyAssignment->default_currency = 'CAD'; + + return $currencyAssignment; + } + + public function testCanCreateMultipleCurrencyAssignments() + { + $this->setReturnData('channel_currency_assignments.json'); + + $this->getApi()->channels()->currencyAssignments()->batchCreate($this->getCurrencyAssignments()); + $this->assertEquals('channels/currency-assignments', $this->getLastRequestPath()); + } + + public function testCanUpdateMultipleCurrencyAssignments() + { + $this->setReturnData('channel_currency_assignments.json'); + + $this->getApi()->channels()->currencyAssignments()->batchUpdate($this->getCurrencyAssignments()); + $this->assertEquals('channels/currency-assignments', $this->getLastRequestPath()); + } + + public function testCanCreateCurrencyAssignment() + { + $this->setReturnData('channel_currency_assignments.json'); + $channelId = 1; + $this->getApi()->channel(1)->currencyAssignments()->create($this->getCurrencyAssignment()); + $this->assertEquals("channels/$channelId/currency-assignments", $this->getLastRequestPath()); + } + + public function testCanUpdateCurrencyAssignment() + { + $this->setReturnData('channel_currency_assignments.json'); + $channelId = 1; + $this->getApi()->channel(1)->currencyAssignments()->update($this->getCurrencyAssignment()); + $this->assertEquals("channels/$channelId/currency-assignments", $this->getLastRequestPath()); + } }