src/EventSubscriber/ProviderSolarSubscriber.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ProviderSolarDeclarationCadastre;
  4. use App\Entity\TheProperty;
  5. use App\Mybase\Services\Base\SBase;
  6. use App\Repository\ProviderSolarDeclarationCadastreRepository;
  7. use App\Service\DompdfService;
  8. use App\Service\TraitementMessage;
  9. use App\Utils\Utils;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\Security\Core\Security;
  15. class ProviderSolarSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var SBase
  19.      */
  20.     private $base;
  21.     /**
  22.      * @var ProviderSolarDeclarationCadastreRepository
  23.      */
  24.     private $repo;
  25.     /**
  26.      * @var EntityManagerInterface
  27.      */
  28.     private $em;
  29.     /**
  30.      * @var DompdfService
  31.      */
  32.     private $dompdfService;
  33.     /**
  34.      * @var TraitementMessage
  35.      */
  36.     private $traitementMessage;
  37.     /**
  38.      * @var Security
  39.      */
  40.     private $security;
  41.     /**
  42.      * @var Utils
  43.      */
  44.     private $utils;
  45.     /**
  46.      * @param Utils
  47.      */
  48.     public function __construct(
  49.         SBase $base,
  50.         ProviderSolarDeclarationCadastreRepository $repo,
  51.         EntityManagerInterface $em,
  52.         DompdfService $dompdfService,
  53.         TraitementMessage $traitementMessage,
  54.         Security $security,
  55.         Utils $utils
  56.     ) {
  57.         $this->base $base;
  58.         $this->repo $repo;
  59.         $this->em $em;
  60.         $this->dompdfService $dompdfService;
  61.         $this->traitementMessage $traitementMessage;
  62.         $this->security $security;
  63.         $this->utils $utils;
  64.     }
  65.     /**
  66.      * @return void
  67.      */
  68.     public function onKernelRequest(RequestEvent $event)
  69.     {
  70.         $request $event->getRequest();
  71.         $object $request->get('_api_resource_class');
  72.         $datas $this->base->getPostedData($request);
  73.         $currentUser $this->security->getUser();
  74.         if (
  75.             $object === ProviderSolarDeclarationCadastre::class
  76.             && $request->getMethod() === 'POST'
  77.         ) {
  78.             $theproperty $this->base->getRepository(TheProperty::class)->findOneBy(['id' => $datas['theProperty'], 'user' => $currentUser]);
  79.             if ($theproperty) {
  80.                 $providerData $this->repo->findOneByThePropertyOperationType($datas['theProperty'], $datas['operationType']);
  81.                 $providerData $providerData $providerData : new ProviderSolarDeclarationCadastre();
  82.                 $this->utils->processQueryData($providerData$datas, ['theProperty''heatEquipment''parkingAreaEquipment'], [null]);
  83.                 $providerData->setHeatEquipment($datas['heatEquipment']);
  84.                 $providerData->setParkingAreaEquipment($datas['parkingAreaEquipment']);
  85.                 $providerData->setTheProperty($theproperty);
  86.                 $this->em->persist($providerData);
  87.                 $this->em->flush();
  88.             }
  89.             $fileRequest $this->dompdfService->createPdfSolarCadastre($providerData);
  90.             $event->setResponse(new JsonResponse([
  91.                 'data' => json_encode($fileRequest),
  92.                 'message' => 'success',
  93.                 'code' => 204,
  94.             ]));
  95.             return;
  96.         }
  97.     }
  98.     /**
  99.      * @return void
  100.      */
  101.     public static function getSubscribedEvents()
  102.     {
  103.         return [
  104.             'kernel.request' => 'onKernelRequest',
  105.         ];
  106.     }
  107. }