planmylife / notification-bundle
Bundle to manage notification system
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 346
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Type:symfony-bundle
Requires
- php: ^5.4|^7.0
- jms/serializer-bundle: ^1.1
- symfony/event-dispatcher: ~3.0
This package is not auto-updated.
Last update: 2024-06-08 17:49:06 UTC
README
The notification bundle was created for the planmylife.io to manage all notifications. Have fun with it.
Installation
Step 1: Download the Bundle
composer require planmylife/notification-bundle
This command requires you to have Composer installed globally, as explained in the Composer documentation.
Step 2: Enable the Bundle
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new PlanMyLife\NotificationBundle\PlanMyLifeNotificationBundle(), ); } // ... }
Step 3: Configure factory and builder
# app/config/routing.yml pml_notification: factory: '@notification.factory' builders: std: class: stdClass builder: PlanMyLife\NotificationBundle\Builder\StdNotificationBuilder user: class: Acme\MainBundle\Entity\User builder: Acme\MainBundle\Builder\UserNotificationBuilder
Step 4: Add your builders classes
<?php namespace Acme\MainBundle\Builder; use Acme\MainBundle\Entity\User; use PlanMyLife\NotificationBundle\Model\Notification; class UserNotificationBuilder implements NotificationBuilderInterface { /** * @param User $target * @param mixed $type * @return Notification */ public function build($target, $type = null) { $notification = new Notification(); $notification->setTitle($target->getName()); $notification->setContent('user notification content example'); $notification->setType('[add type here]'); // log, notice, info, warning, error, critical $notification->setDate($target->getCreatedAt()); $notification->setParams([ 'email' => $target->getEmail(), # Params for mailer manager 'username' => $target->getUsername(), # Params for mailer/slack manager 'channel' => 'notification' # Params for slack manager 'template' => "Acme:Notification:$type.txt.twig" ]); return $notification; } }
Additionnal manager
Step 1 : Define Manager class
<?php namespace PlanMyLife\NotificationBundle\Manager; use PlanMyLife\NotificationBundle\Model\Notification; use Symfony\Component\HttpFoundation\Session\Session; class FlashBagNotificationManager implements NotificationManagerInterface { /** @var Session */ protected $session; /** * FlashBagNotificationManager constructor. * @param Session $session */ public function __construct(Session $session) { $this->session = $session; } public function manage(Notification $notification) { $this->session->getFlashBag()->add($notification->getType(), $notification->getContent()); } }
Step 2 : Define your manager service
services: notification.manager.flash_bag: class: PlanMyLife\NotificationBundle\Manager\FlashBagNotificationManager arguments: - "@session"
Step 3 : Define your listener
<?php namespace PlanMyLife\NotificationBundle\EventListener; class FlashBagNotificationListener extends NotificationListener implements NotificationListenerInterface { public function getName() { return 'flash_bag'; } }
Step 4 : Bind your listener
services: notification.listener.flash_bag: class: PlanMyLife\NotificationBundle\EventListener\FlashBagNotificationListener arguments: - "%pml_notification.factory%" - "@notification.manager.flash_bag" tags: - { name: notification.event_subscriber, event: pml.notification }
Step 5 : Test your notification
$object = new stdClass(); $object->title = 'Test title'; $object->content = 'Test Content'; $object->type = 'success'; $object->date = new \DateTime(); $object->params = []; $this->get('notification.service')->notify($object, ['flash_bag'])