shippinno / notification
Installs: 1 964
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 6
Forks: 1
Open Issues: 1
Requires
- php: ^8.2
- ext-json: *
- adbario/php-dot-notation: ^2.1
- alek13/slack: ^2.2
- doctrine/orm: ^2.5
- mathiasverraes/classfunctions: ^1.1
- shippinno/email: ^1.0.0
- shippinno/template: ^1.0
- symfony/cache: ^6.3
- tanigami/doctrine-json-unescaped-type: ^1.0
- tanigami/specification: ^1.3
- tanigami/value-objects: ^0.4.6
Requires (Dev)
- fakerphp/faker: ^1.9.1
- mockery/mockery: ^1.2
- phpunit/phpunit: ^10.1
- squizlabs/php_codesniffer: ^3.3
- dev-master
- v3.0.0
- v2.1.0
- v2.0.0
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.1.0
- v0.0.1
- dev-feature/php-version-up
- dev-feature/symfony-mailer
- dev-develop
- dev-feature/remove-all-method
This package is auto-updated.
Last update: 2024-12-09 08:06:41 UTC
README
Installation
$ composer require shippinno/notification
Basic Usage
Create and send a notification
Create a Notification
with a Destination
and send it through a Gateway
.
use Shippinno\Email\SwiftMailer\SwiftMailerSendEmail; use Shippinno\Notification\Domain\Model\Notification; use Shippinno\Notification\Domain\Model\NotificationNotSentException; use Shippinno\Notification\Infrastructure\Domain\Model\EmailGateway; $notification = new Notification( new EmailDestination( [new EmailAddress('to@example.com')], ), new Subject('Hello'), new Body('This is a notification.'), ); $gateway = new EmailGateway( new SwiftMailerSendEmail(...), new EmailAddress('from@example.com') ); try { $gateway->send($notification); } catch (NotificationNotSentException $e) { // ... }
Gateway
has to be compatible with the Destination
(check Destination::sendsToDestination(Destination $destination)
). In the case above, we assume EmailGateway
accepts notifications with EmailDestination
.
Persist notifications
Use NotificationRepository
to persist notifications on your database.
If you use DoctrineNotificationRepository
and set $isPrecocious
attribute to true
, you do not have to do EntityManager::flush()
.
$repository = new DoctrineNotificationRepository($em, $class, true); // $isPrecocious === true $repository->add($notification); // Already flushed.
You can retrieve fresh (not sent or failed) notifications to send them.
$notifications = $repository->freshNotifications();
Working with persisted notifications, you should want to mark them as sent or failed after trying to send.
If your DoctrineNotificationRepository
is precocious, calling persist()
will flush immediately.
try { $gateway->send($notification); $notification->markSent(); } catch (NotificationNotSentException $e) { $notification->markFailed($e->__toString()); // mark it failed with the reason } finally { $repository->persist($notification); }
Advanced usage
Using templates
Let’s say you have Liquid templates like:
$ tree -d /templates /templates |-- hello__subject.liquid `-- hello__body.liquid $ $ cat /templates/hello.subject.liquid Hello, {{ you }} !! $ $ cat /templates/hello.body.liquid Good bye, {{ her }} :)
Then you can create notifications using those templates with TemplateNotificationFactory
.
use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; use Shippinno\Template\Liquid; use Shippinno\Notification\Domain\Model\TemplateNotificationFactory; $template = new Liquid(new Filesystem(new Local('/templates'))); $factory = new TemplateNotificationFactory($template); $notification = $factory->create( 'hello', // template name ['you' => 'Shippinno', 'her' => 'Jessica']), // variables for the template new EmailDestination([new EmailAddress('to@example.com')]) ); $notification->subject()->subject(); // => 'Hello Shippinno !!' $notification->body()->body(); // => 'Good bye Jessica :)'
Check out shippinno/template for more details how the template things work.
Gateway routing
SendNotification
service routes a notification to and send it through a gateway designated on GatewayRegistry
.
use Shippinno\Notification\Domain\Model\SendNotification; use Shippinno\Notification\Domain\Model\GatewayRegistry; $gatewayRegistry = new GatewayRegistry; $gatewayRegistry->set('EmailDestination', new EmailGateway(...)); $gatewayRegistry->set('SlackChannelDestination', new SlackGateway(...)); $emailNotification = new Notification(new EmailDestination(...), ...); $slackChannelNotification = new Notification(new SlackChannelDestination(...), ...); $sendNotifiation = new SendNotification($gatewayRegistry); $sendNotification->execute($emailNotification); // will send an email $sendNotification->execute($slackChannelNotification); // will send a message to the Slack channel