From b0080ff23c2d454f8ede089d6463e91f86fdd067 Mon Sep 17 00:00:00 2001 From: Vladimir Kolchin Date: Fri, 31 Mar 2023 14:09:58 +0300 Subject: [PATCH] add notifications/send method support --- src/Client.php | 12 +++ .../Entity/Notifications/Notification.php | 57 ++++++++++++ .../NotificationsSendRequest.php | 31 +++++++ src/ResourceGroup/Notifications.php | 88 +++++++++++++++++++ tests/src/ResourceGroup/NotificationsTest.php | 40 +++++++++ 5 files changed, 228 insertions(+) create mode 100644 src/Model/Entity/Notifications/Notification.php create mode 100644 src/Model/Request/Notifications/NotificationsSendRequest.php create mode 100644 src/ResourceGroup/Notifications.php create mode 100644 tests/src/ResourceGroup/NotificationsTest.php diff --git a/src/Client.php b/src/Client.php index 5f39d4a..4615843 100644 --- a/src/Client.php +++ b/src/Client.php @@ -27,6 +27,7 @@ use RetailCrm\Api\ResourceGroup\Files; use RetailCrm\Api\ResourceGroup\Integration; use RetailCrm\Api\ResourceGroup\Inventories; use RetailCrm\Api\ResourceGroup\Loyalty; +use RetailCrm\Api\ResourceGroup\Notifications; use RetailCrm\Api\ResourceGroup\Orders; use RetailCrm\Api\ResourceGroup\Packs; use RetailCrm\Api\ResourceGroup\Payments; @@ -82,6 +83,9 @@ class Client /** @var \RetailCrm\Api\ResourceGroup\Loyalty */ public $loyalty; + /** @var \RetailCrm\Api\ResourceGroup\Notifications */ + public $notifications; + /** @var \RetailCrm\Api\ResourceGroup\Orders */ public $orders; @@ -223,6 +227,14 @@ class Client $eventDispatcher, $logger ); + $this->notifications = new Notifications( + $url, + $httpClient, + $requestTransformer, + $responseTransformer, + $eventDispatcher, + $logger + ); $this->orders = new Orders( $url, $httpClient, diff --git a/src/Model/Entity/Notifications/Notification.php b/src/Model/Entity/Notifications/Notification.php new file mode 100644 index 0000000..96d6b17 --- /dev/null +++ b/src/Model/Entity/Notifications/Notification.php @@ -0,0 +1,57 @@ +") + * @JMS\SerializedName("userGroups") + */ + public $userGroups; + + /** + * @var string + * + * @JMS\Type("string") + * @JMS\SerializedName("type") + */ + public $type; + + /** + * @var string + * + * @JMS\Type("string") + * @JMS\SerializedName("message") + */ + public $message; + + /** + * @var int[] + * + * @JMS\Type("array") + * @JMS\SerializedName("userIds") + */ + public $userIds; +} diff --git a/src/Model/Request/Notifications/NotificationsSendRequest.php b/src/Model/Request/Notifications/NotificationsSendRequest.php new file mode 100644 index 0000000..2462e20 --- /dev/null +++ b/src/Model/Request/Notifications/NotificationsSendRequest.php @@ -0,0 +1,31 @@ +userGroups = ['superadmins']; + * $notification->type = 'api.info'; + * $notification->message = '

notification text

'; + * + * $request = new NotificationsSendRequest(); + * $request->notification = $notification; + * + * try { + * $response = $client->notifications->send($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; + * } + * + * echo 'Status: ' . var_export($response->success, true); + * ``` + * + * @param NotificationsSendRequest $request + * + * @return SuccessResponse + * @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface + * @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface + * @throws \RetailCrm\Api\Exception\ApiException + * @throws \RetailCrm\Api\Exception\ClientException + * @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 send(NotificationsSendRequest $request): SuccessResponse + { + /** @var SuccessResponse $response */ + $response = $this->sendRequest( + RequestMethod::POST, + 'notifications/send', + $request, + SuccessResponse::class + ); + return $response; + } +} diff --git a/tests/src/ResourceGroup/NotificationsTest.php b/tests/src/ResourceGroup/NotificationsTest.php new file mode 100644 index 0000000..c6a49ea --- /dev/null +++ b/tests/src/ResourceGroup/NotificationsTest.php @@ -0,0 +1,40 @@ +type = 'api.info'; + $notification->message = '

some notification

'; + $notification->userGroups = ['superadmins']; + + $request = new NotificationsSendRequest(); + $request->notification = $notification; + + $mock = static::createApiMockBuilder('notifications/send'); + $mock->matchMethod(RequestMethod::POST) + ->matchBody(self::encodeForm($request)) + ->reply(200) + ->withBody($json); + + $client = TestClientFactory::createClient($mock->getClient()); + $response = $client->notifications->send($request); + + self::assertModelEqualsToResponse($json, $response); + } +}