src/EventSubscriber/BankSimulationSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\BankSimulation;
  4. use App\Mybase\Services\Base\SBase;
  5. use App\Repository\BankSimulationRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. class BankSimulationSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var SBase
  13.      */
  14.     private $base;
  15.     /**
  16.      * @var BankSimulationRepository
  17.      */
  18.     private $repo;
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     private $em;
  23.     public function __construct(
  24.         SBase $base,
  25.         BankSimulationRepository $repo,
  26.         EntityManagerInterface $em
  27.     ) {
  28.         $this->base $base;
  29.         $this->repo $repo;
  30.         $this->em $em;
  31.     }
  32.     /**
  33.      * @return void
  34.      */
  35.     public function onKernelRequest(RequestEvent $event)
  36.     {
  37.         $request $event->getRequest();
  38.         $object $request->get('_api_resource_class');
  39.         $datas $this->base->getPostedData($request);
  40.         if (
  41.             $object === BankSimulation::class
  42.             && $request->getMethod() === 'POST'
  43.         ) {
  44.             $banks $this->repo->findBy(['theProperty' => $datas['theProperty']]);
  45.             if ($banks !== []) {
  46.                 foreach ($banks as $bank) {
  47.                     $this->em->remove($bank);
  48.                 }
  49.                 $this->em->flush();
  50.             }
  51.             $bank $this->base->deserialize($datasBankSimulation::class, 'json');
  52.             $this->em->persist($bank);
  53.             $this->em->flush();
  54.             $event->setResponse($this->base->jsonResponseOk($datas'My Bank'));
  55.             return;
  56.         }
  57.         if (
  58.             $object === BankSimulation::class
  59.             && $request->getMethod() === 'GET'
  60.         ) {
  61.             $theProperty = (int) $request->query->get('theProperty') !== ? (int) $request->query->get('theProperty') : null;
  62.             if ($theProperty !== null) {
  63.                 $bankSimulation $this->base->getRepository(BankSimulation::class)->findBy(['theProperty' => $theProperty]);
  64.                 if ($bankSimulation !== []) {
  65.                     $data $this->base->serialize($bankSimulation[0], 'json', ['groups' => 'bank:read']);
  66.                     $event->setResponse($this->base->jsonResponseOk($data'My Bank'));
  67.                     return;
  68.                 }
  69.             }
  70.             $event->setResponse($this->base->jsonResponseBadRequest('No simulation bank for this property'));
  71.             return;
  72.         }
  73.     }
  74.     /**
  75.      * @return void
  76.      */
  77.     public static function getSubscribedEvents()
  78.     {
  79.         return [
  80.             'kernel.request' => 'onKernelRequest',
  81.         ];
  82.     }
  83. }