cvo-technologies/cakephp-notifier

CvoTechnologies/Notifier plugin for CakePHP

1.0.0 2016-08-08 11:52 UTC

This package is not auto-updated.

Last update: 2024-04-27 17:31:39 UTC


README

Software License Build Status Coverage Status Total Downloads Latest Stable Version

Usage

Configuring notifications transports

Add the following section to your application config in app.php.

    'NotificationTransport' => [
        'email' => [
            'className' => 'CvoTechnologies/Notifier.Email',
            'profile' => 'default',
        ],
        'irc' => [
            'className' => 'Irc',
            'channel' => '#cvo-technlogies'
        ],
        'twitter' => [
            'className' => 'CvoTechnologies/Twitter.Twitter',
        ],
        'example' => [
            'className' => 'Example',
            'someOption' => true
        ]
    ],

Creating a notifier

namespace App\Notifier;

use CvoTechnologies\Notifier\Notifier;

class UserNotifier extends Notifier
{
    public function welcome($user)
    {
        $this
            ->to([
                'irc' => $user->irc_nickname,
                'twitter' => $user->twitter_nickname
            ])
            ->subject(sprintf('Welcome %s', $user->name))
            ->template('welcome_message') // By default template with same name as method name is used.
            ->viewVars([
                'user' => $user
            ])
            ->transports([
                'irc',
                'twitter'
            ]);
    }
}

Creating notification template

Create a template file in Template/Notification/transport-type. This will be used as template for the notification.

For example: Template/Notification/irc/welcome.ctp

Welcome <?= $user->name; ?> to our website!

Using it

Using the notifier is very easy. Here's an example on how to use it in a controller:

namespace App\Controller;

use CvoTechnologies\Notifier\NotifierAwareTrait;

class UsersController extends AppController
{
    use NotifierAwareTrait;

    public function register()
    {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->data())
            if ($this->Users->save($user)) {
                $this->getNotifier('User')->send('welcome', [$user]);
            }
        }
        $this->set('user', $user);
    }
}

Creating a transport

A transport is used to talk to a particular service.

It can accept configuration options that are passed from the NotificationTransport section in your application config.

<?php

namespace App\Notifier\Transport;

use CvoTechnologies\Notifier\AbstractTransport;

class ExampleTransport extends AbstractTransport
{
    const TYPE = 'example';

    /**
     * Send notification.
     *
     * @param \CvoTechnologies\Notifier\Notification $notification Notification instance.
     * @return array
     */
    public function send(Notification $notification)
    {
        // Send notificaiton
        $result = NotificationSendingService::send($notification->message(static::TYPE));

        return (array)$result;
    }
}