src/EventSubscriber/Contest/ContestCreatedEventSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Contest;
  3. use App\Entity\Transaction;
  4. use App\Event\Contest\ContestCreatedEvent;
  5. use App\Factory\TransactionFactory;
  6. use App\Helper\PlayerHelper;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ContestCreatedEventSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private EntityManagerInterface $entityManager, private PlayerHelper $playerHelper,
  13.         private TransactionFactory $transactionFactory
  14.     ) {
  15.     }
  16.     public function onContestCreated(ContestCreatedEvent $contestCreatedEvent): void
  17.     {
  18.         $contest $contestCreatedEvent->getContest();
  19.         $player $contestCreatedEvent->getPlayer();
  20.         // ends here if the contest is part of a tournament
  21.         if ($contest->getPhase()) {
  22.             return;
  23.         }
  24.         // updates his activity if there is a config for it
  25.         $this->playerHelper->updateActivity($playerContestCreatedEvent::class);
  26.         // removes the bet PS from the contest creator's PS
  27.         $this->transactionFactory
  28.             ->forPlayer($player)
  29.             ->takePs($contest->getBet())
  30.             ->because(Transaction::CONTEST_CREATED_TYPE)
  31.             ->save()
  32.         ;
  33.         $this->entityManager->flush();
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             ContestCreatedEvent::class => 'onContestCreated',
  39.         ];
  40.     }
  41. }