WIP: Symfony 6 project remake #2
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
20
src/EventListener/UserSubscribersUpdatedListener.php
Normal file
20
src/EventListener/UserSubscribersUpdatedListener.php
Normal 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);
|
||||
}
|
||||
}
|
20
src/EventListener/UsersRenamedListener.php
Normal file
20
src/EventListener/UsersRenamedListener.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -1,39 +1,27 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace src\PointToolsBundle\EventListener;
|
||||
namespace App\EventListener;
|
||||
|
||||
use App\Event\UsersRenamedEvent;
|
||||
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
|
||||
//use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
|
||||
//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
|
||||
{
|
||||
/**
|
||||
* @var UserRenameEvent[]
|
||||
*/
|
||||
private $renames = [];
|
||||
/** @var UserRenameEvent[] */
|
||||
private array $renameEntities = [];
|
||||
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
private $eventDispatcher;
|
||||
|
||||
|
||||
/**
|
||||
* UserRenameSubscriber constructor.
|
||||
*
|
||||
* @param EventDispatcherInterface $eventDispatcher
|
||||
*/
|
||||
public function __construct(EventDispatcherInterface $eventDispatcher)
|
||||
{
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
public function __construct(
|
||||
private readonly EventDispatcherInterface $eventDispatcher,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getSubscribedEvents(): array
|
||||
|
@ -54,27 +42,28 @@ class UsersUpdatedSubscriber implements EventSubscriber
|
|||
}
|
||||
|
||||
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
|
||||
{
|
||||
if (0 !== count($this->renames)) {
|
||||
if (0 !== count($this->renameEntities)) {
|
||||
// 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) {
|
||||
$em->persist($item);
|
||||
foreach ($this->renameEntities as $item) {
|
||||
$om->persist($item);
|
||||
}
|
||||
|
||||
$this->renames = [];
|
||||
$this->renameEntities = [];
|
||||
|
||||
$em->flush();
|
||||
$om->flush();
|
||||
|
||||
$this->eventDispatcher->dispatch(UsersRenamedEvent::NAME, $usersRenamedEvent);
|
||||
$this->eventDispatcher->dispatch($usersRenamedEvent, UsersRenamedEvent::NAME);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue