<?php
namespace App\EventSubscriber;
use App\Entity\ProviderSolarDeclarationCadastre;
use App\Entity\TheProperty;
use App\Mybase\Services\Base\SBase;
use App\Repository\ProviderSolarDeclarationCadastreRepository;
use App\Service\DompdfService;
use App\Service\TraitementMessage;
use App\Utils\Utils;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Security;
class ProviderSolarSubscriber implements EventSubscriberInterface
{
/**
* @var SBase
*/
private $base;
/**
* @var ProviderSolarDeclarationCadastreRepository
*/
private $repo;
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var DompdfService
*/
private $dompdfService;
/**
* @var TraitementMessage
*/
private $traitementMessage;
/**
* @var Security
*/
private $security;
/**
* @var Utils
*/
private $utils;
/**
* @param Utils
*/
public function __construct(
SBase $base,
ProviderSolarDeclarationCadastreRepository $repo,
EntityManagerInterface $em,
DompdfService $dompdfService,
TraitementMessage $traitementMessage,
Security $security,
Utils $utils
) {
$this->base = $base;
$this->repo = $repo;
$this->em = $em;
$this->dompdfService = $dompdfService;
$this->traitementMessage = $traitementMessage;
$this->security = $security;
$this->utils = $utils;
}
/**
* @return void
*/
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$object = $request->get('_api_resource_class');
$datas = $this->base->getPostedData($request);
$currentUser = $this->security->getUser();
if (
$object === ProviderSolarDeclarationCadastre::class
&& $request->getMethod() === 'POST'
) {
$theproperty = $this->base->getRepository(TheProperty::class)->findOneBy(['id' => $datas['theProperty'], 'user' => $currentUser]);
if ($theproperty) {
$providerData = $this->repo->findOneByThePropertyOperationType($datas['theProperty'], $datas['operationType']);
$providerData = $providerData ? $providerData : new ProviderSolarDeclarationCadastre();
$this->utils->processQueryData($providerData, $datas, ['theProperty', 'heatEquipment', 'parkingAreaEquipment'], [null]);
$providerData->setHeatEquipment($datas['heatEquipment']);
$providerData->setParkingAreaEquipment($datas['parkingAreaEquipment']);
$providerData->setTheProperty($theproperty);
$this->em->persist($providerData);
$this->em->flush();
}
$fileRequest = $this->dompdfService->createPdfSolarCadastre($providerData);
$event->setResponse(new JsonResponse([
'data' => json_encode($fileRequest),
'message' => 'success',
'code' => 204,
]));
return;
}
}
/**
* @return void
*/
public static function getSubscribedEvents()
{
return [
'kernel.request' => 'onKernelRequest',
];
}
}