<?php declare(strict_types=1);
namespace App\Subscriber;
use App\Doctrine\Common\Cache\TaggedMemcachedCache;
use App\Entity\User;
use App\Event\EmailVerifiedEvent;
use App\Event\UserRegisteredEvent;
use App\RabbitMq\Handler\RegistrationStatsHandler;
use App\RabbitMq\TasksPool;
use App\Repository\Message\MessageReceptionRepository;
use App\Service\StaticMessageProvider;
use LogicException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RegistrationStatsSubscriber implements EventSubscriberInterface
{
private TasksPool $tasksPool;
private MessageReceptionRepository $messageReceptionRepository;
private StaticMessageProvider $staticMessageProvider;
private TaggedMemcachedCache $taggedMemcachedCache;
public function __construct(TasksPool $tasksPool, MessageReceptionRepository $messageReceptionRepository, StaticMessageProvider $staticMessageProvider, TaggedMemcachedCache $taggedMemcachedCache)
{
$this->tasksPool = $tasksPool;
$this->messageReceptionRepository = $messageReceptionRepository;
$this->staticMessageProvider = $staticMessageProvider;
$this->taggedMemcachedCache = $taggedMemcachedCache;
}
public function onUserRegistered(UserRegisteredEvent $event): void
{
if (null === $event->getUser()->getId()) {
throw new LogicException('User must have an id.');
}
$this->tasksPool->addTask(
RegistrationStatsHandler::class,
'handle',
User::class,
$event->getUser()->getId(),
[RegistrationStatsHandler::REGISTERED]
);
}
public function onEmailVerified(EmailVerifiedEvent $event): void
{
$user = $event->getVerification()->getUser();
if (null === $user) {
throw new LogicException('Verification must have a User.');
}
if (null === $user->getId()) {
throw new LogicException('User must have an id.');
}
$this->tasksPool->addTask(
RegistrationStatsHandler::class,
'handle',
User::class,
$user->getId(),
[RegistrationStatsHandler::VALIDATED]
);
$welcomeMessage = $this->staticMessageProvider->getWelcomeMessage();
assert(null !== $welcomeMessage->getId());
$this->messageReceptionRepository->sendTo($welcomeMessage->getId(), [$user->getId()], $this->taggedMemcachedCache, null);
}
/**
* @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
*/
public static function getSubscribedEvents(): array
{
return [
UserRegisteredEvent::class => 'onUserRegistered',
EmailVerifiedEvent::class => 'onEmailVerified',
];
}
}