2017 © Pedro Peláez
 

library notify

Abstractions for modeling and implementing application notifications functionality

image

nikolaposa/notify

Abstractions for modeling and implementing application notifications functionality

  • Saturday, June 17, 2017
  • by nikolaposa
  • Repository
  • 3 Watchers
  • 7 Stars
  • 77 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 16 Versions
  • 22 % Grown

The README.md

Notifier

![Build Status][ico-build] ![Code Quality][ico-code-quality] Code Coverage Latest Version PDS Skeleton, (*1)

Extensible library for building notifications and sending them via different delivery channels., (*2)

Installation

The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json:, (*3)

composer require nikolaposa/notifier

Theory of operation

Notifications are informative messages that are sent through different channels (i.e. email, SMS, mobile push) to notify users about certain events in the application. Notification is a higher-level abstraction, a concept that encapsulates a subject to be notified to the recipient, regardless of delivery channels through which that information can be communicated. From an architectural standpoint, notification is a domain concern., (*4)

In order to minimize the coupling of your domain with the infrastructure for sending notifications, Notifier library was based on on unobtrusive interfaces that should be implemented by your objects in order to plug them into the workflow of the library. Those are:, (*5)

  1. Notification - marks the object as a notification that can be used with the Notifier library,
  2. Recipient - represents the recipient of the notification which provides contact (i.e. email address, phone number) for a certain channel; typically implemented by a User domain object.

For each channel through which Notification is supposed to be sent, Notification class should implement channel-specific interface, making the Notification suitable for sending via a specific channel. These interfaces declare message building methods, for example EmailNotification::toEmailMessage(), that convert the notification to a message sent by that particular channel. Channel-specific Notification interfaces extend the Notification interface itself, so you do not need to implement it explicitly., (*6)

Channel component captures implementation details of how a Notification is sent via certain delivery channels. Specific channel implementation typically consists of:, (*7)

  1. channel-specific Notification interface,
  2. Message class - transport-level message to which Notification gets converted,
  3. Channel implementation responsible for the very act of sending the Notification.

Out of the box, this library features facilities for sending notifications via email and SMS. The highly extensible design allows for implementing custom delivery channels., (*8)

Finally, Notifier service is a facade that manages the entire process of sending a Notification to a list of Recipients via supported channels. It is the only service of this library that the calling code is supposed to interact with directly., (*9)

Usage

Creating Notifications, (*10)

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');
    }
}

Implementing Recipient, (*11)

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;
    }
}

Sending Notifications, (*12)

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)
);

Credits

License

Released under MIT License - see the License File for details., (*13)

The Versions

17/06 2017

dev-master

9999999-dev

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

email sms notification notify

25/03 2017

3.0.x-dev

3.0.9999999.9999999-dev

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

email sms notification notify

01/01 2017

3.0.1

3.0.1.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

06/09 2016

3.0.0

3.0.0.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

11/07 2016

2.2.x-dev

2.2.9999999.9999999-dev

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

24/05 2016

2.2.0

2.2.0.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

02/05 2016

2.1.2

2.1.2.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

25/04 2016

2.1.1

2.1.1.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

24/04 2016

2.1.0

2.1.0.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

17/04 2016

2.0.1

2.0.1.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

17/04 2016

2.0.0

2.0.0.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

16/04 2016

1.2.1

1.2.1.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

16/04 2016

1.2.0

1.2.0.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

15/04 2016

1.1.0

1.1.0.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

14/04 2016

1.0.0

1.0.0.0

Abstractions for modeling and implementing application notifications functionality

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

10/04 2016

1.0.0-beta

1.0.0.0-beta

  Sources   Download

The Requires

  • php >=5.6

 

The Development Requires