<?php
namespace App\EventSubscriber\Contest;
use App\Entity\Transaction;
use App\Event\Contest\ContestCreatedEvent;
use App\Factory\TransactionFactory;
use App\Helper\PlayerHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContestCreatedEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private EntityManagerInterface $entityManager, private PlayerHelper $playerHelper,
private TransactionFactory $transactionFactory
) {
}
public function onContestCreated(ContestCreatedEvent $contestCreatedEvent): void
{
$contest = $contestCreatedEvent->getContest();
$player = $contestCreatedEvent->getPlayer();
// ends here if the contest is part of a tournament
if ($contest->getPhase()) {
return;
}
// updates his activity if there is a config for it
$this->playerHelper->updateActivity($player, ContestCreatedEvent::class);
// removes the bet PS from the contest creator's PS
$this->transactionFactory
->forPlayer($player)
->takePs($contest->getBet())
->because(Transaction::CONTEST_CREATED_TYPE)
->save()
;
$this->entityManager->flush();
}
public static function getSubscribedEvents(): array
{
return [
ContestCreatedEvent::class => 'onContestCreated',
];
}
}