src/EventSubscriber/Tournament/TournamentCancelledEventSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Tournament;
  3. use App\Entity\Contest;
  4. use App\Entity\Phase;
  5. use App\Entity\Transaction;
  6. use App\Event\Tournament\TournamentCancelledEvent;
  7. use App\Factory\TransactionFactory;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class TournamentCancelledEventSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         private EntityManagerInterface $entityManager, private TransactionFactory $transactionFactory
  14.     ) {
  15.     }
  16.     public function onTournamentCancelledEvent(TournamentCancelledEvent $tournamentCancelledEvent): void
  17.     {
  18.         $tournament $tournamentCancelledEvent->getTournament();
  19.         if ($tournament->getPlayers()->isEmpty()) {
  20.             return;
  21.         }
  22.         // refunds the tickets
  23.         foreach ($tournament->getPlayers() as $player) {
  24.             $this->transactionFactory
  25.                 ->forPlayer($player)
  26.                 ->giveTickets($tournament->getCost())
  27.                 ->because(Transaction::REFUND_REGISTER_IN_TOURNAMENT_TYPE)
  28.                 ->save()
  29.             ;
  30.         }
  31.         if ($tournament->getPhases()->isEmpty()) {
  32.             return;
  33.         }
  34.         foreach ($tournament->getPhases() as $phase) {
  35.             if ($phase->getStatus() === Phase::ENDED_STATUS) {
  36.                 continue;
  37.             }
  38.             if ($phase->getContests()->isEmpty()) {
  39.                 continue;
  40.             }
  41.             foreach ($phase->getContests() as $contest) {
  42.                 // no need to dispatch the event here
  43.                 $contest->setStatus(Contest::CANCELLED_STATUS);
  44.             }
  45.             $phase->setStatus(Phase::CANCELLED_STATUS);
  46.         }
  47.         $this->entityManager->flush();
  48.     }
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             TournamentCancelledEvent::class => 'onTournamentCancelledEvent',
  53.         ];
  54.     }
  55. }