From b7ae093cc4dd0b95dd533ec41fd418c45139c267 Mon Sep 17 00:00:00 2001 From: Opheugene Date: Thu, 20 Jun 2024 14:03:20 +0200 Subject: [PATCH] Support for working with customer subscriptions --- src/Model/Entity/Customers/Subscription.php | 45 +++++++++++++ .../CustomersSubscriptionsRequest.php | 47 +++++++++++++ src/ResourceGroup/Customers.php | 66 +++++++++++++++++++ tests/src/ResourceGroup/CustomersTest.php | 32 +++++++++ 4 files changed, 190 insertions(+) create mode 100644 src/Model/Entity/Customers/Subscription.php create mode 100644 src/Model/Request/Customers/CustomersSubscriptionsRequest.php diff --git a/src/Model/Entity/Customers/Subscription.php b/src/Model/Entity/Customers/Subscription.php new file mode 100644 index 0000000..0067bad --- /dev/null +++ b/src/Model/Entity/Customers/Subscription.php @@ -0,0 +1,45 @@ +channel = 'waba'; + * $subscription->active = false; + * $subscription->unsubscribeMessageId = 123; + * $request = new CustomersSubscriptionsRequest(); + * $request->by = ByIdentifier::ID; + * $request->site = 'aliexpress'; + * $request->subscriptions = [$subscription]; + * + * try { + * $response = $client->customers->subscriptions(4770, $request); + * } catch (ApiExceptionInterface $exception) { + * echo sprintf( + * 'Error from RetailCRM API (status code: %d): %s', + * $exception->getStatusCode(), + * $exception->getMessage() + * ); + * + * if (count($exception->getErrorResponse()->errors) > 0) { + * echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors); + * } + * + * return; + * } + * ``` + * + * @param int|string $identifier + * @param \RetailCrm\Api\Model\Request\Customers\CustomersSubscriptionsRequest $request + * + * @return \RetailCrm\Api\Model\Response\SuccessResponse + * @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface + * @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface + * @throws \RetailCrm\Api\Exception\Api\AccountDoesNotExistException + * @throws \RetailCrm\Api\Exception\Api\ApiErrorException + * @throws \RetailCrm\Api\Exception\Api\MissingCredentialsException + * @throws \RetailCrm\Api\Exception\Api\MissingParameterException + * @throws \RetailCrm\Api\Exception\Api\ValidationException + * @throws \RetailCrm\Api\Exception\Client\HandlerException + * @throws \RetailCrm\Api\Exception\Client\HttpClientException + */ + public function subscriptions($identifier, CustomersSubscriptionsRequest $request): SuccessResponse + { + /** @var SuccessResponse $response */ + $response = $this->sendRequest( + RequestMethod::POST, + 'customers/' . $identifier . '/subscriptions', + $request, + SuccessResponse::class + ); + return $response; + } } diff --git a/tests/src/ResourceGroup/CustomersTest.php b/tests/src/ResourceGroup/CustomersTest.php index 3b99ed9..79ee92c 100644 --- a/tests/src/ResourceGroup/CustomersTest.php +++ b/tests/src/ResourceGroup/CustomersTest.php @@ -22,6 +22,7 @@ use RetailCrm\Api\Model\Entity\Customers\CustomerNote; use RetailCrm\Api\Model\Entity\Customers\CustomerPhone; use RetailCrm\Api\Model\Entity\Customers\CustomerTag; use RetailCrm\Api\Model\Entity\Customers\SerializedCustomerReference; +use RetailCrm\Api\Model\Entity\Customers\Subscription; use RetailCrm\Api\Model\Entity\FixExternalRow; use RetailCrm\Api\Model\Filter\Customers\CustomerFilter; use RetailCrm\Api\Model\Filter\Customers\CustomerHistoryFilter; @@ -35,6 +36,7 @@ use RetailCrm\Api\Model\Request\Customers\CustomersHistoryRequest; use RetailCrm\Api\Model\Request\Customers\CustomersNotesCreateRequest; use RetailCrm\Api\Model\Request\Customers\CustomersNotesRequest; use RetailCrm\Api\Model\Request\Customers\CustomersRequest; +use RetailCrm\Api\Model\Request\Customers\CustomersSubscriptionsRequest; use RetailCrm\Api\Model\Request\Customers\CustomersUploadRequest; use RetailCrm\TestUtils\Factory\TestClientFactory; use RetailCrm\TestUtils\TestCase\AbstractApiResourceGroupTestCase; @@ -2773,4 +2775,34 @@ EOF; self::assertModelEqualsToResponse($json, $response); } + + public function testCustomersSubscriptions(): void + { + $json = <<<'EOF' +{ + "success": true +} +EOF; + + $subscription = new Subscription(); + $subscription->channel = 'waba'; + $subscription->active = false; + $subscription->unsubscribeMessageId = 123; + + $request = new CustomersSubscriptionsRequest(); + $request->by = ByIdentifier::ID; + $request->site = 'aliexpress'; + $request->subscriptions = [$subscription]; + + $mock = static::createApiMockBuilder('customers/4770/subscriptions'); + $mock->matchMethod(RequestMethod::POST) + ->matchBody(static::encodeForm($request)) + ->reply(200) + ->withBody($json); + + $client = TestClientFactory::createClient($mock->getClient()); + $response = $client->customers->subscriptions(4770, $request); + + self::assertModelEqualsToResponse($json, $response); + } }