From 9e7bc31e589be1cbdda337a6897b60db069841b4 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Tue, 9 Mar 2021 15:54:19 +1030 Subject: [PATCH 1/5] Add skeleton for custom template associations api #36 --- RELEASE_NOTES.md | 3 ++- .../CustomTemplateAssociationsApi.php | 8 +++++++ .../CustomTemplateAssociation.php | 7 ++++++ .../CustomTemplateAssociationsResponse.php | 7 ++++++ .../CustomTemplateAssociationsApiTest.php | 24 +++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php create mode 100644 src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php create mode 100644 src/BigCommerce/ResponseModels/CustomTemplateAssociation/CustomTemplateAssociationsResponse.php create mode 100644 tests/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApiTest.php diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 570f1d33..28b23ff7 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,3 +1,4 @@ ### New Features -Until BigCommerce decide to be consistent and include a _Get Customer_ endpoint, add `CustomersApi::getById(int $id)`. +Implement the new (Custom Template Associations)[https://developer.bigcommerce.com/api-reference/store-management/custom-template-associations] +API. \ No newline at end of file diff --git a/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php b/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php new file mode 100644 index 00000000..e98114cb --- /dev/null +++ b/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php @@ -0,0 +1,8 @@ +markTestIncomplete(); + } + + public function testCanDeleteAssociation() + { + $this->markTestIncomplete(); + } + + public function testCanUpsertAssociation() + { + $this->markTestIncomplete(); + } +} From 8b5a5c9b25d68a356116dbb1a5856739d7372e75 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Tue, 9 Mar 2021 16:32:01 +1030 Subject: [PATCH 2/5] Implement Get Custom Template Association #36 --- .../CustomTemplateAssociationsApi.php | 27 ++++++++++++++-- src/BigCommerce/Client.php | 6 ++++ .../CustomTemplateAssociation.php | 17 +++++++--- .../CustomTemplateAssociationsResponse.php | 18 +++++++++-- .../Customer/SubscribersResponse.php | 1 + .../CustomTemplateAssociationsApiTest.php | 8 +++-- .../responses/templates__get_all.json | 31 +++++++++++++++++++ 7 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 tests/BigCommerce/responses/templates__get_all.json diff --git a/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php b/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php index e98114cb..5cfad259 100644 --- a/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php +++ b/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php @@ -1,8 +1,29 @@ getAllResources($filters, $page, $limit)); + } +} diff --git a/src/BigCommerce/Client.php b/src/BigCommerce/Client.php index b39aa656..4e3d4659 100644 --- a/src/BigCommerce/Client.php +++ b/src/BigCommerce/Client.php @@ -10,6 +10,7 @@ use BigCommerce\ApiV3\Api\Scripts\ScriptsApi; use BigCommerce\ApiV3\Api\Themes\ThemesApi; use BigCommerce\ApiV3\Api\Widgets\WidgetsApi; +use BigCommerce\ApiV3\Api\CustomTemplateAssociations\CustomTemplateAssociationsApi; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; @@ -159,4 +160,9 @@ public function content(): WidgetsApi { return $this->widgets(); } + + public function customTemplateAssociations(): CustomTemplateAssociationsApi + { + return new CustomTemplateAssociationsApi($this); + } } diff --git a/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php b/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php index 8964e870..acd252fc 100644 --- a/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php +++ b/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php @@ -1,7 +1,16 @@ getData(); + } -} \ No newline at end of file + protected function resourceClass(): string + { + return CustomTemplateAssociation::class; + } +} diff --git a/src/BigCommerce/ResponseModels/Customer/SubscribersResponse.php b/src/BigCommerce/ResponseModels/Customer/SubscribersResponse.php index 06bcf3c3..c9922217 100644 --- a/src/BigCommerce/ResponseModels/Customer/SubscribersResponse.php +++ b/src/BigCommerce/ResponseModels/Customer/SubscribersResponse.php @@ -14,6 +14,7 @@ public function getSubscribers(): array { return $this->getData(); } + protected function resourceClass(): string { return Subscriber::class; diff --git a/tests/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApiTest.php b/tests/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApiTest.php index dc2e8e54..31b8d7e2 100644 --- a/tests/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApiTest.php +++ b/tests/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApiTest.php @@ -7,9 +7,13 @@ class CustomTemplateAssociationsApiTest extends BigCommerceApiTest { - public function testCanGetAssociation() + public function testCanGetAssociations() { - $this->markTestIncomplete(); + $this->setReturnData('templates__get_all.json'); + + $response = $this->getApi()->customTemplateAssociations()->getAll(); + $this->assertEquals(5, $response->getPagination()->count); + $this->assertEquals('custom-product-1.html', $response->getCustomTemplateAssociations()[0]->file_name); } public function testCanDeleteAssociation() diff --git a/tests/BigCommerce/responses/templates__get_all.json b/tests/BigCommerce/responses/templates__get_all.json new file mode 100644 index 00000000..558b66cc --- /dev/null +++ b/tests/BigCommerce/responses/templates__get_all.json @@ -0,0 +1,31 @@ +{ + "data": [ + { + "id": 1, + "channel_id": 1, + "entity_type": "product", + "entity_id": 123, + "file_name": "custom-product-1.html" + }, + { + "id": 2, + "channel_id": 12345, + "entity_type": "page", + "entity_id": 123, + "file_name": "custom-page.html" + } + ], + "meta": { + "pagination": { + "total": 246, + "count": 5, + "per_page": 5, + "current_page": 1, + "total_pages": 50, + "links": { + "next": "?limit=5&page=2", + "current": "?limit=5&page=1" + } + } + } +} From f49006ec55f19aeb81f67691f27d030d09b7fb47 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 10 Mar 2021 10:20:28 +1030 Subject: [PATCH 3/5] Implement Upsert Custom Template Associations #36 --- .../CustomTemplateAssociationsApi.php | 29 ++++++++++++-- .../Api/Generic/BatchUpdateResource.php | 40 +++++++++++++++++++ .../Generic/ResourceWithBatchUpdateApi.php | 25 +----------- .../CustomTemplateAssociationsResponse.php | 4 +- .../CustomTemplateAssociationsApiTest.php | 24 ++++++++++- 5 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 src/BigCommerce/Api/Generic/BatchUpdateResource.php diff --git a/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php b/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php index 5cfad259..d476ec3b 100644 --- a/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php +++ b/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php @@ -1,14 +1,16 @@ getAllResources($filters, $page, $limit)); } + + /** + * @param CustomTemplateAssociation[] $templateAssociations + * @return CustomTemplateAssociationsResponse + */ + public function batchUpdate(array $templateAssociations): CustomTemplateAssociationsResponse + { + return CustomTemplateAssociationsResponse::buildFromMultipleResponses( + $this->batchUpdateResource($templateAssociations) + ); + } + + public function multipleResourcesEndpoint(): string + { + return self::TEMPLATES_ENDPOINT; + } + + public function multipleResourceUrl(): string + { + return $this->multipleResourcesEndpoint(); + } } diff --git a/src/BigCommerce/Api/Generic/BatchUpdateResource.php b/src/BigCommerce/Api/Generic/BatchUpdateResource.php new file mode 100644 index 00000000..ca42cb0a --- /dev/null +++ b/src/BigCommerce/Api/Generic/BatchUpdateResource.php @@ -0,0 +1,40 @@ +maxBatchSize()); + $responses = []; + foreach ($chunks as $chunk) { + $responses[] = $this->getClient()->getRestClient()->put( + $this->multipleResourcesEndpoint(), + [ + RequestOptions::JSON => $chunk, + ] + ); + } + + return $responses; + } +} \ No newline at end of file diff --git a/src/BigCommerce/Api/Generic/ResourceWithBatchUpdateApi.php b/src/BigCommerce/Api/Generic/ResourceWithBatchUpdateApi.php index 4744c877..2b9f49f6 100644 --- a/src/BigCommerce/Api/Generic/ResourceWithBatchUpdateApi.php +++ b/src/BigCommerce/Api/Generic/ResourceWithBatchUpdateApi.php @@ -8,28 +8,5 @@ abstract class ResourceWithBatchUpdateApi extends ResourceApi { - protected const MAX_BATCH_SIZE = 10; - - abstract public function batchUpdate(array $resources): PaginatedResponse; - - /** - * @param array $resources - * @return ResponseInterface[] - * @throws \GuzzleHttp\Exception\GuzzleException - */ - protected function batchUpdateResource(array $resources): array - { - $chunks = array_chunk($resources, self::MAX_BATCH_SIZE); - $responses = []; - foreach ($chunks as $chunk) { - $responses[] = $this->getClient()->getRestClient()->put( - $this->multipleResourcesEndpoint(), - [ - RequestOptions::JSON => $chunk, - ] - ); - } - - return $responses; - } + use BatchUpdateResource; } diff --git a/src/BigCommerce/ResponseModels/CustomTemplateAssociation/CustomTemplateAssociationsResponse.php b/src/BigCommerce/ResponseModels/CustomTemplateAssociation/CustomTemplateAssociationsResponse.php index 19de3db9..edac4b87 100644 --- a/src/BigCommerce/ResponseModels/CustomTemplateAssociation/CustomTemplateAssociationsResponse.php +++ b/src/BigCommerce/ResponseModels/CustomTemplateAssociation/CustomTemplateAssociationsResponse.php @@ -1,10 +1,10 @@ markTestIncomplete(); + $this->setReturnData('templates__get_all.json'); + $templateAssociationOne = new CustomTemplateAssociation((object)[ + "id" => 1, + "channel_id" => 1, + "entity_type" => "product", + "entity_id" => 123, + "file_name" => "custom-product-1.html", + ]); + + $templateAssociationTwo = new CustomTemplateAssociation((object)[ + "id" => 2, + "channel_id" => 12345, + "entity_type" => "page", + "entity_id" => 123, + "file_name" => "custom-page.html", + ]); + + $this->getApi()->customTemplateAssociations()->batchUpdate([ + $templateAssociationOne, $templateAssociationTwo + ]); + + $this->assertTrue(true); } } From c2067863c1ecde37721d7200cc40fc7f64014eea Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 10 Mar 2021 10:38:12 +1030 Subject: [PATCH 4/5] Implement Delete Custom Template Associations #36 --- .../CustomTemplateAssociationsApi.php | 37 +++++++++++++++++++ .../CustomTemplateAssociation.php | 5 +++ .../CustomTemplateAssociationsApiTest.php | 11 +++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php b/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php index d476ec3b..cf8ab173 100644 --- a/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php +++ b/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php @@ -6,6 +6,7 @@ use BigCommerce\ApiV3\Api\Generic\V3ApiBase; use BigCommerce\ApiV3\ResourceModels\CustomTemplateAssociation\CustomTemplateAssociation; use BigCommerce\ApiV3\ResponseModels\CustomTemplateAssociation\CustomTemplateAssociationsResponse; +use GuzzleHttp\RequestOptions; class CustomTemplateAssociationsApi extends V3ApiBase { @@ -19,6 +20,11 @@ class CustomTemplateAssociationsApi extends V3ApiBase public const FILTER_TYPE = 'type'; public const FILTER_IS_VALID = 'is_valid'; + public const DELETE_QUERY_ID_IN = 'id:in'; + public const DELETE_QUERY_ENTITY_ID_IN = 'entity_id:in'; + public const DELETE_QUERY_CHANNEL_ID = 'channel_id'; + public const DELETE_QUERY_TYPE = 'type'; + protected function maxBatchSize(): int { return 100; @@ -49,4 +55,35 @@ public function multipleResourceUrl(): string { return $this->multipleResourcesEndpoint(); } + + public function delete(array $query): void + { + $this->getClient()->getRestClient()->delete( + $this->multipleResourceUrl(), + [ + RequestOptions::QUERY => $query + ]); + } + + public function deleteByIds(array $ids): void + { + $this->delete([ + self::DELETE_QUERY_ID_IN => $ids + ]); + } + + public function deleteByChannelId(int $channelId): void + { + $this->delete([ + self::DELETE_QUERY_CHANNEL_ID => $channelId + ]); + } + + public function deleteByEntityIds(string $type, array $ids): void + { + $this->delete([ + self::DELETE_QUERY_ENTITY_ID_IN => $ids, + self::DELETE_QUERY_TYPE => $type, + ]); + } } diff --git a/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php b/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php index acd252fc..76fc2c72 100644 --- a/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php +++ b/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php @@ -5,6 +5,11 @@ class CustomTemplateAssociation extends ResourceModel { + public const TYPE_PRODUCT = 'product'; + public const TYPE_CATEGORY = 'category'; + public const TYPE_BRAND = 'brand'; + public const TYPE_PAGE = 'page'; + public int $id; public int $channel_id; public string $entity_type; diff --git a/tests/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApiTest.php b/tests/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApiTest.php index f12cf96a..35251c7a 100644 --- a/tests/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApiTest.php +++ b/tests/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApiTest.php @@ -2,6 +2,7 @@ namespace BigCommerce\Tests\Api\CustomTemplateAssociation; +use BigCommerce\ApiV3\Api\CustomTemplateAssociations\CustomTemplateAssociationsApi; use BigCommerce\ApiV3\ResourceModels\CustomTemplateAssociation\CustomTemplateAssociation; use BigCommerce\Tests\BigCommerceApiTest; @@ -19,7 +20,15 @@ public function testCanGetAssociations() public function testCanDeleteAssociation() { - $this->markTestIncomplete(); + $this->setReturnData(self::EMPTY_RESPONSE, 204); + + $customTemplateAssocApi = $this->getApi()->customTemplateAssociations(); + $customTemplateAssocApi->deleteByChannelId(1); + $this->setReturnData(self::EMPTY_RESPONSE, 204); + $customTemplateAssocApi->deleteByIds([1, 2, 3]); + $this->setReturnData(self::EMPTY_RESPONSE, 204); + $customTemplateAssocApi->deleteByEntityIds(CustomTemplateAssociation::TYPE_CATEGORY, [4, 5, 6]); + $this->assertTrue(true); } public function testCanUpsertAssociation() From 90c525690fa0d8f831cfb592345ac036d35ddc77 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 10 Mar 2021 11:16:39 +1030 Subject: [PATCH 5/5] Fix code style issues --- .../CustomTemplateAssociationsApi.php | 4 +++- src/BigCommerce/Api/Generic/BatchUpdateResource.php | 2 +- .../CustomTemplateAssociation/CustomTemplateAssociation.php | 1 + .../CustomTemplateAssociationsResponse.php | 1 + .../CustomTemplateAssociationsApiTest.php | 1 - 5 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php b/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php index cf8ab173..fa093004 100644 --- a/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php +++ b/src/BigCommerce/Api/CustomTemplateAssociations/CustomTemplateAssociationsApi.php @@ -1,4 +1,5 @@ multipleResourceUrl(), [ RequestOptions::QUERY => $query - ]); + ] + ); } public function deleteByIds(array $ids): void diff --git a/src/BigCommerce/Api/Generic/BatchUpdateResource.php b/src/BigCommerce/Api/Generic/BatchUpdateResource.php index ca42cb0a..f80e2d88 100644 --- a/src/BigCommerce/Api/Generic/BatchUpdateResource.php +++ b/src/BigCommerce/Api/Generic/BatchUpdateResource.php @@ -37,4 +37,4 @@ protected function batchUpdateResource(array $resources): array return $responses; } -} \ No newline at end of file +} diff --git a/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php b/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php index 76fc2c72..733e0eaf 100644 --- a/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php +++ b/src/BigCommerce/ResourceModels/CustomTemplateAssociation/CustomTemplateAssociation.php @@ -1,4 +1,5 @@