2017 © Pedro Peláez
 

library notificator

A lightweight library to handle notifications the smart way.

image

namshi/notificator

A lightweight library to handle notifications the smart way.

  • Wednesday, June 6, 2018
  • by odino
  • Repository
  • 32 Watchers
  • 158 Stars
  • 49,713 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 22 Forks
  • 0 Open issues
  • 29 Versions
  • 3 % Grown

The README.md

NAMSHI | Notificator

Build Status, (*1)

SensioLabsInsight, (*2)

Notificator is a very simple and lightweight library to handle notifications the smart way., (*3)

It took inspiration from other libraries and patterns (Monolog and event dispatching) in order to provide a domain-driven lean notification library., (*4)

Concepts are very simple: you have a notification Manager which has a few handlers registered with it (maybe an Email handler, a Skype handler, etc.); you only have to create a notification class, define which handlers should handle it and trigger it through the manager., (*5)

It is way simpler in code than in words, check the documentation below!, (*6)

Installation

Installation can be done via composer, as the library is already on packagist., (*7)

The library uses semantic versioning for its API, so it is recommended to use a stable minor version (1.0, 1.1, etc.) and stick to it when declaring dependencies through composer:, (*8)

"namshi/notificator": "1.0.*",

Usage

Using this library is very easy thanks to the simple concept - borrowed from others - behind it: you basically have a notification manager with some handlers and then you fire (trigger()) the notification with the manager. At that point, all the handlers that need to fire that notification will take care of firing it in their context (might be an email, a skype message, etc) and tell the manager that they're done, so that the manager can forward the notification to the next handler., (*9)

``` php <?php, (*10)

// import namespaces use Namshi\Notificator\Notification\Handler\NotifySend as NotifySendHandler; use Namshi\Notificator\Manager; use Namshi\Notificator\Notification\NotifySend\NotifySendNotification;, (*11)

// create the handler $handler = new NotifySendHandler();, (*12)

// create the manager and assign the handler to it $manager = new Manager(); $manager->addHandler($handler);, (*13)

$notification = new NotifySendNotification("...whatever message...");, (*14)

// trigger the notification $manager->trigger($notification);, (*15)


This code, ran on ubuntu, will fire the notification using the `notify-send` utility: ![notify-send](http://odino.org/images/phpunit-notification-ko.png) ## The notification Manager The manager is the entity that registers all the handlers and fires the notification. You can set and add handlers very easily: ``` php <?php $handler = new MyHandler(); $handlers = array( new AnotherHandler(), new AnotherOneHandler(), ); $manager = new Manager(); // add multiple handlers $manager->setHandlers($handlers); // add a single handler $manager->addHandler($handler); // reset the handlers $manager->setHandlers(array());

Creating a new notification

Creating new notifications is very easy, as they are plain PHP classes., (*16)

They might extend the base Notification class but that is not mandatory. It is recommended, to be able to fire one notification through multiple handlers, to extend the base Notification class, and implement different interfaces that will be later checked by the handlers., (*17)

``` php <?php, (*18)

use Namshi\Notificator\Notification; use Namshi\Notificator\NotificationInterface;, (*19)

interface EchoedNotificationInterface extends NotificationInterface { public function getMessage(); }, (*20)

interface EmailNotificationInterface extends NotificationInterface { public function getAddress(); public function getSubject(); public function getBody(); }, (*21)

class DoubleNotification extends Notification implements EchoedNotificationInterface, EmailNotificationInterface { protected $address; protected $body; protected $subject;, (*22)

public function __construct($address, $subject, $body, array $parameters = array())
{
    parent::__construct($parameters);

    $this->address  = $address;
    $this->body     = $body;
    $this->subject  = $subject;
    $this->message  = $body;
}

public function getAddress()
{
    return $this->address;
}

public function getSubject()
{
    return $this->subject;
}

public function getBody()
{
    return $this->body;
}

public function getMessage()
{
    return $this->message;
}

}, (*23)


As you probably got, the above notification class is meant to be triggered via email **and** with the `echo` function (pretty useless, but gives you an idea). But the work wouldn't be over here, as you would need to implement handlers for this notification... ## Creating a new handler Let's say that we want to create the handlers that would handle the notification above, by echoing it and sending it via email: it is a matter of implementing 2 classes with a couple methods, `shouldHandle` and `handle`. Let's see how the `EchoedNotificationHandler` should look like: ``` php use Namshi\Notificator\Notification\Handler\HandlerInterface; use Namshi\Notificator\NotificationInterface; class EchoedNotificationHandler implements HandlerInterface { public function shouldHandle(NotificationInterface $notification) { return $notification instanceOf EchoedNotificationInterface; } public function handle(NotificationInterface $notification) { echo $notification->getMessage(); } }

Pretty easy, right?, (*24)

First, we need to check if this handler is handling the given notification, and that check is done by seeing if the notification implements a known interface; second, we actually trigger the notification., (*25)

The same thing needs to be done for the EmailNotificationHandler:, (*26)

