src/EventListener/ExpireSingleUseLinkListener.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventListener;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Harmonizely\Model\SingleUseLink;
  6. use Symfony\Component\EventDispatcher\GenericEvent;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Harmonizely\Model\EventInterface;
  9. use Webmozart\Assert\Assert;
  10. final class ExpireSingleUseLinkListener
  11. {
  12.     private $requestStack;
  13.     private $entityManager;
  14.     public function __construct(RequestStack $requestStackEntityManagerInterface $entityManager)
  15.     {
  16.         $this->requestStack $requestStack;
  17.         $this->entityManager $entityManager;
  18.     }
  19.     public function expireSingleUseLink(GenericEvent $event): void
  20.     {
  21.         $request $this->requestStack->getCurrentRequest();
  22.         if (!$request->query->has('single_use_link_code')) {
  23.             return;
  24.         }
  25.         $singleUseLinkCode $request->query->get('single_use_link_code');
  26.         /** @var SingleUseLink $singleUseLink */
  27.         $singleUseLink $this->entityManager
  28.             ->getRepository(SingleUseLink::class)
  29.             ->findOneBy(['code' => $singleUseLinkCode]);
  30.         if (null === $singleUseLink) {
  31.             return;
  32.         }
  33.         $singleUseLink->setExpired(true);
  34.         /** @var EventInterface $eventModel */
  35.         $eventModel $event->getSubject();
  36.         Assert::isInstanceOf($eventModelEventInterface::class);
  37.         $eventModel->setSingleUseLink($singleUseLink);
  38.         $this->entityManager->flush();
  39.     }
  40. }