Ported EventListener\* (without service registration).

This commit is contained in:
Alexey Skobkin 2023-03-28 23:22:39 +03:00
parent a129130ba1
commit ca1fb63604
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
5 changed files with 64 additions and 95 deletions

View file

@ -1,30 +0,0 @@
<?php
namespace src\PointToolsBundle\EventListener;
use src\PointToolsBundle\Event\UserSubscribersUpdatedEvent;
use src\PointToolsBundle\Service\Telegram\Notifier;
class UserSubscribersUpdatedListener
{
/**
* @var Notifier
*/
private $notifier;
/**
* UsersRenameNotifierListener constructor.
*
* @param Notifier $notifier
*/
public function __construct(Notifier $notifier)
{
$this->notifier = $notifier;
}
public function onAppUserSubscribersUpdated(UserSubscribersUpdatedEvent $event): void
{
$this->notifier->sendUserSubscribersUpdatedNotification($event->getUser(), $event->getSubscribedUsers(), $event->getUnsubscribedUsers());
}
}

View file

@ -1,30 +0,0 @@
<?php
namespace src\PointToolsBundle\EventListener;
use src\PointToolsBundle\Event\UsersRenamedEvent;
use src\PointToolsBundle\Service\Telegram\Notifier;
class UsersRenamedListener
{
/**
* @var Notifier
*/
private $notifier;
/**
* UsersRenameNotifierListener constructor.
*
* @param Notifier $notifier
*/
public function __construct(Notifier $notifier)
{
$this->notifier = $notifier;
}
public function onAppUsersRenamed(UsersRenamedEvent $event): void
{
$this->notifier->sendUsersRenamedNotification($event->getRenames());
}
}

View file

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\EventListener;
use App\Event\UserSubscribersUpdatedEvent;
use App\Service\Telegram\Notifier;
class UserSubscribersUpdatedListener
{
public function __construct(
private readonly Notifier $notifier,
) {
}
public function onAppUserSubscribersUpdated(UserSubscribersUpdatedEvent $event): void
{
$this->notifier->sendUserSubscribersUpdatedNotification($event->user, $event->subscribed, $event->unsubscribed);
}
}

View file

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\EventListener;
use App\Event\UsersRenamedEvent;
use App\Service\Telegram\Notifier;
class UsersRenamedListener
{
public function __construct(
private readonly Notifier $notifier,
) {
}
public function onAppUsersRenamed(UsersRenamedEvent $event): void
{
$this->notifier->sendUsersRenamedNotification($event->renames);
}
}

View file

@ -1,39 +1,27 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\EventListener; namespace App\EventListener;
use App\Event\UsersRenamedEvent;
use Doctrine\Common\EventSubscriber; use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use App\Entity\{User, UserRenameEvent, UserRenameEvent as RenameEventEntity};
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
// For new doctrine: https://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html#creating-the-subscriber-class // 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\LifecycleEventArgs;
//use Doctrine\Common\Persistence\Event\PreUpdateEventArgs; //use Doctrine\Common\Persistence\Event\PreUpdateEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use src\PointToolsBundle\Entity\User;
use src\PointToolsBundle\Entity\UserRenameEvent;
use src\PointToolsBundle\Event\UsersRenamedEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class UsersUpdatedSubscriber implements EventSubscriber class UsersUpdatedSubscriber implements EventSubscriber
{ {
/** /** @var UserRenameEvent[] */
* @var UserRenameEvent[] private array $renameEntities = [];
*/
private $renames = [];
/** public function __construct(
* @var EventDispatcherInterface private readonly EventDispatcherInterface $eventDispatcher,
*/ ) {
private $eventDispatcher;
/**
* UserRenameSubscriber constructor.
*
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
} }
public function getSubscribedEvents(): array public function getSubscribedEvents(): array
@ -54,27 +42,28 @@ class UsersUpdatedSubscriber implements EventSubscriber
} }
if ($event->hasChangedField('login')) { if ($event->hasChangedField('login')) {
$this->renames[] = new UserRenameEvent($entity, $event->getOldValue('login')); $this->renameEntities[] = new RenameEventEntity($entity, $event->getOldValue('login'));
} }
} }
/** TODO: try to avoid double flush or no? */
public function postFlush(PostFlushEventArgs $event): void public function postFlush(PostFlushEventArgs $event): void
{ {
if (0 !== count($this->renames)) { if (0 !== count($this->renameEntities)) {
// Creating event for dispatch // Creating event for dispatch
$usersRenamedEvent = new UsersRenamedEvent($this->renames); $usersRenamedEvent = new UsersRenamedEvent($this->renameEntities);
$em = $event->getEntityManager(); $om = $event->getObjectManager();
foreach ($this->renames as $item) { foreach ($this->renameEntities as $item) {
$em->persist($item); $om->persist($item);
} }
$this->renames = []; $this->renameEntities = [];
$em->flush(); $om->flush();
$this->eventDispatcher->dispatch(UsersRenamedEvent::NAME, $usersRenamedEvent); $this->eventDispatcher->dispatch($usersRenamedEvent, UsersRenamedEvent::NAME);
} }
} }
} }