<?php
namespace App\EventSubscriber;
use App\Entity\AdmissionRegister;
use App\Entity\AdmissionUser;
use App\Entity\Payment;
use App\Event\PaymentEvent;
use App\Event\RankingKindEvent;
use App\Services\AdmissionUserService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
class RankingKindEventSubscriber implements EventSubscriberInterface
{
protected EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public static function getSubscribedEvents()
{
return [
RankingKindEvent::CAPACITY_CHANGE => 'onCapacityChange',
];
}
public function onCapacityChange(RankingKindEvent $rankingKindEvent): void
{
$rankingKind = $rankingKindEvent->getRankingKind();
if (!$rankingKind) {
return;
}
$summaryData = $this->em->getRepository(AdmissionRegister::class)->getRankingKindSummary($rankingKind);
if (!(is_array($summaryData) && count($summaryData) === 1)) {
return;
}
$summaryData = $summaryData[0];
/*
^ array:1 [
0 => array:2 [
"capacity" => "240"
"minScore" => 550.0
]
]
*/
$rankingKind->setMinScore($summaryData['minScore']);
$rankingKind->setCapacityTotal($summaryData['capacity']);
if (!$rankingKind->getCapacity()) {
$rankingKind->setCapacity($summaryData['capacity']);
}
$this->em->flush();
}
}