nikolaposa/notify

该软件包已被废弃,不再维护。作者建议使用nikolaposa/notifier软件包。

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

4.0.0 2020-04-05 09:46 UTC

This package is auto-updated.

Last update: 2020-04-11 20:50:15 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接口扩展了Notification接口本身,因此您不需要显式实现它。

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

  1. 特定渠道的Notification接口,
  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许可证下发布 - 请参阅许可证文件以获取详细信息。