2016-03-29 00:12:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\EventListener;
|
|
|
|
|
|
|
|
use Doctrine\Common\EventSubscriber;
|
|
|
|
// For new doctrine: https://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html#creating-the-subscriber-class
|
|
|
|
//use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
|
|
|
|
//use Doctrine\Common\Persistence\Event\PreUpdateEventArgs;
|
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs;
|
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\UserRenameEvent;
|
2017-01-07 15:05:54 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Event\UsersRenamedEvent;
|
2017-01-06 21:38:20 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2016-03-29 00:12:52 +00:00
|
|
|
|
2017-01-07 15:05:54 +00:00
|
|
|
class UsersUpdatedSubscriber implements EventSubscriber
|
2016-03-29 00:12:52 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var UserRenameEvent[]
|
|
|
|
*/
|
2017-01-07 15:05:54 +00:00
|
|
|
private $renames = [];
|
2016-03-29 00:12:52 +00:00
|
|
|
|
2017-01-06 21:38:20 +00:00
|
|
|
/**
|
|
|
|
* @var EventDispatcherInterface
|
|
|
|
*/
|
|
|
|
private $eventDispatcher;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UserRenameSubscriber constructor.
|
|
|
|
*
|
|
|
|
* @param EventDispatcherInterface $eventDispatcher
|
|
|
|
*/
|
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher)
|
|
|
|
{
|
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
|
|
|
}
|
|
|
|
|
2016-03-29 00:12:52 +00:00
|
|
|
public function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'preUpdate',
|
|
|
|
'postFlush',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function preUpdate(PreUpdateEventArgs $event)
|
|
|
|
{
|
|
|
|
/** @var User $entity */
|
|
|
|
$entity = $event->getObject();
|
|
|
|
|
|
|
|
if (!$entity instanceof User) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($event->hasChangedField('login')) {
|
2017-01-07 15:05:54 +00:00
|
|
|
$this->renames[] = new UserRenameEvent($entity, $event->getOldValue('login'));
|
2016-03-29 00:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postFlush(PostFlushEventArgs $event)
|
|
|
|
{
|
2017-01-07 15:05:54 +00:00
|
|
|
if (0 !== count($this->renames)) {
|
2017-01-06 21:38:20 +00:00
|
|
|
// Creating event for dispatch
|
2017-01-07 15:05:54 +00:00
|
|
|
$usersRenamedEvent = new UsersRenamedEvent($this->renames);
|
2017-01-06 21:38:20 +00:00
|
|
|
|
2016-03-29 00:12:52 +00:00
|
|
|
$em = $event->getEntityManager();
|
|
|
|
|
2017-01-07 15:05:54 +00:00
|
|
|
foreach ($this->renames as $item) {
|
2016-03-29 00:12:52 +00:00
|
|
|
$em->persist($item);
|
|
|
|
}
|
|
|
|
|
2017-01-07 15:05:54 +00:00
|
|
|
$this->renames = [];
|
2016-03-29 00:12:52 +00:00
|
|
|
|
|
|
|
$em->flush();
|
2017-01-06 21:38:20 +00:00
|
|
|
|
2017-01-07 15:05:54 +00:00
|
|
|
$this->eventDispatcher->dispatch(UsersRenamedEvent::NAME, $usersRenamedEvent);
|
2016-03-29 00:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|