nikolaposa/notifier

一个可扩展的库,用于构建通知并通过不同的交付渠道发送。

4.0.0 2020-04-05 09:46 UTC

This package is auto-updated.

Last update: 2024-09-12 06:29:57 UTC


README

Build Status Code Quality Code Coverage Latest Version PDS Skeleton

一个可扩展的库,用于构建通知并通过不同的交付渠道发送。

安装

首选的安装方法是使用 Composer。运行以下命令安装最新版本的包并将其添加到项目的 composer.json

composer require nikolaposa/notifier

操作理论

通知是通知用户应用中某些事件的信息性消息,可以通过不同的渠道(例如电子邮件、短信、移动推送)发送。通知是一个高级抽象概念,它封装了要通知接收者的主题,而不管可以通过哪种交付渠道传递该信息。从架构角度来看,通知是一个领域关注点。

为了最小化领域与发送通知的基础设施之间的耦合,Notifier 库基于隐蔽的接口,这些接口应由您的对象实现以将其插入到库的工作流程中。这些接口是

  1. Notification - 将对象标记为可使用 Notifier 库的通知,
  2. Recipient - 表示通知的接收者,它为某个渠道提供联系信息(例如电子邮件地址、电话号码);通常由用户领域对象实现。

对于通知应该通过其发送的每个渠道,Notification 类应实现特定的渠道接口,使通知适合通过特定渠道发送。这些接口声明消息构建方法,例如 EmailNotification::toEmailMessage(),它将通知转换为通过该特定渠道发送的消息。特定的渠道通知接口扩展了 Notification 接口本身,因此您不需要显式实现它。

渠道组件捕获了通过特定交付渠道发送通知的实现细节。特定的渠道实现通常包括

  1. 特定渠道的通知接口,
  2. 消息类 - 将通知转换为的传输级别消息,
  3. Channel 实现,负责发送通知的实际操作。

默认情况下,此库提供了通过电子邮件和短信发送通知的功能。高度可扩展的设计允许实现自定义交付渠道。

最后,Notifier 服务是一个外观,它管理将通知发送到一系列接收者的整个过程,通过支持的渠道。这是调用代码直接与之交互的唯一服务。

使用方法

创建通知

namespace App\Model;

use Notifier\Channel\Email\EmailMessage;
use Notifier\Channel\Sms\SmsMessage;
use Notifier\Channel\Email\EmailNotification;
use Notifier\Channel\Sms\SmsNotification;
use Notifier\Recipient\Recipient;

class TodoExpiredNotification implements EmailNotification, SmsNotification
{
    /** @var Todo */
    protected $todo;

    public function __construct(Todo $todo)
    {
        $this->todo = $todo;
    }

    public function toEmailMessage(Recipient $recipient): EmailMessage
    {
        return (new EmailMessage())
            ->from('noreply@example.com')
            ->subject('Todo expired')
            ->htmlBody(
                'Dear ' . $recipient->getRecipientName() . ','
                . '<br><br>'
                . 'Your todo: <b>' . $this->todo->getText() . '</b> has expired.'
            );
    }

    public function toSmsMessage(Recipient $recipient): SmsMessage
    {
        return (new SmsMessage())
            ->text('Todo: ' . $this->todo->getText() . ' has expired');
    }
}

实现接收者

namespace App\Model;

use Notifier\Recipient\Recipient;

class User implements Recipient
{
    /** @var string */
    protected $name;

    /** @var array */
    protected $contacts;

    public function __construct(string $name, array $contacts)
    {
        $this->name = $name;
        $this->contacts = $contacts;
    }
    
    public function getName(): string
    {
        return $this->name;
    }

    public function getRecipientContact(string $channel, Notification $notification): ?string
    {
        return $this->contacts[$channel] ?? null;
    }
    
    public function getRecipientName(): string
    {
        return $this->name;
    }
}

发送通知

use Notifier\Channel\Channels;
use Notifier\Channel\Email\EmailChannel;
use Notifier\Channel\Email\SimpleMailer;
use Notifier\Channel\Sms\SmsChannel;
use Notifier\Channel\Sms\TwilioTexter;
use Notifier\Notifier;
use Notifier\Recipient\Recipients;

$notifier = new Notifier(new Channels(
    new EmailChannel(new SimpleMailer()),
    new SmsChannel(new TwilioTexter('auth_id', 'auth_token'))
));

$notifier->send(
    new TodoExpiredNotification($todo), 
    new Recipients($user1, $user2, $user3)
);

致谢

许可

在 MIT 许可证下发布 - 有关详细信息,请参阅 许可文件