wearesho-team / wearesho-notifications-repository
Requires
- php: >=7.2
- guzzlehttp/guzzle: ^6.3
- horat1us/environment-config: ^1.2
Requires (Dev)
- phpunit/phpunit: ^7.3
- squizlabs/php_codesniffer: ^3.3
This package is auto-updated.
Last update: 2024-10-26 05:18:27 UTC
README
This library represents a flexible adapter between your project and Wearesho Notifications. You can either create user`s authorization token for connecting to notifications server using sockets, or push a notification to it.
Setup
composer require wearesho-team/wearesho-notifications-repository
Usage
Config
To deal with it, you should create ConfigInterface. There are two internal implementations, but you can also implement it by yourself.
<?php /** * @var string $requestsUrl URL to your notification server. * @var string|null $serviceKey Access key to notification server. Optional (depends on server requirements). */ $config = new Wearesho\Notifications\Config($requestsUrl, $serviceKey);
If you prefer environment configuration, you can use EnvironmentConfig
WEARESHO_NOTIFICATIONS_URL=https://your.notification.server/ WEARESHO_NOTIFICATIONS_SERVICE_KEY='your personal service key, optional'
<?php $config = new Wearesho\Notifications\EnvironmentConfig($dotenvPrefix = 'WEARESHO_NOTIFICATIONS_');
Create a repository instance
<?php /** * @var Wearesho\Notifications\ConfigInterface $config * @var GuzzleHttp\ClientInterface $guzzleClient */ $repository = new Wearesho\Notifications\Repository($config, $guzzleClient);
Authorize
This method takes user`s ID and returns authorization token for connection
<?php /** * @var Wearesho\Notifications\Repository $repository * @var int $userId */ try { $authorizationToken = $repository->authorize($userId); } catch (Wearesho\Notifications\Exceptions\Credentials\Missed $exception) { // Your server requires service key, but you have not passed it in config } catch (Wearesho\Notifications\Exceptions\Credentials\Invalid $exception) { // Your service key is invalid } catch (Wearesho\Notifications\Exceptions\InvalidResponse $exception) { // Unexpected service response. // You can receive response instance using $exception->getResponse() }
Push notification
Firstly you need to create a notification entity
<?php /** * @var int $userId Notification's owner * @var string $message Notification's content * @var array|null $context Special params for message. * F.e. if message is like 'Hello, {person}', you can pass params like [ 'person' => 'Jonh', ] * This params can be applied in front-end * * @var string|null $type Notification type. * Can be any string. but we recommend to use Wearesho\Notifications\Notification\Type constants * to avoid unexpected situations. * * @var \DateTime|null $time Notification's creation date * @var bool|null $isRead Mark if the notification is read. */ $notification = new Wearesho\Notifications\Notification( $userId, $message, $context, $type, $time, $isRead );
Then pass it to repository
<?php /** * @var Wearesho\Notifications\Notification $notification * @var Wearesho\Notifications\Repository $repository */ try { $repository->push($notification); } catch (Wearesho\Notifications\Exceptions\Credentials\Missed $exception) { // Your server requires service key, but you have not passed it in config } catch (Wearesho\Notifications\Exceptions\Credentials\Invalid $exception) { // Your service key is invalid } catch (Wearesho\Notifications\Exceptions\InvalidNotification $exception) { // You have been formed invalid notification // You can receive it using $exception->getNotification() }
Push chain
You can push notifications to different notification servers using Push\Chain. It's quite useful in use in pair with Push\Filter.
<?php use Wearesho\Notifications; /** @var Notifications\Repository $repoFirst */ /** @var Notifications\Repository $repoSecond */ $chain = new Notifications\Push\Chain([ new Notifications\Push\Filter( $repoFirst, $types = [ 'primary', ] ), $repoSecond ]); /** @var Notifications\Notification $notification */ $chain->push($notification);
In this example notifications will be pushed to first and second repository
if notification has primary
type, or only to second if notification has
another type.