diff --git a/EventDispatcher/Event/PostFilterEvent.php b/EventDispatcher/Event/PostFilterEvent.php new file mode 100644 index 00000000..9715e753 --- /dev/null +++ b/EventDispatcher/Event/PostFilterEvent.php @@ -0,0 +1,37 @@ +envelopes = $envelopes; + } + + /** + * @return Envelope[] + */ + public function getEnvelopes() + { + return $this->envelopes; + } + + /** + * @param Envelope[] $envelopes + */ + public function setEnvelopes($envelopes) + { + $this->envelopes = $envelopes; + } +} diff --git a/EventDispatcher/Event/EnvelopesEvent.php b/EventDispatcher/Event/PostFlushEvent.php similarity index 95% rename from EventDispatcher/Event/EnvelopesEvent.php rename to EventDispatcher/Event/PostFlushEvent.php index 03f34171..cd1504af 100644 --- a/EventDispatcher/Event/EnvelopesEvent.php +++ b/EventDispatcher/Event/PostFlushEvent.php @@ -4,7 +4,7 @@ namespace Flasher\Prime\EventDispatcher\Event; use Flasher\Prime\Envelope; -final class EnvelopesEvent +final class PostFlushEvent { /** * @var Envelope[] diff --git a/EventDispatcher/Event/PreFilterEvent.php b/EventDispatcher/Event/PreFilterEvent.php new file mode 100644 index 00000000..2d3dec0d --- /dev/null +++ b/EventDispatcher/Event/PreFilterEvent.php @@ -0,0 +1,37 @@ +envelopes = $envelopes; + } + + /** + * @return Envelope[] + */ + public function getEnvelopes() + { + return $this->envelopes; + } + + /** + * @param Envelope[] $envelopes + */ + public function setEnvelopes($envelopes) + { + $this->envelopes = $envelopes; + } +} diff --git a/EventDispatcher/Event/PreFlushEvent.php b/EventDispatcher/Event/PreFlushEvent.php new file mode 100644 index 00000000..1a9ec083 --- /dev/null +++ b/EventDispatcher/Event/PreFlushEvent.php @@ -0,0 +1,37 @@ +envelopes = $envelopes; + } + + /** + * @return Envelope[] + */ + public function getEnvelopes() + { + return $this->envelopes; + } + + /** + * @param Envelope[] $envelopes + */ + public function setEnvelopes($envelopes) + { + $this->envelopes = $envelopes; + } +} diff --git a/EventDispatcher/EventDispatcher.php b/EventDispatcher/EventDispatcher.php index 5f40a1e8..874de6b6 100644 --- a/EventDispatcher/EventDispatcher.php +++ b/EventDispatcher/EventDispatcher.php @@ -81,7 +81,7 @@ final class EventDispatcher implements EventDispatcherInterface */ public function addSubscriber(EventSubscriberInterface $subscriber) { - foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { + foreach ((array) $subscriber->getSubscribedEvents() as $eventName => $params) { if (\is_string($params)) { $this->addListener($eventName, array($subscriber, $params)); } elseif (\is_string($params[0])) { diff --git a/EventDispatcher/EventSubscriber/EventSubscriberInterface.php b/EventDispatcher/EventSubscriber/EventSubscriberInterface.php index ce37c9d5..1c136437 100644 --- a/EventDispatcher/EventSubscriber/EventSubscriberInterface.php +++ b/EventDispatcher/EventSubscriber/EventSubscriberInterface.php @@ -5,7 +5,7 @@ namespace Flasher\Prime\EventDispatcher\EventSubscriber; interface EventSubscriberInterface { /** - * @return array + * @return string|array */ public static function getSubscribedEvents(); } diff --git a/EventDispatcher/EventSubscriber/FilterEnvelopesByHopsListener.php b/EventDispatcher/EventSubscriber/FilterEnvelopesByHopsListener.php index ef3539e9..23c99a01 100644 --- a/EventDispatcher/EventSubscriber/FilterEnvelopesByHopsListener.php +++ b/EventDispatcher/EventSubscriber/FilterEnvelopesByHopsListener.php @@ -2,17 +2,17 @@ namespace Flasher\Prime\EventDispatcher\EventSubscriber; -use Flasher\Prime\EventDispatcher\Event\EnvelopesEvent; +use Flasher\Prime\EventDispatcher\Event\PostFilterEvent; use Flasher\Prime\Envelope; final class FilterEnvelopesByHopsListener implements EventSubscriberInterface { /** - * @param EnvelopesEvent $event + * @param PostFilterEvent $event * * @return Envelope[] */ - public function __invoke(EnvelopesEvent $event) + public function __invoke(PostFilterEvent $event) { $envelopes = $event->getEnvelopes(); @@ -28,10 +28,11 @@ final class FilterEnvelopesByHopsListener implements EventSubscriberInterface $event->setEnvelopes($envelopes); } + /** + * @inheritDoc + */ public static function getSubscribedEvents() { - return array( - 'Flasher\Prime\EventDispatcher\Event\EnvelopesEvent' - ); + return 'Flasher\Prime\EventDispatcher\Event\PostFilterEvent'; } } diff --git a/EventDispatcher/EventSubscriber/RemoveRenderedEnvelopesSubscriber.php b/EventDispatcher/EventSubscriber/RemoveRenderedEnvelopesSubscriber.php new file mode 100644 index 00000000..e5ea6a0a --- /dev/null +++ b/EventDispatcher/EventSubscriber/RemoveRenderedEnvelopesSubscriber.php @@ -0,0 +1,49 @@ +storage = $storage; + } + + /** + * @param PostFlushEvent $event + */ + public function __invoke(PostFlushEvent $event) + { + foreach ($event->getEnvelopes() as $envelope) { + $replayStamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp'); + $replayCount = null === $replayStamp ? 0 : $replayStamp->getAmount() - 1; + + if (1 > $replayCount) { + continue; + } + + $envelope->with(new HopsStamp($replayCount)); + $this->storage->add($envelope); + } + } + + /** + * @inheritDoc + */ + public static function getSubscribedEvents() + { + return 'Flasher\Prime\EventDispatcher\Event\PostFlushEvent'; + } +} diff --git a/Factory/AbstractFlasher.php b/Factory/AbstractFlasher.php index b07c10ef..7ddc4fc1 100644 --- a/Factory/AbstractFlasher.php +++ b/Factory/AbstractFlasher.php @@ -13,6 +13,9 @@ use Flasher\Prime\Notification\NotificationInterface; * @method NotificationBuilderInterface options($options) * @method NotificationBuilderInterface setOption($name, $value) * @method NotificationBuilderInterface unsetOption($name) + * @method NotificationBuilderInterface priority($priority) + * @method NotificationBuilderInterface hops($amount) + * @method NotificationBuilderInterface keep() * @method NotificationBuilderInterface success($message = null, array $options = array()) * @method NotificationBuilderInterface error($message = null, array $options = array()) * @method NotificationBuilderInterface info($message = null, array $options = array()) diff --git a/Presenter/AbstractPresenter.php b/Presenter/AbstractPresenter.php index dde4b9c0..bf37e041 100644 --- a/Presenter/AbstractPresenter.php +++ b/Presenter/AbstractPresenter.php @@ -3,9 +3,9 @@ namespace Flasher\Prime\Presenter; use Flasher\Prime\Config\ConfigInterface; -use Flasher\Prime\EventDispatcher\Event\EnvelopesEvent; -use Flasher\Prime\EventDispatcher\EventDispatcherInterface; use Flasher\Prime\Envelope; +use Flasher\Prime\EventDispatcher\Event\PostFilterEvent; +use Flasher\Prime\EventDispatcher\EventDispatcherInterface; use Flasher\Prime\Filter\FilterManager; use Flasher\Prime\Renderer\HasOptionsInterface; use Flasher\Prime\Renderer\HasScriptsInterface; @@ -71,12 +71,24 @@ abstract class AbstractPresenter implements PresenterInterface return get_class($this) === $name; } + /** + * @param string $filterName + * @param array $criteria + * + * @return Envelope[] + */ protected function getEnvelopes($filterName, $criteria = array()) { - $filter = $this->filterManager->make($filterName); - $envelopes = $filter->filter($this->storage->all(), $criteria); + $filter = $this->filterManager->make($filterName); - $event = new EnvelopesEvent($envelopes); + $envelopes = $this->storage->all(); + + $event = new PostFilterEvent($envelopes); + $this->eventDispatcher->dispatch($event); + + $envelopes = $filter->filter($event->getEnvelopes(), $criteria); + + $event = new PostFilterEvent($envelopes); $this->eventDispatcher->dispatch($event); return $event->getEnvelopes(); diff --git a/Storage/StorageManager.php b/Storage/StorageManager.php index cf3bcc65..4ecb6013 100644 --- a/Storage/StorageManager.php +++ b/Storage/StorageManager.php @@ -3,6 +3,9 @@ namespace Flasher\Prime\Storage; use Flasher\Prime\Envelope; +use Flasher\Prime\EventDispatcher\Event\PostFlushEvent; +use Flasher\Prime\EventDispatcher\Event\PreFlushEvent; +use Flasher\Prime\EventDispatcher\EventDispatcherInterface; use Flasher\Prime\Stamp\HopsStamp; final class StorageManager implements StorageManagerInterface @@ -13,11 +16,18 @@ final class StorageManager implements StorageManagerInterface private $storage; /** - * @param StorageInterface $storage + * @var EventDispatcherInterface */ - public function __construct(StorageInterface $storage) + private $eventDispatcher; + + /** + * @param StorageInterface $storage + * @param EventDispatcherInterface $eventDispatcher + */ + public function __construct(StorageInterface $storage, EventDispatcherInterface $eventDispatcher) { $this->storage = $storage; + $this->eventDispatcher = $eventDispatcher; } /** @@ -27,19 +37,13 @@ final class StorageManager implements StorageManagerInterface { $envelopes = is_array($envelopes) ? $envelopes : func_get_args(); - $this->storage->remove($envelopes); + $event = new PreFlushEvent($envelopes); + $this->eventDispatcher->dispatch($event); - foreach ($envelopes as $envelope) { - $replayStamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp'); - $replayCount = null === $replayStamp ? 0 : $replayStamp->getAmount() - 1; + $this->storage->remove($event->getEnvelopes()); - if (1 > $replayCount) { - continue; - } - - $envelope->with(new HopsStamp($replayCount)); - $this->storage->add($envelope); - } + $event = new PostFlushEvent($envelopes); + $this->eventDispatcher->dispatch($event); } /**