``` php use Namshi\Notificator\Notification\Handler\HandlerInterface; use Namshi\Notificator\NotificationInterface;, (*27)

class EmailNotificationHandler implements HandlerInterface { public function shouldHandle(NotificationInterface $notification) { return $notification instanceOf EmailNotificationInterface; }, (*28)

public function handle(NotificationInterface $notification)
{
    mail($notification->getAddress(), $notification->getSubject(), $notification->getBody());
}

}, (*29)


If you want to stop notification propagation after an handler has triggered the notification, you just need to return `false` in the `handle` method of the handler: ``` php public function handle(NotificationInterface $notification) { // do whatever you want with the notification // ... return false; }

This will tell the manager to stop propagating the notification to other handlers., (*30)

Inside Symfony2

Namshi is currently using this library inside their Symfony2 applications., (*31)

Add the bundle to your AppKernel.php:, (*32)

     $bundles = array(
         ...
         new Namshi\Notificator\Symfony\NamshiNotificatorBundle()
     );

To register a new handler, create a service with the notification.handler tag:, (*33)

namshi.notification.handler.email:
    class: Namshi\Notificator\Notification\Handler\Emailvision
    arguments:
      client: @namshi.email_client.emailvision

    tags:
        - { name: notification.handler }

namshi.email_client.emailvision:
    class: Namshi\Emailvision\Client
    arguments:
      config:
        test_email:
          random:   AAA
          encrypt:  BBB
          uidkey:   email
          stype:    NOTHING

This configuration registers an Emailvision handler., (*34)

RabbitMQ

If you use Symfony2 and the RabbitMQBundle you can trigger notifications with this library via RabbitMQ, by using the provided consumer., (*35)

Declare the consumer as a service:, (*36)

namshi.notification.consumer:
    class: Namshi\Notificator\Messaging\RabbitMQ\Symfony2\Consumer
    arguments: [@namshi.notification.manager]

Then configure it within the RabbitMQ bundle:, (*37)

old_sound_rabbit_mq:
    consumers:
        notification:
            connection: default
            exchange_options: {name: 'notifications', type: direct}
            queue_options:    {name: 'notifications'}
            callback:         namshi.notification.consumer

And at that point you can run the consumer with:, (*38)

php app/console rabbitmq:consumer -w notification

To send notifications, the idea is that you serialize them inside the RabbitMQ messages:, (*39)

``` php $publisher = $container->get('old_sound_rabbit_mq.notifications_producer');, (*40)

$notification = new MyWhateverNotification("man, this comes from RabbitMQ and Symfony2!");, (*41)

$publisher->publish(serialize($notification));, (*42)


That's it! ## Built-in handlers We, at [Namshi](https://github.com/namshi) have developed some very simple, built-in, handlers according to our needs. Keep in mind that the main reason behind building this kind of library is the ability of triggering notification from each component of our SOA, mostly via RabbitMQ. You can take advantage of the following handlers: * `SwiftMailer`, which lets you use the amazing [SwiftMailer](http://swiftmailer.org/) to send email notifications through any SMTP server (ie. Amazon's SES, or SendGrid) * `HipChat`, which posts messages in an [HipChat](https://www.hipchat.com) room * `Emailvision`, which sends emails through the [Emailvision API](http://www.emailvision.com/) * `NotifySend`, which triggers notifications on Ubuntu * `RabbitMQ`, which triggers notifications through [RabbitMQ](http://www.rabbitmq.com/) If you have an idea for a new handler, don't hesitate with a pull request: sure, they can be implemented within your own code, but why not sharing them with the OSS ecosystem? ## Examples You can have a look at the few examples provided so far, under the `examples` directory: * [sending messages on HipChat](https://github.com/namshi/notificator/blob/master/examples/hipchat.php) * [creating a custom handler that sends an email](https://github.com/namshi/notificator/blob/master/examples/email-custom-handler.php) * [using the notify-send handler](https://github.com/namshi/notificator/blob/master/examples/notify-send.php) * [triggering a notification that is not handled by any handler](https://github.com/namshi/notificator/blob/master/examples/unhandled.php) ## Running specs In order to run the spec suite after running `composer install` do the following: ``` cli php vendor/bin/phpspec run --format=dot

The Versions

20/12 2015
08/04 2015
29/09 2014
22/08 2013

1.0.4

1.0.4.0

A lightweight library to handle notifications the smart way.

  Sources   Download

MIT

The Development Requires

19/07 2013

1.0.3

1.0.3.0

A lightweight library to handle notifications the smart way.

  Sources   Download

MIT

The Development Requires

13/07 2013

1.0.2

1.0.2.0

A lightweight library to handle notifications the smart way.

  Sources   Download

MIT

The Development Requires

10/07 2013

1.0.1

1.0.1.0

A lightweight library to handle notifications the smart way.

  Sources   Download

MIT

The Development Requires

10/07 2013

1.0.0

1.0.0.0

A lightweight library to handle notifications the smart way.

  Sources   Download

MIT

The Development Requires