clean up config and add delay stamp

This commit is contained in:
Khoubza Younes
2020-12-06 14:04:54 +01:00
parent 7482f1dbc0
commit ff3b03fa56
5 changed files with 95 additions and 5 deletions
@@ -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);
}
/**
+1 -1
View File
@@ -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()
{
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Flasher\Prime\Middleware;
use Flasher\Prime\Envelope;
use Flasher\Prime\Stamp\DelayStamp;
final class AddDelayStampMiddleware implements MiddlewareInterface
{
public function handle(Envelope $envelope, callable $next)
{
if (null === $envelope->get('Flasher\Prime\Stamp\DelayStamp')) {
$envelope->withStamp(new DelayStamp(0));
}
return $next($envelope);
}
}
+21
View File
@@ -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
*/
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Flasher\Prime\Stamp;
final class DelayStamp implements StampInterface
{
/**
* @var int
*/
private $delay;
/**
* @param int $delay
*/
public function __construct($delay)
{
$this->delay = $delay;
}
/**
* @return int
*/
public function getDelay()
{
return $this->delay;
}
}