1. Go to this page and download the library: Download tobento/service-notifier library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tobento / service-notifier example snippets
use Tobento\Service\Notifier\NotifierInterface;
use Tobento\Service\Notifier\Notification;
use Tobento\Service\Notifier\Recipient;
class SomeService
{
public function send(NotifierInterface $notifier): void
{
// Create a Notification that has to be sent:
// using the "mail" and "sms" channel
$notification = (new Notification(subject: 'New Invoice', channels: ['mail', 'sms']))
->content('You got a new invoice for 15 EUR.');
// The receiver of the notification:
$recipient = new Recipient(
email: '[email protected]',
phone: '15556666666',
);
// Send the notification to the recipient:
$notifier->send($notification, $recipient);
}
}
use Tobento\Service\Notifier\NotifierInterface;
use Tobento\Service\Notifier\Notifier;
use Tobento\Service\Notifier\ChannelsInterface;
use Tobento\Service\Notifier\Channels;
use Tobento\Service\Notifier\QueueHandlerInterface;
$notifier = new Notifier(
channels: new Channels(), // ChannelsInterface
// you may set a queue handler if you want to support queuing notifications:
queueHandler: null, // null|QueueHandlerInterface
// you may set an event dispatcher if you want to support events:
eventDispatcher: null, // null|EventDispatcherInterface
);
var_dump($notifier instanceof NotifierInterface);
// bool(true)
use Tobento\Service\Notifier\Notification;
$notification = new Notification(
subject: 'New Invoice',
content: 'You got a new invoice for 15 EUR.',
channels: ['mail', 'sms'],
);
use Tobento\Service\Notifier\Notification;
$notification = (new Notification())
// you may prefer using the subject method:
->subject('New Invoice')
// you may prefer using the content method:
->content('You got a new invoice for 15 EUR.')
// you may specify a name for any later usage:
->name('New Invoice');
use Tobento\Service\Notifier\Notification;
use Tobento\Service\Notifier\Message;
$notification = (new Notification(
subject: 'General subject used if no specific message',
channels: ['mail', 'sms'],
))
->addMessage('sms', new Message\Sms(
subject: 'Specific sms message',
))
// or specific sms channel:
->addMessage('sms/vonage', new Message\Sms(
subject: 'Specific sms message',
));
use Tobento\Service\Notifier\AbstractNotification;
use Tobento\Service\Notifier\RecipientInterface;
use Tobento\Service\Notifier\Message;
class OrderNotification extends AbstractNotification implements Message\ToSms
{
/**
* Create an order notification.
*
* @param Order $order
*/
public function __construct(
protected Order $order,
) {}
/**
* Returns the sms message.
*
* @param RecipientInterface $recipient
* @param string $channel The channel name.
* @return Message\SmsInterface
*/
public function toSms(RecipientInterface $recipient, string $channel, SomeService $service): Message\SmsInterface
{
return new Message\Sms(
subject: sprintf('Thanks for your order %s', $this->order->name),
);
}
}
use Tobento\Service\Notifier\Recipient;
$recipient = new Recipient(
email: '[email protected]', // null|string
phone: '15556666666', // null|string
id: 'unique-id', // null|string|int
type: 'users', // null|string
locale: 'en', // string (en default)
// you may set the channels the recipient prefers:
channels: [],
);
// you may add specific addresses:
$recipient->addAddress(
channel: 'chat/slack',
address: ['key' => 'value'] // mixed
);
use Tobento\Service\Notifier\UserRecipient;
use Tobento\Service\User\UserInterface;
$recipient = new UserRecipient(
user: $user, // UserInterface
channels: [],
);
use Tobento\Service\Notifier\Mail;
use Tobento\Service\Notifier\ChannelInterface;
use Tobento\Service\Mail\MailerInterface;
use Psr\Container\ContainerInterface;
$channel = new Mail\Channel(
name: 'mail',
mailer: $mailer, // MailerInterface
container: $container, // ContainerInterface
);
var_dump($channel instanceof ChannelInterface);
// bool(true)
use Tobento\Service\Notifier\AbstractNotification;
use Tobento\Service\Notifier\RecipientInterface;
use Tobento\Service\Notifier\Message\ToMail;
use Tobento\Service\Mail\Message;
class SampleNotification extends AbstractNotification implements ToMail
{
/**
* Returns the mail message.
*
* @param RecipientInterface $recipient
* @param string $channel The channel name.
* @return Message
*/
public function toMail(RecipientInterface $recipient, string $channel, SomeService $service): Message
{
return (new Message())
// not
use Tobento\Service\Mail\Message;
$message = (new Message())
->from('[email protected]');
use Tobento\Service\Notifier\Notification;
use Tobento\Service\Mail;
$notification = new Notification(
subject: 'New Invoice',
content: 'You got a new invoice for 15 EUR.',
);
// with specific mail message:
$notification = (new Notification())
->addMessage('mail', (new Mail\Message())
->subject('Subject')
->html('<p>Lorem Ipsum</p>')
);
use Tobento\Service\Notifier\Recipient;
use Tobento\Service\Notifier\Address;
use Tobento\Service\Notifier\Notification;
$recipient = new Recipient(
email: '[email protected]',
// or
email: new Address\Email('[email protected]', 'Name'),
);
$address = $recipient->getAddressForChannel('mail', new Notification('subject'));
var_dump($address instanceof Address\EmailInterface);
// bool(true)
use Tobento\Service\Notifier\Mail\ChannelFactory;
use Tobento\Service\Notifier\ChannelInterface;
use Tobento\Service\Mail\MailerInterface;
use Psr\Container\ContainerInterface;
$factory = new ChannelFactory(
mailer: $mailer, // MailerInterface
container: $container, // ContainerInterface
);
$channel = $factory->createChannel(name: 'mail');
// using a specific mailer:
$channel = $factory->createChannel(name: 'mail/mailchimp', config: [
'mailer' => 'mailchimp',
]);
var_dump($channel instanceof ChannelInterface);
// bool(true)
use Tobento\Service\Notifier\Symfony\ChannelAdapter;
use Tobento\Service\Notifier\ChannelInterface;
use Psr\Container\ContainerInterface;
$channel = new ChannelAdapter(
name: 'sms/vonage',
channel: new \Symfony\Component\Notifier\Channel\SmsChannel(
transport: new \Symfony\Component\Notifier\Bridge\Vonage\VonageTransport(
apiKey: '******',
apiSecret: '******',
from: 'FROM',
)
),
container: $container, // ContainerInterface
);
var_dump($channel instanceof ChannelInterface);
// bool(true)
use Tobento\Service\Notifier\AbstractNotification;
use Tobento\Service\Notifier\RecipientInterface;
use Tobento\Service\Notifier\Message;
class SampleNotification extends AbstractNotification implements Message\ToSms
{
/**
* Returns the sms message.
*
* @param RecipientInterface $recipient
* @param string $channel The channel name.
* @return Message\SmsInterface
*/
public function toSms(RecipientInterface $recipient, string $channel): Message\SmsInterface
{
return new Message\Sms(
subject: 'Sms message',
);
// you may set a specific to address:
return new Message\Sms(
subject: 'Sms message',
to: $recipient->getAddressForChannel('sms/vonage'),
);
}
}
use Tobento\Service\Notifier\Notification;
use Tobento\Service\Notifier\Message;
$notification = new Notification(
subject: 'New Invoice',
content: 'You got a new invoice for 15 EUR.',
);
// with specific sms message:
$notification = (new Notification())
->addMessage('sms', new Message\Sms(
subject: 'Sms message',
));
use Tobento\Service\Notifier\Recipient;
use Tobento\Service\Notifier\Address;
use Tobento\Service\Notifier\Notification;
$recipient = new Recipient(
phone: '15556666666',
// or
phone: new Address\Phone('15556666666', 'Name'),
);
$address = $recipient->getAddressForChannel('sms', new Notification('subject'));
var_dump($address instanceof Address\PhoneInterface);
// bool(true)
use Tobento\Service\Notifier\Symfony\ChannelAdapter;
use Tobento\Service\Notifier\ChannelInterface;
use Psr\Container\ContainerInterface;
$channel = new ChannelAdapter(
name: 'chat/slack',
channel: new \Symfony\Component\Notifier\Channel\ChatChannel(
transport: new \Symfony\Component\Notifier\Bridge\Slack\SlackTransport(
accessToken: '******',
)
),
container: $container, // ContainerInterface
);
var_dump($channel instanceof ChannelInterface);
// bool(true)
use Tobento\Service\Notifier\AbstractNotification;
use Tobento\Service\Notifier\RecipientInterface;
use Tobento\Service\Notifier\Message;
use Tobento\Service\Notifier\Symfony\MessageOptions;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
class SampleNotification extends AbstractNotification implements Message\ToChat
{
/**
* Returns the chat message.
*
* @param RecipientInterface $recipient
* @param string $channel The channel name.
* @return Message\ChatInterface
*/
public function toChat(RecipientInterface $recipient, string $channel): Message\ChatInterface
{
if ($channel === 'chat/slack') {
// you may set message options:
$options = new SlackOptions();
return new (Message\Chat('Chat message'))
->parameter(new MessageOptions($options));
}
// for any other chat channel:
return new Message\Chat(
subject: 'Chat message',
);
}
}
use Tobento\Service\Notifier\Notification;
use Tobento\Service\Notifier\Message;
$notification = new Notification(
subject: 'New Invoice',
content: 'You got a new invoice for 15 EUR.',
);
// with specific chat message:
$notification = (new Notification())
->addMessage('chat/slack', new Message\Chat(
subject: 'Chat message',
));
use Tobento\Service\Notifier\Recipient;
use Tobento\Service\Notifier\Address;
use Tobento\Service\Notifier\Notification;
$recipient = (new Recipient())
->addAddress('chat/slack', ['channel' => 'name']);
$address = $recipient->getAddressForChannel('chat/slack', new Notification('subject'));
var_dump($address);
// array(1) { ["channel"]=> string(4) "name" }
use Tobento\Service\Notifier\AbstractNotification;
use Tobento\Service\Notifier\RecipientInterface;
use Tobento\Service\Notifier\Message;
use Tobento\Service\Notifier\Symfony\MessageOptions;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
class SampleNotification extends AbstractNotification implements Message\ToChat
{
/**
* Returns the chat message.
*
* @param RecipientInterface $recipient
* @param string $channel The channel name.
* @return Message\ChatInterface
*/
public function toChat(RecipientInterface $recipient, string $channel): Message\ChatInterface
{
if ($channel === 'chat/slack') {
$address = $recipient->getAddressForChannel('chat/slack', $this);
$options = new SlackOptions([
'recipient_id' => $address['channel'] ?? null,
]);
return new (Message\Chat('Chat message'))
->parameter(new MessageOptions($options));
}
// for any other chat channel:
return new Message\Chat(
subject: 'Chat message',
);
}
}
use Tobento\Service\Notifier\Symfony\ChannelAdapter;
use Tobento\Service\Notifier\ChannelInterface;
use Psr\Container\ContainerInterface;
$channel = new ChannelAdapter(
name: 'push/one-signal',
channel: new \Symfony\Component\Notifier\Channel\ChatChannel(
transport: new \Symfony\Component\Notifier\Bridge\OneSignal\OneSignalTransport(
appId: '******',
apiKey: '******',
)
),
container: $container, // ContainerInterface
);
var_dump($channel instanceof ChannelInterface);
// bool(true)
use Tobento\Service\Notifier\AbstractNotification;
use Tobento\Service\Notifier\RecipientInterface;
use Tobento\Service\Notifier\Message;
use Tobento\Service\Notifier\Symfony\MessageOptions;
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalOptions;
class SampleNotification extends AbstractNotification implements Message\ToPush
{
/**
* Returns the push message.
*
* @param RecipientInterface $recipient
* @param string $channel The channel name.
* @return Message\PushInterface
*/
public function toPush(RecipientInterface $recipient, string $channel): Message\PushInterface
{
if ($channel === 'push/one-signal') {
// you may set message options:
$options = new OneSignalOptions([]);
return new (Message\Push('Push subject'))
->content('Push content')
->parameter(new MessageOptions($options));
}
// for any other push channel:
return new Message\Push(
subject: 'Push subject',
content: 'Push content',
);
}
}
use Tobento\Service\Notifier\Notification;
use Tobento\Service\Notifier\Message;
$notification = new Notification(
subject: 'New Invoice',
content: 'You got a new invoice for 15 EUR.',
);
// with specific chat message:
$notification = (new Notification())
->addMessage('push/one-signal', new Message\Push(
subject: 'Push subject',
content: 'Push content',
));
use Tobento\Service\Notifier\Recipient;
use Tobento\Service\Notifier\Address;
use Tobento\Service\Notifier\Notification;
$recipient = (new Recipient())
->addAddress('push/one-signal', ['recipient_id' => 'id']);
$address = $recipient->getAddressForChannel('push/one-signal', new Notification('subject'));
var_dump($address);
// array(1) { ["recipient_id"]=> string(2) "id" }
use Tobento\Service\Notifier\AbstractNotification;
use Tobento\Service\Notifier\RecipientInterface;
use Tobento\Service\Notifier\Message;
use Tobento\Service\Notifier\Symfony\MessageOptions;
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalOptions;
class SampleNotification extends AbstractNotification implements Message\ToPush
{
/**
* Returns the push message.
*
* @param RecipientInterface $recipient
* @param string $channel The channel name.
* @return Message\PushInterface
*/
public function toPush(RecipientInterface $recipient, string $channel): Message\PushInterface
{
if ($channel === 'push/one-signal') {
$address = $recipient->getAddressForChannel('push/one-signal', $this);
$options = new OneSignalOptions([
'recipient_id' => $address['recipient_id'] ?? null,
]);
return new (Message\Push('Push subject'))
->content('Push content')
->parameter(new MessageOptions($options));
}
// for any other push channel:
return new Message\Push(
subject: 'Push subject',
content: 'Push content',
);
}
}
use Tobento\Service\Notifier\Storage;
use Tobento\Service\Notifier\ChannelInterface;
use Tobento\Service\Repository\RepositoryInterface;
use Psr\Container\ContainerInterface;
$channel = new Storage\Channel(
name: 'storage/database',
repository: $repository, // RepositoryInterface
container: $container, // ContainerInterface
);
var_dump($channel instanceof ChannelInterface);
// bool(true)
use Tobento\Service\Notifier\Storage;
use Tobento\Service\Notifier\ChannelInterface;
use Tobento\Service\Repository\RepositoryInterface;
use Tobento\Service\Storage\StorageInterface;
use Psr\Container\ContainerInterface;
$channel = new Storage\Channel(
name: 'storage/database',
repository: new StorageRepository(
storage: $storage, // StorageInterface
table: 'notifications',
),
container: $container, // ContainerInterface
);
use Tobento\Service\Notifier\AbstractNotification;
use Tobento\Service\Notifier\RecipientInterface;
use Tobento\Service\Notifier\Message;
class SampleNotification extends AbstractNotification implements Message\ToStorage
{
/**
* Returns the storage message.
*
* @param RecipientInterface $recipient
* @param string $channel The channel name.
* @return Message\StorageInterface
*/
public function toStorage(RecipientInterface $recipient, string $channel): Message\StorageInterface
{
return new Message\Storage(data: [
'order_id' => $this->order->id,
]);
}
}
use Tobento\Service\Notifier\Notification;
use Tobento\Service\Notifier\Message;
$notification = new Notification(
subject: 'New Invoice',
content: 'You got a new invoice for 15 EUR.',
);
// with specific storage message:
$notification = (new Notification())
->addMessage('storage', new Message\Storage([
'foo' => 'bar',
]));
// channel will store on sending:
$repository->create([
'name' => $notification->getName(),
'recipient_id' => $recipient->getId(),
'recipient_type' => $recipient->getType(),
'data' => $message->getData(),
'read_at' => null,
'created_at' => null,
]);
use Tobento\Service\Notifier\Channels;
use Tobento\Service\Notifier\ChannelsInterface;
use Tobento\Service\Notifier\ChannelInterface;
$channels = new Channels(
$channel, // ChannelInterface
$anotherChannel, // ChannelInterface
);
var_dump($channels instanceof ChannelsInterface);
// bool(true)
use Tobento\Service\Notifier\LazyQueues;
use Tobento\Service\Notifier\ChannelsInterface;
use Tobento\Service\Notifier\ChannelInterface;
use Tobento\Service\Notifier\ChannelFactoryInterface;
use Tobento\Service\Notifier\Symfony;
use Psr\Container\ContainerInterface;
$channels = new LazyChannels(
container: $container, // ContainerInterface
channels: [
// using a factory:
'sms' => [
// factory must implement ChannelFactoryInterface
'factory' => Symfony\ChannelFactory::class,
'config' => [
'dsn' => 'vonage://KEY:SECRET@default?from=FROM',
'channel' => \Symfony\Component\Notifier\Channel\SmsChannel::class,
],
],
// using a closure:
'mail' => static function (string $name, ContainerInterface $c): ChannelInterface {
// create channel ...
return $channel;
},
// or you may sometimes just create the channel (not lazy):
'sms/null' => new NullChannel(name: 'sms'),
],
);
var_dump($channels instanceof ChannelsInterface);
// bool(true)
use Tobento\Service\Notifier\Notification;
use Tobento\Service\Notifier\Parameter\Queue;
$notification = (new Notification())
->parameter(new Queue(
// you may specify the queue to be used:
name: 'secondary',
// you may specify a delay in seconds:
delay: 30,
// you may specify how many times to retry:
retry: 3,
// you may specify a priority:
priority: 100,
// you may specify if you want to encrypt the message:
encrypt: true,
));
composer
use Tobento\Service\Notifier\NotifierInterface;
use Tobento\Service\Notifier\Notifier;
use Tobento\Service\Notifier\ChannelsInterface;
use Tobento\Service\Notifier\Channels;
use Tobento\Service\Notifier\Queue\QueueHandler;
use Tobento\Service\Queue\QueueInterface;
$notifier = new Notifier(
channels: new Channels(), // ChannelsInterface
// set a queue handler:
queueHandler: new QueueHandler(
queue: $queue, // QueueInterface
// you may define the default queue used if no specific is defined on the notification.
queueName: 'mails', // null|string
),
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.