update events

This commit is contained in:
Khoubza Younes
2020-12-04 13:36:17 +01:00
parent 3091013c39
commit 10e1ec7aa0
11 changed files with 207 additions and 27 deletions
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Flasher\Prime\EventDispatcher\Event;
use Flasher\Prime\Envelope;
final class PostFilterEvent
{
/**
* @var Envelope[]
*/
private $envelopes;
/**
* @param Envelope[] $envelopes
*/
public function __construct(array $envelopes)
{
$this->envelopes = $envelopes;
}
/**
* @return Envelope[]
*/
public function getEnvelopes()
{
return $this->envelopes;
}
/**
* @param Envelope[] $envelopes
*/
public function setEnvelopes($envelopes)
{
$this->envelopes = $envelopes;
}
}
@@ -4,7 +4,7 @@ namespace Flasher\Prime\EventDispatcher\Event;
use Flasher\Prime\Envelope;
final class EnvelopesEvent
final class PostFlushEvent
{
/**
* @var Envelope[]
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Flasher\Prime\EventDispatcher\Event;
use Flasher\Prime\Envelope;
final class PreFilterEvent
{
/**
* @var Envelope[]
*/
private $envelopes;
/**
* @param Envelope[] $envelopes
*/
public function __construct(array $envelopes)
{
$this->envelopes = $envelopes;
}
/**
* @return Envelope[]
*/
public function getEnvelopes()
{
return $this->envelopes;
}
/**
* @param Envelope[] $envelopes
*/
public function setEnvelopes($envelopes)
{
$this->envelopes = $envelopes;
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Flasher\Prime\EventDispatcher\Event;
use Flasher\Prime\Envelope;
final class PreFlushEvent
{
/**
* @var Envelope[]
*/
private $envelopes;
/**
* @param Envelope[] $envelopes
*/
public function __construct(array $envelopes)
{
$this->envelopes = $envelopes;
}
/**
* @return Envelope[]
*/
public function getEnvelopes()
{
return $this->envelopes;
}
/**
* @param Envelope[] $envelopes
*/
public function setEnvelopes($envelopes)
{
$this->envelopes = $envelopes;
}
}
+1 -1
View File
@@ -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])) {
@@ -5,7 +5,7 @@ namespace Flasher\Prime\EventDispatcher\EventSubscriber;
interface EventSubscriberInterface
{
/**
* @return array
* @return string|array
*/
public static function getSubscribedEvents();
}
@@ -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';
}
}
@@ -0,0 +1,49 @@
<?php
namespace Flasher\Prime\EventDispatcher\EventSubscriber;
use Flasher\Prime\EventDispatcher\Event\PostFlushEvent;
use Flasher\Prime\Stamp\HopsStamp;
use Flasher\Prime\Storage\StorageInterface;
class RemoveRenderedEnvelopesSubscriber implements EventSubscriberInterface
{
/**
* @var StorageInterface
*/
private $storage;
/**
* @param StorageInterface $storage
*/
public function __construct(StorageInterface $storage)
{
$this->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';
}
}
+3
View File
@@ -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())
+17 -5
View File
@@ -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();
+17 -13
View File
@@ -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);
}
/**