vendor/sonata-project/notification-bundle/Backend/PostponeRuntimeBackend.php line 78

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sonata Project package.
  4.  *
  5.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Sonata\NotificationBundle\Backend;
  11. use Sonata\NotificationBundle\Iterator\IteratorProxyMessageIterator;
  12. use Sonata\NotificationBundle\Model\MessageInterface;
  13. use Symfony\Component\EventDispatcher\Event;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use ZendDiagnostics\Result\Success;
  16. /**
  17.  * This backend postpones the handling of messages to a registered event.
  18.  *
  19.  * It's based on the asynchronous event dispatcher:
  20.  *
  21.  * @see https://gist.github.com/3852361
  22.  *
  23.  * @author Toni Uebernickel <tuebernickel@gmail.com>
  24.  */
  25. class PostponeRuntimeBackend extends RuntimeBackend
  26. {
  27.     /**
  28.      * @var MessageInterface[]
  29.      */
  30.     protected $messages = array();
  31.     /**
  32.      * If set to true, you have to fire an event the onEvent method is subscribed to manually!
  33.      *
  34.      * @var bool
  35.      */
  36.     protected $postponeOnCli false;
  37.     /**
  38.      * @param EventDispatcherInterface $dispatcher
  39.      * @param bool                     $postponeOnCli Whether to postpone the messages on the CLI, too
  40.      */
  41.     public function __construct(EventDispatcherInterface $dispatcher$postponeOnCli false)
  42.     {
  43.         parent::__construct($dispatcher);
  44.         $this->postponeOnCli $postponeOnCli;
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function publish(MessageInterface $message)
  50.     {
  51.         // if the message is generated from the cli the message is handled
  52.         // directly as there is no kernel.terminate in cli
  53.         if (!$this->postponeOnCli && $this->isCommandLineInterface()) {
  54.             $this->handle($message$this->dispatcher);
  55.             return;
  56.         }
  57.         $this->messages[] = $message;
  58.     }
  59.     /**
  60.      * Listen on any event and handle the messages.
  61.      *
  62.      * Actually, an event is not necessary, you can call this method manually, to.
  63.      * The event is not processed in any way.
  64.      *
  65.      * @param Event|null $event
  66.      */
  67.     public function onEvent(Event $event null)
  68.     {
  69.         reset($this->messages);
  70.         while (list($key$message) = each($this->messages)) {
  71.             $this->handle($message$this->dispatcher);
  72.             unset($this->messages[$key]);
  73.         }
  74.     }
  75.     /**
  76.      * {@inheritdoc}
  77.      */
  78.     public function getIterator()
  79.     {
  80.         return new IteratorProxyMessageIterator(new \ArrayIterator($this->messages));
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function getStatus()
  86.     {
  87.         return new Success('Postpone runtime backend''Ok (Postpone Runtime)');
  88.     }
  89.     /**
  90.      * Check whether this Backend is run on the CLI.
  91.      *
  92.      * @return bool
  93.      */
  94.     protected function isCommandLineInterface()
  95.     {
  96.         return 'cli' === PHP_SAPI;
  97.     }
  98. }