remove unused events and notification builder add hops, priority, keep methods

This commit is contained in:
Khoubza Younes
2020-12-04 10:52:01 +01:00
parent 5f386cd0d4
commit 7e31e4abb1
41 changed files with 455 additions and 316 deletions
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Event;
final class AfterFilter
{
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Event;
class AfterNotificationDispatched
{
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Event;
class AfterNotificationRendered
{
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Event;
class BeforeFilter
{
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Event;
class BeforeNotificationDispatched
{
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Event;
class BeforeNotificationRendered
{
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Event;
interface EventInterface
{
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Event;
class NotificationDispatched implements EventInterface
{
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Event;
class NotificationRendered implements EventInterface
{
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Event;
class PresenterReady implements EventInterface
{
}
-78
View File
@@ -1,78 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher;
use Flasher\Prime\Dispatcher\Event\EventInterface;
use Flasher\Prime\Dispatcher\Listener\ListenerInterface;
final class EventDispatcher implements EventDispatcherInterface
{
private $listeners = array();
private $sorted = array();
/**
* @inheritDoc
*/
public function addListener($eventName, ListenerInterface $listener, $priority = 0)
{
$this->listeners[$eventName][$priority][] = $listener;
}
/**
* @inheritDoc
*/
public function dispatch(EventInterface $event, $eventName = null)
{
$eventName = $eventName ?: get_class($event);
$listeners = $this->getListeners($eventName);
$this->callListeners($listeners, $event);
return $event;
}
/**
* @param string $eventName
*
* @return ListenerInterface[]
*/
public function getListeners($eventName)
{
if (empty($this->listeners[$eventName])) {
return array();
}
if (!isset($this->sorted[$eventName])) {
$this->sortListeners($eventName);
}
return $this->sorted[$eventName];
}
/**
* @param $eventName
*/
private function sortListeners($eventName)
{
krsort($this->listeners[$eventName]);
$this->sorted[$eventName] = array();
foreach ($this->listeners[$eventName] as $listeners) {
foreach ($listeners as $k => $listener) {
$this->sorted[$eventName][] = $listener;
}
}
}
/**
* @param ListenerInterface[] $listeners
* @param EventInterface $event
*/
protected function callListeners(array $listeners, $event)
{
foreach ($listeners as $listener) {
$listener->handle($event);
}
}
}
@@ -1,23 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher;
use Flasher\Prime\Dispatcher\Event\EventInterface;
use Flasher\Prime\Dispatcher\Listener\ListenerInterface;
interface EventDispatcherInterface
{
/**
* @param string $eventName
* @param ListenerInterface $listener
*/
public function addListener($eventName, ListenerInterface $listener);
/**
* @param EventInterface $event
* @param string|null $eventName
*
* @return EventInterface
*/
public function dispatch(EventInterface $event, $eventName = null);
}
-12
View File
@@ -1,12 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher;
final class FlusherEvents
{
const NOTIFICATION_CREATED = 'flusher.notification.created';
const NOTIFICATION_RENDERED = 'flusher.notification.rendered';
const NOTIFICATION_CLEARED = 'flusher.notification.cleared';
}
@@ -1,10 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Listener;
use Flasher\Prime\Dispatcher\Event\EventInterface;
interface ListenerInterface
{
public function handle(EventInterface $event);
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Dispatcher\Listener;
interface SubscriberInterface
{
}
+46 -1
View File
@@ -5,7 +5,7 @@ namespace Flasher\Prime;
use Flasher\Prime\Notification\NotificationInterface;
use Flasher\Prime\Stamp\StampInterface;
final class Envelope
final class Envelope implements NotificationInterface
{
/**
* @var NotificationInterface
@@ -104,4 +104,49 @@ final class Envelope
{
return $this->notification;
}
public function getType()
{
return $this->notification->getType();
}
public function setType($type)
{
$this->notification->setType($type);
}
public function getMessage()
{
return $this->notification->getMessage();
}
public function setMessage($message)
{
$this->notification->setMessage($message);
}
public function getOptions()
{
return $this->notification->getOptions();
}
public function setOptions(array $options)
{
$this->notification->setOptions($options);
}
public function getOption($name, $default = null)
{
return $this->notification->getOption($name, $default);
}
public function setOption($name, $value)
{
$this->notification->setOption($name, $value);
}
public function unsetOption($name)
{
$this->notification->unsetOption($name);
}
}
@@ -0,0 +1,37 @@
<?php
namespace Flasher\Prime\EventDispatcher\Event;
use Flasher\Prime\Envelope;
final class EnvelopesEvent
{
/**
* @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;
}
}
@@ -0,0 +1,8 @@
<?php
namespace Flasher\Prime\EventDispatcher\Event;
interface EventInterface
{
}
@@ -0,0 +1,11 @@
<?php
namespace Flasher\Prime\EventDispatcher\Event;
interface StoppableEventInterface
{
/**
* @return bool
*/
public function isPropagationStopped();
}
@@ -0,0 +1,96 @@
<?php
namespace Flasher\Prime\EventDispatcher;
use Flasher\Prime\EventDispatcher\Event\StoppableEventInterface;
use Flasher\Prime\EventDispatcher\EventSubscriber\EventSubscriberInterface;
final class EventDispatcher implements EventDispatcherInterface
{
private $listeners = array();
private $sorted = array();
/**
* @inheritDoc
*/
public function dispatch($event)
{
$listeners = $this->getListeners(get_class($event));
$this->callListeners($listeners, $event);
return $event;
}
/**
* {@inheritdoc}
*/
public function getListeners($eventName)
{
if (empty($this->listeners[$eventName])) {
return array();
}
if (!isset($this->sorted[$eventName])) {
$this->sortListeners($eventName);
}
return $this->sorted[$eventName];
}
/**
* @param $eventName
*/
private function sortListeners($eventName)
{
krsort($this->listeners[$eventName]);
$this->sorted[$eventName] = array();
foreach ($this->listeners[$eventName] as $listeners) {
foreach ($listeners as $k => $listener) {
$this->sorted[$eventName][] = $listener;
}
}
}
/**
* @param callable[] $listeners
* @param object $event
*/
protected function callListeners(array $listeners, $event)
{
foreach ($listeners as $listener) {
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
break;
}
$listener($event, $this);
}
}
/**
* @inheritDoc
*/
public function addListener($eventName, $listener, $priority = 0)
{
$this->listeners[$eventName][$priority][] = $listener;
}
/**
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
{
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
if (\is_string($params)) {
$this->addListener($eventName, array($subscriber, $params));
} elseif (\is_string($params[0])) {
$this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
} else {
foreach ($params as $listener) {
$this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
}
}
}
}
}
@@ -0,0 +1,34 @@
<?php
namespace Flasher\Prime\EventDispatcher;
use Flasher\Prime\EventDispatcher\EventSubscriber\EventSubscriberInterface;
interface EventDispatcherInterface
{
/**
* @param string $eventName
* @param callable $listener
* @param int $priority
*/
public function addListener($eventName, $listener, $priority = 0);
/**
* @param EventSubscriberInterface $subscriber
*/
public function addSubscriber(EventSubscriberInterface $subscriber);
/**
* @param string $eventName
*
* @return array
*/
public function getListeners($eventName);
/**
* @param object $event
*
* @return object
*/
public function dispatch($event);
}
@@ -0,0 +1,11 @@
<?php
namespace Flasher\Prime\EventDispatcher\EventSubscriber;
interface EventSubscriberInterface
{
/**
* @return array
*/
public static function getSubscribedEvents();
}
@@ -0,0 +1,37 @@
<?php
namespace Flasher\Prime\EventDispatcher\EventSubscriber;
use Flasher\Prime\EventDispatcher\Event\EnvelopesEvent;
use Flasher\Prime\Envelope;
final class FilterEnvelopesByHopsListener implements EventSubscriberInterface
{
/**
* @param EnvelopesEvent $event
*
* @return Envelope[]
*/
public function __invoke(EnvelopesEvent $event)
{
$envelopes = $event->getEnvelopes();
$envelopes = array_filter(
$envelopes,
static function (Envelope $envelope) {
$hopsStamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp');
return $hopsStamp->getAmount() > 0;
}
);
$event->setEnvelopes($envelopes);
}
public static function getSubscribedEvents()
{
return array(
'Flasher\Prime\EventDispatcher\Event\EnvelopesEvent'
);
}
}
+3 -3
View File
@@ -2,13 +2,13 @@
namespace Flasher\Prime\Filter;
use Flasher\Prime\Filter\Specification\ReplaySpecification;
use Flasher\Prime\Filter\Specification\HopsSpecification;
use Flasher\Prime\Filter\Specification\PrioritySpecification;
final class CriteriaBuilder
{
/**
* @var \Flasher\Prime\Filter\FilterBuilder
* @var FilterBuilder
*/
private $filterBuilder;
@@ -66,7 +66,7 @@ final class CriteriaBuilder
$min = isset($life['min']) ? $life['min'] : null;
$max = isset($life['max']) ? $life['max'] : null;
$this->filterBuilder->andWhere(new ReplaySpecification($min, $max));
$this->filterBuilder->andWhere(new HopsSpecification($min, $max));
}
public function buildLimit()
@@ -0,0 +1,46 @@
<?php
namespace Flasher\Prime\Filter\Specification;
use Flasher\Prime\Envelope;
final class HopsSpecification implements SpecificationInterface
{
/**
* @var int
*/
private $minAmount;
/**
* @var int|null
*/
private $maxAmount;
/**
* @param int $minAmount
* @param int|null $maxAmount
*/
public function __construct($minAmount, $maxAmount = null)
{
$this->minAmount = $minAmount;
$this->maxAmount = $maxAmount;
}
/**
* @inheritDoc
*/
public function isSatisfiedBy(Envelope $envelope)
{
$stamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp');
if (null === $stamp) {
return false;
}
if (null !== $this->maxAmount && $stamp->getAmount() > $this->maxAmount) {
return false;
}
return $stamp->getAmount() >= $this->minAmount;
}
}
@@ -11,6 +11,9 @@ final class NotSpecification implements SpecificationInterface
*/
private $specification;
/**
* @param SpecificationInterface $specification
*/
public function __construct(SpecificationInterface $specification)
{
$this->specification = $specification;
@@ -7,7 +7,7 @@ use Flasher\Prime\Envelope;
final class OrSpecification implements SpecificationInterface
{
/**
* @var \Flasher\Prime\Filter\Specification\SpecificationInterface[]
* @var SpecificationInterface[]
*/
private $specifications;
@@ -23,7 +23,7 @@ final class PrioritySpecification implements SpecificationInterface
}
/**
* @param \Flasher\Prime\Envelope $envelope
* @param Envelope $envelope
*
* @return bool
*/
@@ -1,44 +0,0 @@
<?php
namespace Flasher\Prime\Filter\Specification;
use Flasher\Prime\Envelope;
final class ReplaySpecification implements SpecificationInterface
{
/**
* @var int
*/
private $minLife;
/**
* @var int|null
*/
private $maxLife;
public function __construct($minLife, $maxLife = null)
{
$this->minLife = $minLife;
$this->maxLife = $maxLife;
}
/**
* @param Envelope $envelope
*
* @return bool
*/
public function isSatisfiedBy(Envelope $envelope)
{
$stamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp');
if (null === $stamp) {
return false;
}
if (null !== $this->maxLife && $stamp->getCount() > $this->maxLife) {
return false;
}
return $stamp->getCount() >= $this->minLife;
}
}
@@ -7,7 +7,7 @@ use Flasher\Prime\Envelope;
interface SpecificationInterface
{
/**
* @param \Flasher\Prime\Envelope $envelope
* @param Envelope $envelope
*
* @return bool
*/
@@ -2,6 +2,7 @@
namespace Flasher\Prime\Filter\Specification;
use DateTime;
use Flasher\Prime\Envelope;
final class TimeSpecification implements SpecificationInterface
@@ -16,14 +17,18 @@ final class TimeSpecification implements SpecificationInterface
*/
private $maxTime;
public function __construct($minTime, $maxTime = null)
/**
* @param DateTime $minTime
* @param DateTime|null $maxTime
*/
public function __construct(DateTime $minTime, DateTime $maxTime = null)
{
$this->minTime = $minTime;
$this->maxTime = $maxTime;
}
/**
* @param \Flasher\Prime\Envelope $envelope
* @param Envelope $envelope
*
* @return bool
*/
+43 -11
View File
@@ -2,19 +2,25 @@
namespace Flasher\Prime\Notification;
use Flasher\Prime\Envelope;
use Flasher\Prime\Stamp\HopsStamp;
use Flasher\Prime\Stamp\PriorityStamp;
class NotificationBuilder implements NotificationBuilderInterface
{
/**
* @var NotificationInterface
* @var Envelope
*/
protected $notification;
protected $envelope;
/**
* @param NotificationInterface|null $notification
*/
public function __construct(NotificationInterface $notification = null)
{
$this->notification = $notification ?: new Notification();
$notification = $notification ?: new Notification();
$this->envelope = Envelope::wrap($notification);
}
/**
@@ -22,7 +28,7 @@ class NotificationBuilder implements NotificationBuilderInterface
*/
public function type($type, $message = null, array $options = array())
{
$this->notification->setType($type);
$this->envelope->setType($type);
if (null !== $message) {
$this->message($message);
@@ -40,7 +46,7 @@ class NotificationBuilder implements NotificationBuilderInterface
*/
public function message($message)
{
$this->notification->setMessage($message);
$this->envelope->setMessage($message);
return $this;
}
@@ -51,10 +57,10 @@ class NotificationBuilder implements NotificationBuilderInterface
public function options($options, $merge = true)
{
if (true === $merge) {
$options = array_merge($this->notification->getOptions(), $options);
$options = array_merge($this->envelope->getOptions(), $options);
}
$this->notification->setOptions($options);
$this->envelope->setOptions($options);
return $this;
}
@@ -64,7 +70,7 @@ class NotificationBuilder implements NotificationBuilderInterface
*/
public function option($name, $value)
{
$this->notification->setOption($name, $value);
$this->envelope->setOption($name, $value);
return $this;
}
@@ -74,7 +80,15 @@ class NotificationBuilder implements NotificationBuilderInterface
*/
public function getNotification()
{
return $this->notification;
return $this->getEnvelope();
}
/**
* @return NotificationInterface
*/
public function getEnvelope()
{
return $this->envelope;
}
/**
@@ -109,18 +123,36 @@ class NotificationBuilder implements NotificationBuilderInterface
return $this->type(NotificationInterface::TYPE_WARNING, $message, $options);
}
/**
* @inheritDoc
*/
public function priority($priority)
{
$this->envelope->withStamp(new PriorityStamp($priority));
return $this;
}
public function hops()
/**
* @inheritDoc
*/
public function hops($amount)
{
$this->envelope->withStamp(new HopsStamp($amount));
return $this;
}
public function sticky()
/**
* @inheritDoc
*/
public function keep()
{
$hopsStamp = $this->envelope->get('Flasher\Prime\Stamp\HopsStamp');
$amount = $hopsStamp instanceof HopsStamp ? $hopsStamp->getAmount() : 1;
$this->envelope->withStamp(new HopsStamp($amount + 1));
return $this;
}
}
@@ -73,4 +73,23 @@ interface NotificationBuilderInterface
* @return self
*/
public function warning($message = null, array $options = array());
/**
* @param int $priority
*
* @return self
*/
public function priority($priority);
/**
* @return self
*/
public function keep();
/**
* @param int $amount
*
* @return self
*/
public function hops($amount);
}
+21 -20
View File
@@ -3,6 +3,8 @@
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\Filter\FilterManager;
use Flasher\Prime\Renderer\HasOptionsInterface;
@@ -13,40 +15,48 @@ use Flasher\Prime\Storage\StorageInterface;
abstract class AbstractPresenter implements PresenterInterface
{
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var ConfigInterface
*/
protected $config;
/**
* @var Flasher\Prime\Storage\StorageInterface
* @var StorageInterface
*/
protected $storage;
/**
* @var \Flasher\Prime\Filter\FilterManager
* @var FilterManager
*/
protected $filterManager;
/**
* @var \Flasher\Prime\Renderer\RendererManager
* @var RendererManager
*/
protected $rendererManager;
/**
* AbstractPresenter constructor.
*
* @param Flasher\Prime\Config\ConfigInterface $config
* @param \Flasher\Prime\Storage\StorageInterface $storage
* @param \Flasher\Prime\Filter\FilterManager $filterManager
* @param \Flasher\Prime\Renderer\RendererManager $rendererManager
* @param EventDispatcherInterface $eventDispatcher
* @param ConfigInterface $config
* @param StorageInterface $storage
* @param FilterManager $filterManager
* @param RendererManager $rendererManager
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
ConfigInterface $config,
StorageInterface $storage,
FilterManager $filterManager,
RendererManager $rendererManager
) {
$this->eventDispatcher = $eventDispatcher;
$this->config = $config;
$this->storage = $storage;
$this->filterManager = $filterManager;
@@ -64,16 +74,12 @@ abstract class AbstractPresenter implements PresenterInterface
protected function getEnvelopes($filterName, $criteria = array())
{
$filter = $this->filterManager->make($filterName);
$envelopes = $filter->filter($this->storage->get(), $criteria);
$envelopes = $filter->filter($this->storage->all(), $criteria);
return array_filter(
$envelopes,
static function (Envelope $envelope) {
$lifeStamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp');
$event = new EnvelopesEvent($envelopes);
$this->eventDispatcher->dispatch($event);
return $lifeStamp->getLife() > 0;
}
);
return $event->getEnvelopes();
}
/**
@@ -159,9 +165,4 @@ abstract class AbstractPresenter implements PresenterInterface
return array_values(array_filter(array_unique($options)));
}
public function hasNotifications()
{
}
}
@@ -2,6 +2,7 @@
namespace Flasher\Prime\Presenter\Adapter;
use Flasher\Prime\Envelope;
use Flasher\Prime\Presenter\AbstractPresenter;
final class HtmlPresenter extends AbstractPresenter
@@ -53,7 +54,7 @@ HTML;
}
/**
* @param \Flasher\Prime\Envelope[] $envelopes
* @param Envelope[] $envelopes
*
* @return string
*/
@@ -72,7 +73,7 @@ HTML;
}
/**
* @param \Flasher\Prime\Envelope[] $envelopes
* @param Envelope[] $envelopes
*
* @return string
*/
@@ -88,7 +89,7 @@ HTML;
}
/**
* @param \Flasher\Prime\Envelope[] $envelopes
* @param Envelope[] $envelopes
*
* @return string
*/
@@ -2,6 +2,7 @@
namespace Flasher\Prime\Presenter\Adapter;
use Flasher\Prime\Envelope;
use Flasher\Prime\Presenter\AbstractPresenter;
final class JsonPresenter extends AbstractPresenter
@@ -39,7 +40,7 @@ final class JsonPresenter extends AbstractPresenter
}
/**
* @param \Flasher\Prime\Envelope[] $envelopes
* @param Envelope[] $envelopes
*
* @return array
*/
+6 -6
View File
@@ -7,21 +7,21 @@ final class HopsStamp implements StampInterface
/**
* @var int
*/
private $count;
private $amount;
/**
* @param int $count
* @param int $amount
*/
public function __construct($count)
public function __construct($amount)
{
$this->count = $count;
$this->amount = $amount;
}
/**
* @return int
*/
public function getCount()
public function getAmount()
{
return $this->count;
return $this->amount;
}
}
+1 -1
View File
@@ -31,7 +31,7 @@ final class StorageManager implements StorageManagerInterface
foreach ($envelopes as $envelope) {
$replayStamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp');
$replayCount = null === $replayStamp ? 0 : $replayStamp->getCount() - 1;
$replayCount = null === $replayStamp ? 0 : $replayStamp->getAmount() - 1;
if (1 > $replayCount) {
continue;
@@ -16,6 +16,6 @@ final class LifeStampTest extends TestCase
$this->assertSame($stamp, $envelop->get('Flasher\Prime\Stamp\HopsStamp'));
$this->assertInstanceOf('Flasher\Prime\Stamp\HopsStamp', $stamp);
$this->assertSame(5, $stamp->getCount());
$this->assertSame(5, $stamp->getAmount());
}
}
@@ -2,6 +2,10 @@
namespace Flasher\Prime\Tests\Producer;
use Flasher\Prime\EventDispatcher\Event\BeforeFilter;
use Flasher\Prime\EventDispatcher\EventDispatcher;
use Flasher\Prime\EventDispatcher\FlusherEvents;
use Flasher\Prime\Notification\Notification;
use Flasher\Prime\Tests\TestCase;
use ReflectionClass;
@@ -34,7 +38,7 @@ final class ProducerManagerTest extends TestCase
$config = $this->getMockBuilder('Notify\Config\ConfigInterface')->getMock();
$config->method('get')->willReturn(null);
$manager = new Flasher\PrimeFlasher\Prime($config);
$manager = new \Flasher\PrimeFlasher\Prime($config);
$producer = $this->getMockBuilder('NotifyFlasher\PrimeFactory')->getMock();
$manager->addDriver($producer);
@@ -54,4 +58,15 @@ final class ProducerManagerTest extends TestCase
$this->assertSame($producer, $manager->make('not_supported'));
}
public function testDispatcher()
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(FlusherEvents::NOTIFICATION_CLEARED, function ($event) {
return $event;
});
$dispatcher->dispatch(new Notification());
}
}
@@ -1,8 +0,0 @@
<?php
namespace Flasher\Prime\Translator;
interface TranslatorInterface
{
}