<?php
declare(strict_types=1);
namespace Harmonizely\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Harmonizely\Model\SingleUseLink;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Harmonizely\Model\EventInterface;
use Webmozart\Assert\Assert;
final class ExpireSingleUseLinkListener
{
private $requestStack;
private $entityManager;
public function __construct(RequestStack $requestStack, EntityManagerInterface $entityManager)
{
$this->requestStack = $requestStack;
$this->entityManager = $entityManager;
}
public function expireSingleUseLink(GenericEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
if (!$request->query->has('single_use_link_code')) {
return;
}
$singleUseLinkCode = $request->query->get('single_use_link_code');
/** @var SingleUseLink $singleUseLink */
$singleUseLink = $this->entityManager
->getRepository(SingleUseLink::class)
->findOneBy(['code' => $singleUseLinkCode]);
if (null === $singleUseLink) {
return;
}
$singleUseLink->setExpired(true);
/** @var EventInterface $eventModel */
$eventModel = $event->getSubject();
Assert::isInstanceOf($eventModel, EventInterface::class);
$eventModel->setSingleUseLink($singleUseLink);
$this->entityManager->flush();
}
}