From b6c6b1c2fc368e189c35519cd2b38259f6319fea Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Sun, 6 Dec 2020 14:04:54 +0100 Subject: [PATCH] clean up config and add delay stamp --- .../EventListener/PostFilterListener.php | 32 ++++++++++++++++--- Flasher.php | 2 +- Middleware/AddDelayStampMiddleware.php | 18 +++++++++++ Notification/NotificationBuilder.php | 21 ++++++++++++ Stamp/DelayStamp.php | 27 ++++++++++++++++ 5 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 Middleware/AddDelayStampMiddleware.php create mode 100644 Stamp/DelayStamp.php diff --git a/EventDispatcher/EventListener/PostFilterListener.php b/EventDispatcher/EventListener/PostFilterListener.php index d91853ab..79335fc9 100644 --- a/EventDispatcher/EventListener/PostFilterListener.php +++ b/EventDispatcher/EventListener/PostFilterListener.php @@ -4,9 +4,24 @@ namespace Flasher\Prime\EventDispatcher\EventListener; use Flasher\Prime\EventDispatcher\Event\PostFilterEvent; use Flasher\Prime\Envelope; +use Flasher\Prime\Stamp\DelayStamp; +use Flasher\Prime\Storage\StorageInterface; final class PostFilterListener implements EventSubscriberInterface { + /** + * @var StorageInterface + */ + private $storage; + + /** + * @param StorageInterface $storage + */ + public function __construct(StorageInterface $storage) + { + $this->storage = $storage; + } + /** * @param PostFilterEvent $event * @@ -15,14 +30,23 @@ final class PostFilterListener implements EventSubscriberInterface public function __invoke(PostFilterEvent $event) { $envelopes = $event->getEnvelopes(); + $filtered = array(); - $envelopes = array_filter($envelopes, static function (Envelope $envelope) { + foreach ($envelopes as $envelope) { $hopsStamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp'); + $delayStamp = $envelope->get('Flasher\Prime\Stamp\DelayStamp'); - return $hopsStamp->getAmount() > 0; - }); + if (0 < $hopsStamp->getAmount() && 0 === $delayStamp->getDelay()) { + $filtered[] = $envelope; - $event->setEnvelopes($envelopes); + continue; + } + + $envelope->withStamp(new DelayStamp($delayStamp->getDelay() - 1)); + $this->storage->update($envelope); + } + + $event->setEnvelopes($filtered); } /** diff --git a/Flasher.php b/Flasher.php index a48e28df..4d4f3bfb 100644 --- a/Flasher.php +++ b/Flasher.php @@ -18,7 +18,7 @@ use Flasher\Prime\Notification\NotificationInterface; * @method NotificationBuilderInterface warning($message = null, array $options = array()) * @method NotificationInterface getNotification() */ -final class Flasher extends AbstractManager +final class Flasher extends AbstractManager implements FlasherInterface { public function getDefaultDriver() { diff --git a/Middleware/AddDelayStampMiddleware.php b/Middleware/AddDelayStampMiddleware.php new file mode 100644 index 00000000..7ad9f206 --- /dev/null +++ b/Middleware/AddDelayStampMiddleware.php @@ -0,0 +1,18 @@ +get('Flasher\Prime\Stamp\DelayStamp')) { + $envelope->withStamp(new DelayStamp(0)); + } + + return $next($envelope); + } +} diff --git a/Notification/NotificationBuilder.php b/Notification/NotificationBuilder.php index 898bb9de..fd1bc589 100644 --- a/Notification/NotificationBuilder.php +++ b/Notification/NotificationBuilder.php @@ -5,6 +5,7 @@ namespace Flasher\Prime\Notification; use Flasher\Prime\Envelope; use Flasher\Prime\EventDispatcher\Event\EnvelopeDispatchedEvent; use Flasher\Prime\EventDispatcher\EventDispatcherInterface; +use Flasher\Prime\Stamp\DelayStamp; use Flasher\Prime\Stamp\HandlerStamp; use Flasher\Prime\Stamp\HopsStamp; use Flasher\Prime\Stamp\PriorityStamp; @@ -184,6 +185,26 @@ class NotificationBuilder implements NotificationBuilderInterface return $this; } + /** + * @param int $delay + * + * @return $this + */ + public function delay($delay) + { + $this->envelope->withStamp(new DelayStamp($delay)); + + return $this; + } + + /** + * @return $this + */ + public function now() + { + return $this->delay(0); + } + /** * @inheritDoc */ diff --git a/Stamp/DelayStamp.php b/Stamp/DelayStamp.php new file mode 100644 index 00000000..6900afd1 --- /dev/null +++ b/Stamp/DelayStamp.php @@ -0,0 +1,27 @@ +delay = $delay; + } + + /** + * @return int + */ + public function getDelay() + { + return $this->delay; + } +}