UsersRenamedEvent now dispatches instead of GenericEvent on users rename. Some events refactoring.

This commit is contained in:
Alexey Skobkin 2017-01-07 18:05:54 +03:00
parent 3db25e9bdb
commit 9488eddd9f
4 changed files with 54 additions and 18 deletions

View file

@ -0,0 +1,37 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Event;
use Skobkin\Bundle\PointToolsBundle\Entity\UserRenameEvent;
use Symfony\Component\EventDispatcher\Event;
/**
* Dispatched when one or more users were renamed
*/
class UsersRenamedEvent extends Event
{
const NAME = 'app.users.renamed';
/**
* @var UserRenameEvent[]
*/
private $renames;
/**
* UsersRenamedEvent constructor.
*
* @param UserRenameEvent[] $renames
*/
public function __construct(array $renames)
{
$this->renames = $renames;
}
/**
* @return UserRenameEvent[]
*/
public function getRenames(): array
{
return $this->renames;
}
}

View file

@ -2,11 +2,10 @@
namespace Skobkin\Bundle\PointToolsBundle\EventListener; namespace Skobkin\Bundle\PointToolsBundle\EventListener;
use Skobkin\Bundle\PointToolsBundle\Event\UsersRenamedEvent;
use Skobkin\Bundle\PointToolsBundle\Service\Telegram\Notifier; use Skobkin\Bundle\PointToolsBundle\Service\Telegram\Notifier;
use Symfony\Component\EventDispatcher\GenericEvent;
class UsersRenameNotifierListener class UsersRenamedNotifierListener
{ {
/** /**
* @var Notifier * @var Notifier
@ -24,8 +23,8 @@ class UsersRenameNotifierListener
$this->notifier = $notifier; $this->notifier = $notifier;
} }
public function onAppUsersRenamed(GenericEvent $event) public function onAppUsersRenamed(UsersRenamedEvent $event)
{ {
$this->notifier->sendUsersRenamedNotification((array) $event->getIterator()); $this->notifier->sendUsersRenamedNotification($event->getRenames());
} }
} }

View file

@ -10,15 +10,15 @@ use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Event\PreUpdateEventArgs;
use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Entity\User;
use Skobkin\Bundle\PointToolsBundle\Entity\UserRenameEvent; use Skobkin\Bundle\PointToolsBundle\Entity\UserRenameEvent;
use Skobkin\Bundle\PointToolsBundle\Event\UsersRenamedEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class UserRenameSubscriber implements EventSubscriber class UsersUpdatedSubscriber implements EventSubscriber
{ {
/** /**
* @var UserRenameEvent[] * @var UserRenameEvent[]
*/ */
private $items = []; private $renames = [];
/** /**
* @var EventDispatcherInterface * @var EventDispatcherInterface
@ -54,27 +54,27 @@ class UserRenameSubscriber implements EventSubscriber
} }
if ($event->hasChangedField('login')) { if ($event->hasChangedField('login')) {
$this->items[] = new UserRenameEvent($entity, $event->getOldValue('login')); $this->renames[] = new UserRenameEvent($entity, $event->getOldValue('login'));
} }
} }
public function postFlush(PostFlushEventArgs $event) public function postFlush(PostFlushEventArgs $event)
{ {
if (0 !== count($this->items)) { if (0 !== count($this->renames)) {
// Creating event for dispatch // Creating event for dispatch
$usersRenamedEvent = new GenericEvent(null, $this->items); $usersRenamedEvent = new UsersRenamedEvent($this->renames);
$em = $event->getEntityManager(); $em = $event->getEntityManager();
foreach ($this->items as $item) { foreach ($this->renames as $item) {
$em->persist($item); $em->persist($item);
} }
$this->items = []; $this->renames = [];
$em->flush(); $em->flush();
$this->eventDispatcher->dispatch('app.users.renamed', $usersRenamedEvent); $this->eventDispatcher->dispatch(UsersRenamedEvent::NAME, $usersRenamedEvent);
} }
} }
} }

View file

@ -85,14 +85,14 @@ services:
# Event listener # Event listener
point_tools.event_listener.user_rename_subscriber: point_tools.event_listener.users_updated:
class: Skobkin\Bundle\PointToolsBundle\EventListener\UserRenameSubscriber class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersUpdatedSubscriber
arguments: [@event_dispatcher] arguments: [@event_dispatcher]
tags: tags:
- { name: doctrine.event_subscriber, connection: default } - { name: doctrine.event_subscriber, connection: default }
point_tools.event_listener.users_renamed_notifier_listener: point_tools.event_listener.users_renamed_notifier:
class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersRenameNotifierListener class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersRenamedNotifierListener
arguments: [@point_tools.telegram.notifier] arguments: [@point_tools.telegram.notifier]
tags: tags:
- { name: kernel.event_listener, event: app.users.renamed } - { name: kernel.event_listener, event: app.users.renamed }