PHP code example of shippinno / notification

1. Go to this page and download the library: Download shippinno/notification 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/ */

    

shippinno / notification example snippets


use Shippinno\Email\SwiftMailer\SwiftMailerSendEmail;
use Shippinno\Notification\Domain\Model\Notification;
use Shippinno\Notification\Domain\Model\NotificationNotSentException;
use Shippinno\Notification\Infrastructure\Domain\Model\EmailGateway;

$notification = new Notification(
    new EmailDestination(
        [new EmailAddress('[email protected]')],
    ),
    new Subject('Hello'),
    new Body('This is a notification.'),
    
);

$gateway = new EmailGateway(
    new SwiftMailerSendEmail(...),
    new EmailAddress('[email protected]')
);

try {
    $gateway->send($notification);
} catch (NotificationNotSentException $e) {
    // ...
}

$repository = new DoctrineNotificationRepository($em, $class, true); // $isPrecocious === true
$repository->add($notification); // Already flushed.

$notifications = $repository->freshNotifications();

try {
    $gateway->send($notification);
    $notification->markSent();
} catch (NotificationNotSentException $e) {
    $notification->markFailed($e->__toString()); // mark it failed with the reason
} finally {
    $repository->persist($notification);
}

use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Shippinno\Template\Liquid;
use Shippinno\Notification\Domain\Model\TemplateNotificationFactory;

$template = new Liquid(new Filesystem(new Local('/templates')));
$factory = new TemplateNotificationFactory($template);
$notification = $factory->create(
    'hello', // template name
    ['you' => 'Shippinno', 'her' => 'Jessica']), // variables for the template
    new EmailDestination([new EmailAddress('[email protected]')])
);
$notification->subject()->subject(); // => 'Hello Shippinno !!'
$notification->body()->body(); // => 'Good bye Jessica :)'

use Shippinno\Notification\Domain\Model\SendNotification;
use Shippinno\Notification\Domain\Model\GatewayRegistry;

$gatewayRegistry = new GatewayRegistry;
$gatewayRegistry->set('EmailDestination', new EmailGateway(...));
$gatewayRegistry->set('SlackChannelDestination', new SlackGateway(...));

$emailNotification = new Notification(new EmailDestination(...), ...);
$slackChannelNotification = new Notification(new SlackChannelDestination(...), ...);

$sendNotifiation = new SendNotification($gatewayRegistry);
$sendNotification->execute($emailNotification); // will send an email
$sendNotification->execute($slackChannelNotification); // will send a message to the Slack channel