src/EventSubscriber/Tournament/TournamentRegistrationClosedEventSubscriber.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Tournament;
  3. use App\Entity\Notification;
  4. use App\Entity\Phase;
  5. use App\Event\Tournament\TournamentRegistrationClosedEvent;
  6. use DateInterval;
  7. use DateTime;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class TournamentRegistrationClosedEventSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(private EntityManagerInterface $entityManager)
  13.     {
  14.     }
  15.     public function onTournamentRegistrationClosedEvent(TournamentRegistrationClosedEvent $tournamentRegistrationClosedEvent): void
  16.     {
  17.         $tournament $tournamentRegistrationClosedEvent->getTournament();
  18.         $currentDateTime = new DateTime();
  19.         // used to ease with the calculation of the tournament start date
  20.         $supposedStartingTime $tournament->getStartingTime()->setDate(
  21.             (int) $currentDateTime->format('Y'),
  22.             (int) $currentDateTime->format('m'),
  23.             (int) $currentDateTime->format('d'),
  24.         );
  25.         // sets the tournament start date if the supposedStartingDate is later today
  26.         if ($supposedStartingTime $currentDateTime) {
  27.             $tournament->setStartDate($supposedStartingTime);
  28.         } else {
  29.             // otherwise, we add one day to the supposed starting date and set it as the tournament start date
  30.             $tournament->setStartDate(
  31.                 $supposedStartingTime->add(
  32.                     new DateInterval(sprintf('P%sD'1))
  33.                 )
  34.             );
  35.         }
  36.         // creates the tournament phases
  37.         for ($i 0$i $tournament->getPhaseCount(); ++$i) {
  38.             /** @var Phase|false $previousPhase */
  39.             $previousPhase $tournament->getPhases()->last();
  40.             if ($previousPhase) {
  41.                 $startDate $previousPhase->getEndDate();
  42.                 $endDate $startDate->add(new DateInterval(sprintf('PT%sH'$tournament->getPhaseDuration())));
  43.             } else {
  44.                 $startDate $tournament->getStartDate();
  45.                 $endDate $tournament->getStartDate()->add(new DateInterval(sprintf('PT%sH'$tournament->getPhaseDuration())));
  46.             }
  47.             $tournament->addPhase(
  48.                 (new Phase())
  49.                     ->setGame($tournament->getGame())
  50.                     ->setStartDate($startDate)
  51.                     ->setEndDate($endDate)
  52.             );
  53.         }
  54.         $players $tournament->getPlayers();
  55.         foreach ($players as $player) {
  56.             $player->addNotification(
  57.                 (new Notification())
  58.                     ->setType(Notification::TOURNAMENT_REGISTRATION_CLOSED)
  59.                     ->setMessage(sprintf(
  60.                         '%s : Préparez-vous ! Le tournoi démarre le %s, à %s.',
  61.                         $tournament->getName(),
  62.                         $tournament->getStartDate()->format('jS M'),
  63.                         $tournament->getStartDate()->format('H:i')
  64.                     ))
  65.                     ->setExtraData(['tournamentId' => $tournament->getId()])
  66.             );
  67.         }
  68.         $this->entityManager->flush();
  69.     }
  70.     public static function getSubscribedEvents(): array
  71.     {
  72.         return [
  73.             TournamentRegistrationClosedEvent::class => 'onTournamentRegistrationClosedEvent',
  74.         ];
  75.     }
  76. }