CvoTechnologies/Notifier plugin for CakePHP
, (*1)
Usage
Configuring notifications transports
Add the following section to your application config in app.php
., (*2)
'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., (*3)
For example: Template/Notification/irc/welcome.ctp
, (*4)
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:, (*5)
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., (*6)
It can accept configuration options that are passed from the NotificationTransport
section in your application config., (*7)
<?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;
}
}