Skip to content

Commit

Permalink
Merge pull request #3351 from MTES-MCT/feature/3346-disable-entity-li…
Browse files Browse the repository at this point in the history
…stener

[TECH] Option pour désactiver EntityHistoryListener
  • Loading branch information
numew authored Nov 29, 2024
2 parents a19b900 + e6b3a7f commit 57f0da4
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/EventListener/Behaviour/DoctrineListenerRemoverTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\EventListener\Behaviour;

use Doctrine\Common\EventManager;

trait DoctrineListenerRemoverTrait
{
public function removeListener(EventManager $eventManager, string $listenerClass, string $event): void
{
foreach ($eventManager->getListeners($event) as $listener) {
if ($listener instanceof $listenerClass) {
$eventManager->removeEventListener([$event], $listener);
}
}
}

public function removeListeners(EventManager $eventManager, string $listenerClass, array $events): void
{
foreach ($events as $event) {
$this->removeListener($eventManager, $listenerClass, $event);
}
}
}
18 changes: 18 additions & 0 deletions src/Manager/HistoryEntryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@
use App\Entity\HistoryEntry;
use App\Entity\Partner;
use App\Entity\Signalement;
use App\EventListener\Behaviour\DoctrineListenerRemoverTrait;
use App\EventListener\EntityHistoryListener;
use App\Factory\HistoryEntryFactory;
use App\Repository\AffectationRepository;
use App\Repository\HistoryEntryRepository;
use App\Repository\PartnerRepository;
use App\Service\TimezoneProvider;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Exception\ExceptionInterface;

class HistoryEntryManager extends AbstractManager
{
use DoctrineListenerRemoverTrait;

public const FORMAT_DATE_TIME = 'Y-m-d H:i:s';

public function __construct(
Expand Down Expand Up @@ -101,6 +107,18 @@ public function getAffectationHistory(int $signalementId): array
return $formattedHistory;
}

public function removeEntityListeners(): void
{
/** @var EntityManagerInterface $objectManager */
$objectManager = $this->managerRegistry->getManager();
$eventManager = $objectManager->getEventManager();
$this->removeListeners(
$eventManager,
EntityHistoryListener::class,
[Events::postPersist, Events::postUpdate, Events::preRemove]
);
}

private function getHistoryEntries(int $signalementId, string $entityClass, ?HistoryEntryEvent $event = null): array
{
$criteria = ['entityName' => str_replace($this->historyEntryFactory::ENTITY_PROXY_PREFIX, '', $entityClass)];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Tests\Unit\EventListener\Behaviour;

use App\EventListener\Behaviour\DoctrineListenerRemoverTrait;
use Doctrine\Common\EventManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class DoctrineListenerRemoverTraitTest extends TestCase
{
private MockObject|EventManager $eventManagerMock;

protected function setUp(): void
{
$this->eventManagerMock = $this->createMock(EventManager::class);
}

public function testRemoveListenerWhenClassUsingTrait(): void
{
$listener = $this->createMock(ListenerToRemove::class);

$this->eventManagerMock
->expects($this->once())
->method('getListeners')
->with('postPersist')
->willReturn([$listener]);

$this->eventManagerMock
->expects($this->once())
->method('removeEventListener')
->with(['postPersist'], $listener);

$classUsingTrait = new class {
use DoctrineListenerRemoverTrait;
};

$classUsingTrait->removeListener(
$this->eventManagerMock,
ListenerToRemove::class,
'postPersist'
);
}

public function testDoNotRemoveListenerWhenClassNotUsingTrait(): void
{
$listener = $this->createMock(Listener::class);

$this->eventManagerMock
->expects($this->once())
->method('getListeners')
->with('postPersist')
->willReturn([$listener]);

$this->eventManagerMock
->expects($this->never())
->method('removeEventListener');

$classUsingTrait = new class {
use DoctrineListenerRemoverTrait;
};

$classUsingTrait->removeListener(
$this->eventManagerMock,
ListenerToRemove::class,
'postPersist'
);
}
}

class ListenerToRemove // As EntityHistoryListener
{
}

class Listener
{
}

0 comments on commit 57f0da4

Please sign in to comment.