1
0
mirror of synced 2024-11-25 14:56:07 +03:00
api-client-php/lib/RetailCrm/Methods/V5/Notifications.php
2023-03-09 09:58:39 +03:00

48 lines
1.5 KiB
PHP

<?php
namespace RetailCrm\Methods\V5;
use RetailCrm\Response\ApiResponse;
trait Notifications
{
protected static $allowedTypes = ['api.info', 'api.error'];
/**
* @return ApiResponse
*/
public function sendNotification(array $notification)
{
if (empty($notification['type']) || !in_array($notification['type'], self::$allowedTypes, true)) {
throw new \InvalidArgumentException(
'Parameter `type` must be not empty & must be equal one of these values: api.info|api.error'
);
}
if (empty($notification['message'])) {
throw new \InvalidArgumentException(
'Parameter `message` should not be blank'
);
}
if (empty($notification['userGroups']) && empty($notification['userIds'])) {
throw new \InvalidArgumentException(
'One of the parameters must be filled: `userGroups` or `userIds`'
);
}
if (!empty($notification['userGroups']) && !empty($notification['userIds'])) {
throw new \InvalidArgumentException(
'Only one of the two fields must be set: `userIds`, `userGroups`'
);
}
/** @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
"/notifications/send",
"POST",
['notification' => \json_encode($notification, JSON_UNESCAPED_SLASHES)]
);
}
}