1
0
mirror of synced 2024-11-25 14:56:07 +03:00

add notifications/send method support

This commit is contained in:
Vladimir Kolchin 2023-03-31 14:09:58 +03:00
parent 186b44b0d4
commit b0080ff23c
5 changed files with 228 additions and 0 deletions

View File

@ -27,6 +27,7 @@ use RetailCrm\Api\ResourceGroup\Files;
use RetailCrm\Api\ResourceGroup\Integration; use RetailCrm\Api\ResourceGroup\Integration;
use RetailCrm\Api\ResourceGroup\Inventories; use RetailCrm\Api\ResourceGroup\Inventories;
use RetailCrm\Api\ResourceGroup\Loyalty; use RetailCrm\Api\ResourceGroup\Loyalty;
use RetailCrm\Api\ResourceGroup\Notifications;
use RetailCrm\Api\ResourceGroup\Orders; use RetailCrm\Api\ResourceGroup\Orders;
use RetailCrm\Api\ResourceGroup\Packs; use RetailCrm\Api\ResourceGroup\Packs;
use RetailCrm\Api\ResourceGroup\Payments; use RetailCrm\Api\ResourceGroup\Payments;
@ -82,6 +83,9 @@ class Client
/** @var \RetailCrm\Api\ResourceGroup\Loyalty */ /** @var \RetailCrm\Api\ResourceGroup\Loyalty */
public $loyalty; public $loyalty;
/** @var \RetailCrm\Api\ResourceGroup\Notifications */
public $notifications;
/** @var \RetailCrm\Api\ResourceGroup\Orders */ /** @var \RetailCrm\Api\ResourceGroup\Orders */
public $orders; public $orders;
@ -223,6 +227,14 @@ class Client
$eventDispatcher, $eventDispatcher,
$logger $logger
); );
$this->notifications = new Notifications(
$url,
$httpClient,
$requestTransformer,
$responseTransformer,
$eventDispatcher,
$logger
);
$this->orders = new Orders( $this->orders = new Orders(
$url, $url,
$httpClient, $httpClient,

View File

@ -0,0 +1,57 @@
<?php
/**
* PHP version 7.3
*
* @category Notification
* @package RetailCrm\Api\Model\Entity\Notifications
*/
namespace RetailCrm\Api\Model\Entity\Notifications;
use RetailCrm\Api\Component\Serializer\Annotation as JMS;
/**
* Class Notification
*
* @category Notification
* @package RetailCrm\Api\Model\Entity\Notifications
*
* @SuppressWarnings(PHPMD.LongVariables)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
*/
class Notification
{
/**
* @var string[]
*
* @JMS\Type("array<string>")
* @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<int>")
* @JMS\SerializedName("userIds")
*/
public $userIds;
}

View File

@ -0,0 +1,31 @@
<?php
/**
* PHP version 7.3
*
* @category NotificationsSendRequest
* @package RetailCrm\Api\Model\Request\Notifications
*/
namespace RetailCrm\Api\Model\Request\Notifications;
use RetailCrm\Api\Component\FormData\Mapping as Form;
use RetailCrm\Api\Interfaces\RequestInterface;
/**
* Class NotificationsSendRequest
*
* @category NotificationsSendRequest
* @package RetailCrm\Api\Model\Request\Notifications
*/
class NotificationsSendRequest implements RequestInterface
{
/**
* @var \RetailCrm\Api\Model\Entity\Notifications\Notification
*
* @Form\Type("RetailCrm\Api\Model\Entity\Notifications\Notification")
* @Form\SerializedName("notification")
* @Form\JsonField()
*/
public $notification;
}

View File

@ -0,0 +1,88 @@
<?php
/**
* PHP version 7.3
*
* @category Notifications
* @package RetailCrm\Api\ResourceGroup
*/
namespace RetailCrm\Api\ResourceGroup;
use RetailCrm\Api\Enum\RequestMethod;
use RetailCrm\Api\Model\Request\Notifications\NotificationsSendRequest;
use RetailCrm\Api\Model\Response\SuccessResponse;
/**
* Class Notifications
*
* @category Notifications
* @package RetailCrm\Api\ResourceGroup
*/
class Notifications extends AbstractApiResourceGroup
{
/**
* Makes POST "/api/v5/notifications/send" request.
*
* Example:
* ```php
* use RetailCrm\Api\Factory\SimpleClientFactory;
* use RetailCrm\Api\Model\Entity\Notifications\Notification;
* use RetailCrm\Api\Model\Request\Notifications\NotificationsSendRequest;
*
* $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey');
*
* $notification = new Notification();
* $notification->userGroups = ['superadmins'];
* $notification->type = 'api.info';
* $notification->message = '<p>notification text</p>';
*
* $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;
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace RetailCrm\Tests\ResourceGroup;
use RetailCrm\Api\Enum\RequestMethod;
use RetailCrm\Api\Model\Entity\Notifications\Notification;
use RetailCrm\Api\Model\Request\Notifications\NotificationsSendRequest;
use RetailCrm\TestUtils\Factory\TestClientFactory;
use RetailCrm\TestUtils\TestCase\AbstractApiResourceGroupTestCase;
class NotificationsTest extends AbstractApiResourceGroupTestCase
{
public function testSend(): void
{
$json = <<<'EOF'
{
"success": true
}
EOF;
$notification = new Notification();
$notification->type = 'api.info';
$notification->message = '<p>some notification</p>';
$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);
}
}