src/EventSubscriber/Tournament/Contest/ContestCancelledEventSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Tournament\Contest;
  3. use App\Event\Tournament\Contest\ContestCancelledEvent;
  4. use App\Helper\ContestHelper;
  5. use App\Helper\PlayerHelper;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class ContestCancelledEventSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(
  11.         private EntityManagerInterface $entityManager, private PlayerHelper $playerHelper
  12.     ) {
  13.     }
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             ContestCancelledEvent::class => 'onContestCancelled',
  18.         ];
  19.     }
  20.     public function onContestCancelled(ContestCancelledEvent $contestCancelledEvent): void
  21.     {
  22.         $contest $contestCancelledEvent->getContest();
  23.         // loops through all opponents of this contest and alter the loser's activity and set the only one who scored as winner
  24.         foreach ($contest->getContestPlayers() as $contestPlayer) {
  25.             $player $contestPlayer->getPlayer();
  26.             if (!ContestHelper::hasScored($contest$player)) {
  27.                 $this->playerHelper->updateActivity($playerContestCancelledEvent::class);
  28.                 continue;
  29.             }
  30.             // the only one who scored automatically wins
  31.             $contest->setWinner($player);
  32.             $player->setXp(
  33.                 $player->getXp() + ContestHelper::getXpToEarn($contest$player)
  34.             );
  35.             $this->entityManager->flush();
  36.         }
  37.     }
  38. }