src/EventSubscriber/CalculateFilterResultsSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\WebList;
  4. use App\Entity\Selection;
  5. use App\Entity\WebFilter;
  6. use App\Service\ProductApi;
  7. use App\Entity\EditionsPlanner;
  8. use App\Service\FilterCalculationService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use ApiPlatform\Core\EventListener\EventPriorities;
  14. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. final class CalculateFilterResultsSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var EntityManagerInterface
  20.      */
  21.     protected $em;
  22.     /**
  23.      * @var ProductApi
  24.      */
  25.     protected $productApi;
  26.     /**
  27.      * @var FilterCalculationService
  28.      */
  29.     protected $filterCalculationService;
  30.     public function __construct(EntityManagerInterface $emProductApi $productApiFilterCalculationService $filterCalculationService)
  31.     {
  32.         $this->em $em;
  33.         $this->productApi $productApi;
  34.         $this->filterCalculationService $filterCalculationService// new FilterCalculationService($em, $productApi);
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             KernelEvents::VIEW => ['postPersist'EventPriorities::POST_WRITE],
  40.         ];
  41.     }
  42.     public function postPersist(ViewEvent $event): void
  43.     {
  44.         $object $event->getControllerResult();
  45.         $request $event->getRequest();
  46.         if(!$object instanceof WebList && !$object instanceof Selection && !$object instanceof WebFilter) {
  47.             return;
  48.         }
  49.         if(!in_array($request->getMethod(), [Request::METHOD_POSTRequest::METHOD_PUT])){
  50.             return;
  51.         }
  52.         $attributes RequestAttributesExtractor::extractAttributes($request);
  53.         // ND 24/07/2024 : maintenant que le code de calcul des filtres est optimisé, on fait les traitements directement à l'enregistrement - fini les tâches de fond
  54.         switch ($attributes['resource_class'])
  55.         {
  56.             case WebList::class:
  57.                 $selectionId $object->getSelection()->getId();
  58.                 $this->filterCalculationService->deleteResultsForSelection($selectionId);
  59.                 $this->filterCalculationService->calculateSelection($selectionId);
  60.                 break;
  61.             case Selection::class:
  62.                 $selectionId $object->getId();
  63.                 $this->filterCalculationService->deleteResultsForSelection($selectionId);
  64.                 $this->filterCalculationService->calculateSelection($selectionId);
  65.                 break;
  66.             case WebFilter::class:
  67.                 $filterId $object->getId();
  68.                 $this->filterCalculationService->deleteResultsForFilter($filterId);
  69.                 $this->filterCalculationService->calculateFilterOnAllWebSelection($filterId);
  70.                 break;
  71.             default:
  72.                 return;
  73.         }
  74.         return;
  75. /*
  76.         // on choppe toutes les taches edition planner de type FilterCalculationService en attente -> si y'en a on return
  77.         $tasks = $this->em->getRepository(EditionsPlanner::class)->findBy(['serviceName' => '\App\Service\FilterCalculationService', 'state' => 0]);
  78.         // On vérifie si une tâche similaire est déjà en attente
  79.         if(count($tasks) > 0) {
  80.             foreach($tasks as $task) {
  81.                 $param = unserialize($task->getParams());
  82.                 if(isset($param['selection']) && $object instanceof Selection && $param['selection'] == $object->getId()) {
  83.                     return;
  84.                 }
  85.                 if(isset($param['selection']) && $object instanceof WebList && $param['selection'] == $object->getSelection()->getId()) {
  86.                     return;
  87.                 }
  88.                 if(isset($param['filter']) && $object instanceof WebFilter && $param['filter'] == $object->getId()) {
  89.                     return;
  90.                 }
  91.             }
  92.         }
  93.         $attributes = RequestAttributesExtractor::extractAttributes($request);
  94.         $task = new EditionsPlanner();
  95.         $task->setServiceName('\App\Service\FilterCalculationService');
  96.         $task->setLog('En attente');
  97.         $task->setState(0);
  98.         $task->setCreatedAt(new \DateTime());
  99.         $task->setProcessedAt(null);
  100.         $taskParams = array();
  101.         $tmpName = 'selection';
  102.         switch ($attributes['resource_class'])
  103.         {
  104.             case WebList::class:
  105.                 $taskParams['selection'] = $object->getSelection()->getId();
  106.                 break;
  107.             case Selection::class:
  108.                 $taskParams['selection'] = $object->getId();
  109.                 break;
  110.             case WebFilter::class:
  111.                 $taskParams['filter'] = $object->getId();
  112.                 $tmpName = 'filter';
  113.                 break;
  114.             default:
  115.                 return;
  116.         }
  117.         $task->setParams(serialize($taskParams));
  118.         $task->setName("calcul_web_" . $tmpName . "_" . (new \DateTime())->format('YmdHis'));
  119.         $this->em->persist($task);
  120.         $this->em->flush();
  121. */
  122.     }
  123. }