Commit bcd9d0ae authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

HistoriqueListener : précédemment, on injectait le AuthenticationService mais...

HistoriqueListener : précédemment, on injectait le AuthenticationService mais cela créait une boucle infinie de dépendances de services. Donc on a choisi d'injecter le service manager :-( et c'est HistoriqueListener qui récupère lui-même le AuthenticationService au moment opportun.
parent f4f3a483
Loading
Loading
Loading
Loading
Loading
+12 −10
Original line number Diff line number Diff line
@@ -3,9 +3,10 @@
namespace UnicaenAuth\ORM\Event\Listeners;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;
use Psr\Container\ContainerInterface;
use RuntimeException;
use UnicaenApp\Entity\HistoriqueAwareInterface;
use UnicaenAuth\Entity\Db\AbstractUser;
@@ -24,10 +25,7 @@ use Laminas\Authentication\AuthenticationService;
 */
class HistoriqueListener implements EventSubscriber
{
    /**
     * @var AuthenticationService
     */
    private $authenticationService;
    protected ContainerInterface $container;

    /**
     * @var mixed
@@ -35,11 +33,14 @@ class HistoriqueListener implements EventSubscriber
    protected $identity;

    /**
     * @param AuthenticationService $authenticationService
     * **ATTENTION : Précédemment, on injectait {@see \Laminas\Authentication\AuthenticationService}
     * mais cela créait une boucle infinie de dépendances de services.
     * Donc on a choisi d'injecter le service manager :-( et c'est HistoriqueListener qui récupère
     * lui-même le {@see \Laminas\Authentication\AuthenticationService} au moment opportun.**
     */
    public function setAuthenticationService(AuthenticationService $authenticationService)
    public function __construct(ContainerInterface $container)
    {
        $this->authenticationService = $authenticationService;
        $this->container = $container;
    }

    /**
@@ -48,7 +49,7 @@ class HistoriqueListener implements EventSubscriber
     */
    protected function updateHistorique(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $entity = $args->getObject();

        // l'entité doit implémenter l'interface requise
        if (! $entity instanceof HistoriqueAwareInterface) {
@@ -147,7 +148,8 @@ class HistoriqueListener implements EventSubscriber
    public function getIdentity()
    {
        if (null === $this->identity) {
            $authenticationService = $this->authenticationService;
            /** @var AuthenticationService $authenticationService */
            $authenticationService = $this->container->get(AuthenticationService::class);
            if ($authenticationService->hasIdentity()) {
                $this->identity = $authenticationService->getIdentity();
            }
+9 −15
Original line number Diff line number Diff line
@@ -2,16 +2,10 @@

namespace UnicaenAuth\ORM\Event\Listeners;

use Psr\Container\ContainerInterface;
use Laminas\Authentication\AuthenticationService;
use Laminas\ServiceManager\FactoryInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Psr\Container\ContainerInterface;

/**
 * Description of MouchardServiceFactory
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
class HistoriqueListenerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
@@ -19,14 +13,14 @@ class HistoriqueListenerFactory implements FactoryInterface
        return $this->__invoke($serviceLocator, '?');
    }

    /**
     * **ATTENTION : Précédemment, on injectait {@see \Laminas\Authentication\AuthenticationService}
     * mais cela créait une boucle infinie de dépendances de services.
     * Donc on a choisi d'injecter le service manager :-( et c'est HistoriqueListener qui récupère
     * lui-même le {@see \Laminas\Authentication\AuthenticationService} au moment opportun.**
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        /** @var AuthenticationService $authenticationService */
        $authenticationService = $container->get('Laminas\Authentication\AuthenticationService');

        $listener = new HistoriqueListener();
        $listener->setAuthenticationService($authenticationService);

        return $listener;
        return new HistoriqueListener($container);
    }
}