src/App/Subscriber/RegistrationStatsSubscriber.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Subscriber;
  3. use App\Doctrine\Common\Cache\TaggedMemcachedCache;
  4. use App\Entity\User;
  5. use App\Event\EmailVerifiedEvent;
  6. use App\Event\UserRegisteredEvent;
  7. use App\RabbitMq\Handler\RegistrationStatsHandler;
  8. use App\RabbitMq\TasksPool;
  9. use App\Repository\Message\MessageReceptionRepository;
  10. use App\Service\StaticMessageProvider;
  11. use LogicException;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class RegistrationStatsSubscriber implements EventSubscriberInterface
  14. {
  15.     private TasksPool $tasksPool;
  16.     private MessageReceptionRepository $messageReceptionRepository;
  17.     private StaticMessageProvider $staticMessageProvider;
  18.     private TaggedMemcachedCache $taggedMemcachedCache;
  19.     public function __construct(TasksPool $tasksPoolMessageReceptionRepository $messageReceptionRepositoryStaticMessageProvider $staticMessageProviderTaggedMemcachedCache $taggedMemcachedCache)
  20.     {
  21.         $this->tasksPool $tasksPool;
  22.         $this->messageReceptionRepository $messageReceptionRepository;
  23.         $this->staticMessageProvider $staticMessageProvider;
  24.         $this->taggedMemcachedCache $taggedMemcachedCache;
  25.     }
  26.     public function onUserRegistered(UserRegisteredEvent $event): void
  27.     {
  28.         if (null === $event->getUser()->getId()) {
  29.             throw new LogicException('User must have an id.');
  30.         }
  31.         $this->tasksPool->addTask(
  32.             RegistrationStatsHandler::class,
  33.             'handle',
  34.             User::class,
  35.             $event->getUser()->getId(),
  36.             [RegistrationStatsHandler::REGISTERED]
  37.         );
  38.     }
  39.     public function onEmailVerified(EmailVerifiedEvent $event): void
  40.     {
  41.         $user $event->getVerification()->getUser();
  42.         if (null === $user) {
  43.             throw new LogicException('Verification must have a User.');
  44.         }
  45.         if (null === $user->getId()) {
  46.             throw new LogicException('User must have an id.');
  47.         }
  48.         $this->tasksPool->addTask(
  49.             RegistrationStatsHandler::class,
  50.             'handle',
  51.             User::class,
  52.             $user->getId(),
  53.             [RegistrationStatsHandler::VALIDATED]
  54.         );
  55.         $welcomeMessage $this->staticMessageProvider->getWelcomeMessage();
  56.         assert(null !== $welcomeMessage->getId());
  57.         $this->messageReceptionRepository->sendTo($welcomeMessage->getId(), [$user->getId()], $this->taggedMemcachedCachenull);
  58.     }
  59.     /**
  60.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  61.      */
  62.     public static function getSubscribedEvents(): array
  63.     {
  64.         return [
  65.             UserRegisteredEvent::class => 'onUserRegistered',
  66.             EmailVerifiedEvent::class => 'onEmailVerified',
  67.         ];
  68.     }
  69. }