Skip to content

Commit

Permalink
Update listener to not dispatch event for entities which not have not…
Browse files Browse the repository at this point in the history
…ification rules
  • Loading branch information
Niduck committed Jan 29, 2016
1 parent ac6b4dd commit 5699254
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
71 changes: 53 additions & 18 deletions Doctrine/Listener/NotificationDoctrineListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ class NotificationDoctrineListener
*/
protected $eventDispatcher;

protected $class;
/**
* @var string
*/
protected $notificationEntityClass;

/**
* @var array
*/
protected $notificationRules;

/**
* NotificationDoctrineListener constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param $notificationClass
*/
public function __construct(EventDispatcherInterface $eventDispatcher, $notificationClass)
public function __construct(EventDispatcherInterface $eventDispatcher, $notificationEntityClass, array $notificationRules)
{
$this->eventDispatcher = $eventDispatcher;
$this->class = $notificationClass;
$this->notificationEntityClass = $notificationEntityClass;
$this->notificationRules = $notificationRules;
}

/**
Expand All @@ -36,9 +45,17 @@ public function __construct(EventDispatcherInterface $eventDispatcher, $notifica
*/
public function preUpdate(PreUpdateEventArgs $args)
{
$event = new PreUpdateNotificationEvent($args->getEntity(), $args->getEntityChangeSet());
$this->eventDispatcher
->dispatch('numerique1.notification.event.entity_pre_update', $event);
$entityClass = ClassUtils::getClass($args->getEntity());
if (array_key_exists($entityClass, $this->notificationRules))
{
$rules = $this->notificationRules[$entityClass];
$event = new PreUpdateNotificationEvent($args->getEntity(), $args->getEntityChangeSet(), array(
'em' => $args->getEntityManager(),
'rules' => $rules
));
$this->eventDispatcher
->dispatch('numerique1.notification.event.entity_pre_update', $event);
}

return $this;
}
Expand All @@ -51,8 +68,11 @@ public function preUpdate(PreUpdateEventArgs $args)
*/
public function postUpdate(LifecycleEventArgs $args)
{
$this->eventDispatcher
->dispatch('numerique1.notification.event.entity_post_update', $this->getNotificationEvent($args));
$event = $this->getNotificationEvent($args);
if($event){
$this->eventDispatcher
->dispatch('numerique1.notification.event.entity_post_update', $event);
}

return $this;
}
Expand All @@ -65,8 +85,11 @@ public function postUpdate(LifecycleEventArgs $args)
*/
public function postPersist(LifecycleEventArgs $args)
{
$this->eventDispatcher
->dispatch('numerique1.notification.event.entity_post_persist', $this->getNotificationEvent($args));
$event = $this->getNotificationEvent($args);
if($event){
$this->eventDispatcher
->dispatch('numerique1.notification.event.entity_post_persist', $event);
}
return $this;
}

Expand All @@ -78,8 +101,13 @@ public function postPersist(LifecycleEventArgs $args)
*/
public function postRemove(LifecycleEventArgs $args)
{
$this->eventDispatcher
->dispatch('numerique1.notification.event.entity_post_remove', $this->getNotificationEvent($args));
$event = $this->getNotificationEvent($args);
if($event){
$this->eventDispatcher
->dispatch('numerique1.notification.event.entity_post_remove', $event);
}

return $this;
}

/**
Expand All @@ -91,10 +119,7 @@ public function postRemove(LifecycleEventArgs $args)
public function preRemove(LifecycleEventArgs $args)
{
$em = $args->getEntityManager();
$this->eventDispatcher
->dispatch('numerique1.notification.event.entity_pre_remove', $this->getNotificationEvent($args));

$notifications = $em->getRepository($this->class)->findBy(array(
$notifications = $em->getRepository($this->notificationEntityClass)->findBy(array(
'targetClass' => ClassUtils::getClass($args->getEntity()),
'targetId' => $args->getEntity()->getId()
));
Expand All @@ -113,7 +138,17 @@ public function preRemove(LifecycleEventArgs $args)
*/
public function getNotificationEvent(LifecycleEventArgs $args)
{
$event = new NotificationEvent($args->getEntity());
return $event;
$entityClass = ClassUtils::getClass($args->getEntity());
if (array_key_exists($entityClass, $this->notificationRules))
{
$rules = $this->notificationRules[$entityClass];
$event = new NotificationEvent($args->getEntity(), array(
'em' => $args->getEntityManager(),
'rules' => $rules
));
return $event;
}

return false;
}
}
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:
arguments:
- @event_dispatcher
- %numerique1_notification.notification.class%
- %numerique1_notification.configs%
tags:
- { name: doctrine.event_listener, event: postPersist }
- { name: doctrine.event_listener, event: preUpdate }
Expand Down

0 comments on commit 5699254

Please sign in to comment.