Notification

Library which helps you to send notification (via different channels) to user.

View the Project on GitHub FunkyMonkeyLabs/notification

What's that?

FML/Notification lib allows to send notification through different channels during application runtime. Library can collect many messages to many users and send it to them once, when application is going down.

Instalation

composer

$ php composer.phar require fml/notification:1.5.*

Usage

By default there is a one channel - mail channel. It requires mailer service (Swift_Mailer). If you want to send notification to user Patryk with e-mail address contact@funkymonkeylabs.pl, with message "Hello Patryk", you have to write:

<?php
// index.php
require_once __DIR__.'/vendor/autoload.php';

$channel = new \FML\Notification\Channel\Channel\SwiftMailerMailChannel();
$messageManager = new \FML\Notification\MessageManager();
$messageManager->addChannel($channel);

$recipient = new \FML\Notification\Recipient\Recipient();
$recipient
    ->setAddress('contact@funkymonkeylabs.pl')
    ->setName('Patryk');

$message = new \FML\Notification\Example\Message\PlainMessage();
$message
    ->addRecipient($recipient);

$messageManager->addMessage($message);
$messageManager->flush();

Creating new channels

You can also create your own channel (for example if you are going to send SMS messages).

// src/Acme/Channel/SmsChannel.php
namespace Acme\Channel;

use FML\Notification\Channel\AbstractChannel;
yse Acme\Provider\SmsProvider

class SmsChannel extends AbstractChannel
{
    public static $channelName = 'sms';

    /**
     * @param SmsProvider
     */
    private $smsProvider;

    public function __construct($smsProvider)
    {
        $this->name = self::$channelName;
        $this->provider = $smsProvider;
    }

    /**
     * send notifications by sms provider
     * @return string
     */
    public function flush()
    {
        foreach ($this->messages as $message) {
            $recipients = $message->getRecipients();
            foreach($message->getRecipients() as $recipient) {
                $this->provider->send(
                    $recipient->getAddress(), // it can be phone number for example
                    $message->getContent()
                );
            }
        }
    }
}

next, you have to create notification that can be send by SmsChannel:

<?php
// src/Acme/Message/SmsMessage.php
namespace Acme\Message;

use FML\Notification\Message\Message;

class SmsMessage extends Message
{
    public function __construct()
    {
        $this->channel = 'sms';
    }

    /**
     * @return string
     */
    public function getContent()
    {
        return $content; // simple return given content
    }
}

last step is similar to first example:

<?php
// index.php
require_once __DIR__.'/vendor/autoload.php';
$provider = new Acme\Provider\SmsProvider(array(...)); // create new instance of sms provider with some configuration
$smsChannel = new Acme\Channel\SmsChannel($provider);
$mailChannel = new \FML\Notification\Channel\Channel\SwiftMailerMailChannel();

$messageManager = new \FML\Notification\MessageManager();
$messageManager
    ->addChannel($mailChannel)
    ->addChannel($smsChannel);

$recipient = new \FML\Notification\Recipient\Recipient();
$recipient
    ->setAddress('contact@funkymonkeylabs.pl')
    ->setName('Patryk');

// mail message
$message = new \FML\Notification\Example\Message\PlainMessage();
$message
    ->addRecipient($recipient);

// sms message
$smsRecipient = new \FML\Notification\Recipient\Recipient();
$smsRecipient
    ->setAddress('883-211-446'); //FunkyMonkeyLabs phone number! Yeah!

$smsMessage = new \Acme\Message\SmsMessage();
$smsMessage
    ->addRecipient($recipient)
    ->setContent("Hello from Mars!");

$messageManager
    ->addMessage($message)
    ->addMessage($smsMessage);

$messageManager->flush();

That's it! You just sent two messages. Email + sms. So simple, right?

Authors and Contributors

@abdulklarapl Patryk Szlagowski

@shadowhand Woody Gilk

Support or Contact

Having trouble with library? Check https://github.com/FunkyMonkeyLabs/notification and give us some info! You can create issue as well.