mirror of
https://github.com/php-flasher/php-flasher.git
synced 2026-03-31 15:07:47 +01:00
clean up config and add delay stamp
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
services:
|
||||
Flasher\Prime\Config\Config: '@flasher.config'
|
||||
Flasher\Prime\Manager\AbstractManager: '@flasher.abstract_manager'
|
||||
Flasher\Prime\Flasher: '@flasher'
|
||||
Flasher\Prime\Presenter\PresenterManager: '@flasher.presenter_manager'
|
||||
Flasher\Prime\Renderer\RendererManager: '@flasher.renderer_manager'
|
||||
Flasher\Prime\Middleware\FlasherBus: '@flasher.flasher_bus'
|
||||
Flasher\Prime\EventDispatcher\EventDispatcher: '@flasher.event_dispatcher'
|
||||
Flasher\Symfony\Storage\Storage: '@flasher.storage'
|
||||
Flasher\Prime\Storage\StorageManager: '@flasher.storage_manager'
|
||||
Flasher\Prime\Filter\FilterManager: '@flasher.filter_manager'
|
||||
Flasher\Prime\Filter\FilterBuilder: '@flasher.filter_builder'
|
||||
Flasher\Prime\Filter\DefaultFilter: '@flasher.filter.default'
|
||||
Flasher\Prime\Factory\AbstractFactory: '@flasher.abstract_factory'
|
||||
Flasher\Prime\Presenter\AbstractPresenter: '@flasher.abstract_presenter'
|
||||
Flasher\Prime\Presenter\Adapter\JsonPresenter: '@flasher.presenter.json'
|
||||
Flasher\Prime\Presenter\Adapter\HtmlPresenter: '@flasher.presenter.html'
|
||||
|
||||
flasher.factory_manager: '@flasher'
|
||||
Flasher\Prime\FlasherInterface: '@flasher'
|
||||
Flasher\Prime\Storage\StorageManagerInterface: '@flasher.storage_manager'
|
||||
Flasher\Prime\Renderer\RendererManagerInterface: '@flasher.renderer_manager'
|
||||
Flasher\Prime\Presenter\PresenterManagerInterface: '@flasher.presenter_manager'
|
||||
Flasher\Prime\Middleware\FlasherBusInterface: '@flasher.flasher_bus'
|
||||
Flasher\Prime\Filter\FilterManagerInterface: '@flasher.filter_manager'
|
||||
Flasher\Prime\EventDispatcher\EventDispatcherInterface: '@flasher.event_dispatcher'
|
||||
Flasher\Prime\Storage\StorageInterface: '@flasher.storage'
|
||||
@@ -1,13 +1,3 @@
|
||||
imports:
|
||||
# - { resource: 'services.yaml' }
|
||||
- { resource: 'middlewares.yaml' }
|
||||
- { resource: 'storage.yaml' }
|
||||
- { resource: 'presenters.yaml' }
|
||||
- { resource: 'filters.yaml' }
|
||||
- { resource: 'twig.yaml' }
|
||||
- { resource: 'event_subscribers.yaml' }
|
||||
- { resource: 'aliases.yaml' }
|
||||
|
||||
services:
|
||||
flasher.config:
|
||||
class: Flasher\Prime\Config\Config
|
||||
@@ -33,3 +23,147 @@ services:
|
||||
flasher.renderer_manager:
|
||||
class: Flasher\Prime\Renderer\RendererManager
|
||||
parent: 'flasher.abstract_manager'
|
||||
|
||||
flasher.flasher_bus:
|
||||
class: Flasher\Prime\Middleware\FlasherBus
|
||||
|
||||
flasher.middleware.add_created_stamp:
|
||||
class: Flasher\Prime\Middleware\AddCreatedAtStampMiddleware
|
||||
public: false
|
||||
tags:
|
||||
- { name: 'flasher.middleware' }
|
||||
|
||||
flasher.middleware.add_hops_stamp:
|
||||
class: Flasher\Prime\Middleware\AddHopsStampMiddleware
|
||||
public: false
|
||||
tags:
|
||||
- { name: 'flasher.middleware' }
|
||||
|
||||
flasher.middleware.add_priority_stamp:
|
||||
class: Flasher\Prime\Middleware\AddPriorityStampMiddleware
|
||||
public: false
|
||||
tags:
|
||||
- { name: 'flasher.middleware' }
|
||||
|
||||
flasher.middleware.add_delay_stamp:
|
||||
class: Flasher\Prime\Middleware\AddDelayStampMiddleware
|
||||
public: false
|
||||
tags:
|
||||
- { name: 'flasher.middleware' }
|
||||
|
||||
flasher.storage:
|
||||
class: Flasher\Symfony\Storage\Storage
|
||||
arguments:
|
||||
- '@session'
|
||||
|
||||
flasher.storage_manager:
|
||||
class: Flasher\Prime\Storage\StorageManager
|
||||
arguments:
|
||||
- '@flasher.storage'
|
||||
- '@flasher.event_dispatcher'
|
||||
|
||||
flasher.event_dispatcher:
|
||||
class: Flasher\Prime\EventDispatcher\EventDispatcher
|
||||
|
||||
flasher.event_listener.post_filter:
|
||||
class: Flasher\Prime\EventDispatcher\EventListener\PostFilterListener
|
||||
public: false
|
||||
arguments:
|
||||
- '@flasher.storage'
|
||||
tags:
|
||||
- { name: 'flasher.event_subscriber' }
|
||||
|
||||
flasher.event_listener.post_flush:
|
||||
class: Flasher\Prime\EventDispatcher\EventListener\PostFlushListener
|
||||
public: false
|
||||
arguments:
|
||||
- '@flasher.storage'
|
||||
tags:
|
||||
- { name: 'flasher.event_subscriber' }
|
||||
|
||||
flasher.event_listener.middleware:
|
||||
class: Flasher\Prime\EventDispatcher\EventListener\MiddlewareListener
|
||||
public: false
|
||||
arguments:
|
||||
- '@flasher.flasher_bus'
|
||||
tags:
|
||||
- { name: 'flasher.event_subscriber' }
|
||||
|
||||
flasher.event_listener.storage:
|
||||
class: Flasher\Prime\EventDispatcher\EventListener\StorageListener
|
||||
public: false
|
||||
arguments:
|
||||
- '@flasher.storage'
|
||||
tags:
|
||||
- { name: 'flasher.event_subscriber' }
|
||||
|
||||
flasher.filter_manager:
|
||||
class: Flasher\Prime\Filter\FilterManager
|
||||
parent: 'flasher.abstract_manager'
|
||||
|
||||
flasher.filter_builder:
|
||||
class: Flasher\Prime\Filter\FilterBuilder
|
||||
|
||||
flasher.filter.default:
|
||||
class: Flasher\Prime\Filter\DefaultFilter
|
||||
arguments:
|
||||
- '@flasher.filter_builder'
|
||||
tags:
|
||||
- { name: 'flasher.filter' }
|
||||
|
||||
flasher.presenter_manager:
|
||||
class: Flasher\Prime\Presenter\PresenterManager
|
||||
parent: 'flasher.abstract_manager'
|
||||
|
||||
flasher.abstract_presenter:
|
||||
class: Flasher\Prime\Presenter\AbstractPresenter
|
||||
abstract: true
|
||||
arguments:
|
||||
- '@flasher.event_dispatcher'
|
||||
- '@flasher.config'
|
||||
- '@flasher.storage_manager'
|
||||
- '@flasher.filter_manager'
|
||||
- '@flasher.renderer_manager'
|
||||
|
||||
flasher.presenter.html:
|
||||
class: Flasher\Prime\Presenter\Adapter\HtmlPresenter
|
||||
parent: 'flasher.abstract_presenter'
|
||||
tags:
|
||||
- { name: 'flasher.presenter' }
|
||||
|
||||
flasher.presenter.json:
|
||||
class: Flasher\Prime\Presenter\Adapter\JsonPresenter
|
||||
parent: 'flasher.abstract_presenter'
|
||||
tags:
|
||||
- { name: 'flasher.presenter' }
|
||||
|
||||
flasher.twig.extension:
|
||||
class: Flasher\Symfony\Twig\FlasherTwigExtension
|
||||
arguments:
|
||||
- '@flasher.presenter.html'
|
||||
tags:
|
||||
- { name: 'twig.extension' }
|
||||
|
||||
Flasher\Prime\Config\Config: '@flasher.config'
|
||||
Flasher\Prime\Flasher: '@flasher'
|
||||
Flasher\Prime\Presenter\PresenterManager: '@flasher.presenter_manager'
|
||||
Flasher\Prime\Renderer\RendererManager: '@flasher.renderer_manager'
|
||||
Flasher\Prime\Middleware\FlasherBus: '@flasher.flasher_bus'
|
||||
Flasher\Prime\EventDispatcher\EventDispatcher: '@flasher.event_dispatcher'
|
||||
Flasher\Symfony\Storage\Storage: '@flasher.storage'
|
||||
Flasher\Prime\Storage\StorageManager: '@flasher.storage_manager'
|
||||
Flasher\Prime\Filter\FilterManager: '@flasher.filter_manager'
|
||||
Flasher\Prime\Filter\FilterBuilder: '@flasher.filter_builder'
|
||||
Flasher\Prime\Filter\DefaultFilter: '@flasher.filter.default'
|
||||
Flasher\Prime\Presenter\Adapter\JsonPresenter: '@flasher.presenter.json'
|
||||
Flasher\Prime\Presenter\Adapter\HtmlPresenter: '@flasher.presenter.html'
|
||||
|
||||
flasher.factory_manager: '@flasher'
|
||||
Flasher\Prime\FlasherInterface: '@flasher'
|
||||
Flasher\Prime\Storage\StorageManagerInterface: '@flasher.storage_manager'
|
||||
Flasher\Prime\Renderer\RendererManagerInterface: '@flasher.renderer_manager'
|
||||
Flasher\Prime\Presenter\PresenterManagerInterface: '@flasher.presenter_manager'
|
||||
Flasher\Prime\Middleware\FlasherBusInterface: '@flasher.flasher_bus'
|
||||
Flasher\Prime\Filter\FilterManagerInterface: '@flasher.filter_manager'
|
||||
Flasher\Prime\EventDispatcher\EventDispatcherInterface: '@flasher.event_dispatcher'
|
||||
Flasher\Prime\Storage\StorageInterface: '@flasher.storage'
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
services:
|
||||
flasher.event_dispatcher:
|
||||
class: Flasher\Prime\EventDispatcher\EventDispatcher
|
||||
|
||||
Flasher\Prime\EventDispatcher\EventListener\PostFilterListener:
|
||||
public: false
|
||||
tags:
|
||||
- 'flasher.event_subscriber'
|
||||
|
||||
Flasher\Prime\EventDispatcher\EventListener\PostFlushListener:
|
||||
public: false
|
||||
arguments:
|
||||
- '@flasher.storage'
|
||||
tags:
|
||||
- 'flasher.event_subscriber'
|
||||
|
||||
Flasher\Prime\EventDispatcher\EventListener\MiddlewareListener:
|
||||
public: false
|
||||
arguments:
|
||||
- '@flasher.flasher_bus'
|
||||
tags:
|
||||
- 'flasher.event_subscriber'
|
||||
|
||||
Flasher\Prime\EventDispatcher\EventListener\StorageListener:
|
||||
public: false
|
||||
arguments:
|
||||
- '@flasher.storage'
|
||||
tags:
|
||||
- 'flasher.event_subscriber'
|
||||
@@ -1,14 +0,0 @@
|
||||
services:
|
||||
flasher.filter_manager:
|
||||
class: Flasher\Prime\Filter\FilterManager
|
||||
parent: 'flasher.abstract_manager'
|
||||
|
||||
flasher.filter_builder:
|
||||
class: Flasher\Prime\Filter\FilterBuilder
|
||||
|
||||
flasher.filter.default:
|
||||
class: Flasher\Prime\Filter\DefaultFilter
|
||||
arguments:
|
||||
- '@flasher.filter_builder'
|
||||
tags:
|
||||
- 'flasher.filter'
|
||||
@@ -1,18 +0,0 @@
|
||||
services:
|
||||
flasher.flasher_bus:
|
||||
class: Flasher\Prime\Middleware\FlasherBus
|
||||
|
||||
Flasher\Prime\Middleware\AddCreatedAtStampMiddleware:
|
||||
public: false
|
||||
tags:
|
||||
- 'flasher.middleware'
|
||||
|
||||
Flasher\Prime\Middleware\AddHopsStampMiddleware:
|
||||
public: false
|
||||
tags:
|
||||
- 'flasher.middleware'
|
||||
|
||||
Flasher\Prime\Middleware\AddPriorityStampMiddleware:
|
||||
public: false
|
||||
tags:
|
||||
- 'flasher.middleware'
|
||||
@@ -1,26 +0,0 @@
|
||||
services:
|
||||
flasher.presenter_manager:
|
||||
class: Flasher\Prime\Presenter\PresenterManager
|
||||
parent: 'flasher.abstract_manager'
|
||||
|
||||
flasher.abstract_presenter:
|
||||
class: Flasher\Prime\Presenter\AbstractPresenter
|
||||
abstract: true
|
||||
arguments:
|
||||
- '@flasher.event_dispatcher'
|
||||
- '@flasher.config'
|
||||
- '@flasher.storage_manager'
|
||||
- '@flasher.filter_manager'
|
||||
- '@flasher.renderer_manager'
|
||||
|
||||
flasher.presenter.html:
|
||||
class: Flasher\Prime\Presenter\Adapter\HtmlPresenter
|
||||
parent: 'flasher.abstract_presenter'
|
||||
tags:
|
||||
- 'flasher.presenter'
|
||||
|
||||
flasher.presenter.json:
|
||||
class: Flasher\Prime\Presenter\Adapter\JsonPresenter
|
||||
parent: 'flasher.abstract_presenter'
|
||||
tags:
|
||||
- 'flasher.presenter'
|
||||
@@ -1,25 +0,0 @@
|
||||
services:
|
||||
flasher.config:
|
||||
class: Flasher\Prime\Config\Config
|
||||
arguments:
|
||||
- null
|
||||
|
||||
flasher.abstract_manager:
|
||||
class: Flasher\Prime\Manager\AbstractManager
|
||||
abstract: true
|
||||
arguments:
|
||||
- '@flasher.config'
|
||||
|
||||
flasher:
|
||||
class: Flasher\Prime\Flasher
|
||||
parent: 'flasher.abstract_manager'
|
||||
|
||||
flasher.renderer_manager:
|
||||
class: Flasher\Prime\Renderer\RendererManager
|
||||
parent: 'flasher.abstract_manager'
|
||||
|
||||
flasher.abstract_factory:
|
||||
class: Flasher\Prime\Factory\AbstractFactory
|
||||
abstract: true
|
||||
arguments:
|
||||
- '@flasher.flasher_bus'
|
||||
@@ -1,11 +0,0 @@
|
||||
services:
|
||||
flasher.storage:
|
||||
class: Flasher\Symfony\Storage\Storage
|
||||
arguments:
|
||||
- '@session'
|
||||
|
||||
flasher.storage_manager:
|
||||
class: Flasher\Prime\Storage\StorageManager
|
||||
arguments:
|
||||
- '@flasher.storage'
|
||||
- '@flasher.event_dispatcher'
|
||||
@@ -1,7 +0,0 @@
|
||||
services:
|
||||
Flasher\Symfony\Twig\FlasherTwigExtension:
|
||||
class:
|
||||
arguments:
|
||||
- '@flasher.presenter.html'
|
||||
tags:
|
||||
- 'twig.extension'
|
||||
@@ -51,6 +51,28 @@ final class Storage implements StorageInterface
|
||||
$this->session->set(self::ENVELOPES_NAMESPACE, $store);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function update($envelopes)
|
||||
{
|
||||
$envelopes = is_array($envelopes) ? $envelopes : func_get_args();
|
||||
$map = UuidStamp::indexWithUuid($envelopes);
|
||||
|
||||
$store = $this->all();
|
||||
foreach ($store as $index => $envelope) {
|
||||
$uuid = $envelope->get('Flasher\Prime\Stamp\UuidStamp')->getUuid();
|
||||
|
||||
if (!isset($map[$uuid])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$store[$index] = $map[$uuid];
|
||||
}
|
||||
|
||||
$this->session->set(self::ENVELOPES_NAMESPACE, $store);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user