src/EventSubscriber/Contest/ContestCancelledEventSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Contest;
  3. use App\Entity\Notification;
  4. use App\Entity\Transaction;
  5. use App\Event\Contest\ContestCancelledEvent;
  6. use App\Factory\TransactionFactory;
  7. use App\Helper\ContestHelper;
  8. use App\Helper\PlayerHelper;
  9. use App\Repository\BetConfigRepository;
  10. use App\Repository\NotificationRepository;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class ContestCancelledEventSubscriber implements EventSubscriberInterface
  14. {
  15.     public function __construct(
  16.         private EntityManagerInterface $entityManager, private PlayerHelper $playerHelper,
  17.         private BetConfigRepository $betConfigRepository, private NotificationRepository $notificationRepository,
  18.         private TransactionFactory $transactionFactory
  19.     ) {
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ContestCancelledEvent::class => 'onContestCancelled',
  25.         ];
  26.     }
  27.     public function onContestCancelled(ContestCancelledEvent $contestCancelledEvent): void
  28.     {
  29.         $contest $contestCancelledEvent->getContest();
  30.         $contestCreator $contest->getCreatedBy();
  31.         // refunds the contest's creator if he is the one who cancelled it
  32.         if ($contestCancelledEvent->isTriggeredByCreator()) {
  33.             $this->transactionFactory
  34.                 ->forPlayer($contestCreator)
  35.                 ->givePs($contest->getBet())
  36.                 ->because(Transaction::REFUNDED_PS_DUE_TO_CONTEST_CANCELLATION_TYPE)
  37.                 ->save()
  38.             ;
  39.             $this->entityManager->flush();
  40.             return;
  41.         }
  42.         // updates the contest creator activity and notifies him if the contest has been cancelled because he hasn't scored on time
  43.         if (!ContestHelper::hasScored($contest$contestCreator)) {
  44.             $this->playerHelper->updateActivity($contestCreatorContestCancelledEvent::class);
  45.             $contestCreator->addNotification(
  46.                 (new Notification())
  47.                     ->setType(Notification::CONTEST_CANCELLED_TYPE)
  48.                     ->setContest($contest)
  49.                     ->setMessage(sprintf('Votre match de %s a été annulé pour cause d\'inactivité de votre part.'$contest->getGame()->getName()))
  50.             );
  51.             $this->entityManager->flush();
  52.             return;
  53.         }
  54.         // Updates all invite notifications for this Contest to "archived"
  55.         $this->notificationRepository->updateBy(
  56.             ['archived' => true],
  57.             [
  58.                 'contest' => $contest,
  59.                 'type'    => Notification::CONTEST_INVITATION_TYPE,
  60.             ]
  61.         );
  62.         // loops through all opponents of this contest who has not scored, alters their activity
  63.         foreach ($contest->getContestPlayers() as $contestPlayer) {
  64.             $player $contestPlayer->getPlayer();
  65.             // we don't need to check if the current player is an opponent or the creator here because we already checked above
  66.             // so we are sure that we are only getting opponents who have not scored
  67.             if (ContestHelper::hasScored($contest$player)) {
  68.                 continue;
  69.             }
  70.             $this->playerHelper->updateActivity($playerContestCancelledEvent::class);
  71.             $player->addNotification(
  72.                 (new Notification())
  73.                     ->setType(Notification::CONTEST_CANCELLED_TYPE)
  74.                     ->setContest($contest)
  75.                     ->setMessage(sprintf('Votre match de %s a été annulé pour cause d\'inactivité de votre part.'$contest->getGame()->getName()))
  76.             );
  77.         }
  78.         // the contest's creator automatically becomes the winner as his opponents are inactive
  79.         $winner $contestCreator;
  80.         $betConfig $this->betConfigRepository->findOneBy([
  81.             'initialValue' => $contest->getBet(),
  82.         ]);
  83.         $contest->setWinner($winner);
  84.         $winner
  85.             ->setXp($winner->getXp() + ContestHelper::getXpToEarn($contest$winner))
  86.         ;
  87.         $this->transactionFactory
  88.             ->forPlayer($winner)
  89.             ->givePs($betConfig->getWinningValue())
  90.             ->because(Transaction::CONTEST_WINNER_TYPE)
  91.             ->save()
  92.         ;
  93.         $winner->addNotification(
  94.             (new Notification())
  95.                 ->setContest($contest)
  96.                 ->setType(Notification::CONTEST_CLOSED_TYPE)
  97.                 ->setMessage(sprintf('%s, vous avez gagné un match.'$winner->getUser()->getUsername()))
  98.         );
  99.         $this->entityManager->flush();
  100.     }
  101. }