src/Component/ProductComparer/EventListener/LogoutListener.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\ProductComparer\EventListener;
  4. use BitBag\OpenMarketplace\Component\ProductComparer\Entity\ComparerInterface;
  5. use BitBag\OpenMarketplace\Component\ProductComparer\Repository\ComparerRepositoryInterface;
  6. use BitBag\OpenMarketplace\Component\Vendor\Entity\ShopUserInterface;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\Security\Http\Event\LogoutEvent;
  9. final class LogoutListener
  10. {
  11.     public function __construct(
  12.         private ComparerRepositoryInterface $comparerRepository,
  13.         private EntityManagerInterface $entityManager,
  14.     ) {
  15.     }
  16.     public function onLogout(LogoutEvent $event): void
  17.     {
  18.         $token $event->getToken();
  19.         if (null === $token) {
  20.             return;
  21.         }
  22.         $shopUser $token->getUser();
  23.         if (!$shopUser instanceof ShopUserInterface) {
  24.             return;
  25.         }
  26.         $comparer $this->comparerRepository->findOneByShopUser($shopUser);
  27.         if (!$comparer instanceof ComparerInterface) {
  28.             return;
  29.         }
  30.         foreach ($comparer->getComparerProducts() as $comparerProduct) {
  31.             $comparer->removeComparerProduct($comparerProduct);
  32.         }
  33.         $this->entityManager->persist($comparer);
  34.         $this->entityManager->flush();
  35.     }
  36. }