shippinno/notification

该软件包最新版本(v3.0.0)没有可用的许可信息。


README

Scrutinizer Code Quality Code Coverage Build Status

安装

$ composer require shippinno/notification

基本用法

创建并发送通知

创建一个包含DestinationNotification并通过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必须与Destination兼容(检查Destination::sendsToDestination(Destination $destination))。在上面的例子中,我们假设EmailGateway接受EmailDestination的通知。

持久化通知

使用NotificationRepository将通知持久化到您的数据库。

如果您使用DoctrineNotificationRepository并将$isPrecocious属性设置为true,您不需要执行EntityManager::flush()

$repository = new DoctrineNotificationRepository($em, $class, true); // $isPrecocious === true
$repository->add($notification); // Already flushed.

您可以检索新鲜(未发送或失败的)通知以发送它们。

$notifications = $repository->freshNotifications();

与持久化通知一起工作,您可能希望在尝试发送后将其标记为已发送或失败。

如果您的DoctrineNotificationRepository是预先的,调用persist()将立即刷新。

try {
    $gateway->send($notification);
    $notification->markSent();
} catch (NotificationNotSentException $e) {
    $notification->markFailed($e->__toString()); // mark it failed with the reason
} finally {
    $repository->persist($notification);
}

高级用法

使用模板

假设您有Liquid模板,如下所示:

$ 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 }} :)

然后您可以使用这些模板通过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 :)'

有关模板如何工作的更多详细信息,请参阅shippinno/template

网关路由

SendNotification服务将通知路由到并通过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