src/Bundles/EpidemiologicalSurveillanceBundle/EventSubscriber/SurveillanceCaseTypeSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Bundles\EpidemiologicalSurveillanceBundle\EventSubscriber;
  4. use App\Bundles\EpidemiologicalSurveillanceBundle\Enum\SurveillanceCaseTypeEnum;
  5. use App\Bundles\EpidemiologicalSurveillanceBundle\Repository\SurveillanceCaseRepository;
  6. use App\Bundles\EpidemiologicalSurveillanceBundle\Service\Template\WeekReportService;
  7. use App\Bundles\LocationBundle\Entity\Location;
  8. use App\Bundles\LocationBundle\Enum\LocationTypeEnum;
  9. use App\Bundles\LocationBundle\Repository\LocationRepository;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Form\FormEvent;
  12. use Symfony\Component\Form\FormEvents;
  13. class SurveillanceCaseTypeSubscriber implements EventSubscriberInterface
  14. {
  15.     public function __construct(
  16.         private readonly LocationRepository $locationRepository,
  17.         private readonly SurveillanceCaseRepository $surveillanceCaseRepository,
  18.     ) {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             FormEvents::PRE_SUBMIT => 'onFormSubmit',
  24.         ];
  25.     }
  26.     public function onFormSubmit(FormEvent $event): void
  27.     {
  28.         /** @var SurveillanceCaseTypeEnum $type */
  29.         $type $event->getForm()->getConfig()->getOption('type');
  30.         $data $event->getData();
  31.         $this->setValidIndividualNumberAndYear($type$data);
  32.         $this->resolveLocationData($data);
  33.         $event->setData($data);
  34.     }
  35.     private function resolveLocationData(array &$data): void
  36.     {
  37.         $filters $data['filters'] ?? [];
  38.         if (empty($filters)) {
  39.             return;
  40.         }
  41.         $data['location'] = $filters['clinicalData']['state'];
  42.     }
  43.     private function setValidIndividualNumberAndYear(SurveillanceCaseTypeEnum $type, array &$data): void
  44.     {
  45.         $filters $data['filters'] ?? [];
  46.         if (empty($filters)) {
  47.             return;
  48.         }
  49.         $week = (int) $filters['week'];
  50.         $year = (int) $filters['year'];
  51.         $locationId $data['location'];
  52.         /** @var Location $location */
  53.         $location $this->locationRepository->find($locationId);
  54.         /** @var Location $location */
  55.         $location $this->resolveLocation($location);
  56.         $latestIndividualNumber $this->surveillanceCaseRepository->getLatestIndividualNumber(
  57.             $type,
  58.             $location,
  59.             $this->getStartSeason($year$week),
  60.             $this->getEndSeason($year$week)
  61.         );
  62.         $data['individualNumber'] = $latestIndividualNumber 1;
  63.     }
  64.     private function resolveLocation(Location $location): ?Location
  65.     {
  66.         return match ($location->getType()) {
  67.             LocationTypeEnum::DISTRICTLocationTypeEnum::SUB_DISTRICT => $location->getState(),
  68.             LocationTypeEnum::CITY_DISTRICT => $location->getCity(),
  69.             default => $location,
  70.         };
  71.     }
  72.     private function getStartSeason(int $yearint $week): int
  73.     {
  74.         if ($week >= WeekReportService::START_SEASON_START_WEEK) {
  75.             return $year;
  76.         }
  77.         return $year 1;
  78.     }
  79.     private function getEndSeason(int $yearint $week): int
  80.     {
  81.         if ($week >= WeekReportService::START_SEASON_START_WEEK) {
  82.             return $year 1;
  83.         }
  84.         return $year;
  85.     }
  86. }