src/EventSubscriber/RankingKindEventSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\AdmissionRegister;
  4. use App\Entity\AdmissionUser;
  5. use App\Entity\Payment;
  6. use App\Event\PaymentEvent;
  7. use App\Event\RankingKindEvent;
  8. use App\Services\AdmissionUserService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  12. class RankingKindEventSubscriber implements EventSubscriberInterface
  13. {
  14.     protected EntityManagerInterface $em;
  15.     public function __construct(EntityManagerInterface $em)
  16.     {
  17.         $this->em $em;
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             RankingKindEvent::CAPACITY_CHANGE => 'onCapacityChange',
  23.         ];
  24.     }
  25.     public function onCapacityChange(RankingKindEvent $rankingKindEvent): void
  26.     {
  27.         $rankingKind $rankingKindEvent->getRankingKind();
  28.         if (!$rankingKind) {
  29.             return;
  30.         }
  31.         $summaryData $this->em->getRepository(AdmissionRegister::class)->getRankingKindSummary($rankingKind);
  32.         if (!(is_array($summaryData) && count($summaryData) === 1)) {
  33.             return;
  34.         }
  35.         $summaryData $summaryData[0];
  36.         /*
  37. ^ array:1 [
  38.   0 => array:2 [
  39.     "capacity" => "240"
  40.     "minScore" => 550.0
  41.   ]
  42. ]
  43.          */
  44.         $rankingKind->setMinScore($summaryData['minScore']);
  45.         $rankingKind->setCapacityTotal($summaryData['capacity']);
  46.         if (!$rankingKind->getCapacity()) {
  47.             $rankingKind->setCapacity($summaryData['capacity']);
  48.         }
  49.         $this->em->flush();
  50.     }
  51. }