src/EventListener/LocalOperationSubscriber.php line 118

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\AbstractOperation;
  4. use App\Entity\CoopOperation;
  5. use App\Entity\LocalOperation;
  6. use App\Entity\Operation;
  7. use App\Entity\OperationDate;
  8. use App\Manager\CustomerManager;
  9. use App\Manager\OperationDateManager;
  10. use App\Service\ConvertXlsToArray;
  11. use DateTime;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Psr\Container\ContainerInterface;
  14. use Sogec\BOBundle\Event\CRUDEvents;
  15. use Sogec\BOBundle\Event\GetCRUDPersistResponseEvent;
  16. use Sogec\BOBundle\Event\GetCRUDSubmittedFormEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\File\UploadedFile;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. /**
  23.  * Class LocalOperationSubscriber.
  24.  */
  25. final class LocalOperationSubscriber implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var CustomerManager
  29.      */
  30.     protected $manager;
  31.     /**
  32.      * @var UrlGeneratorInterface
  33.      */
  34.     protected $urlGenerator;
  35.     /**
  36.      * @var ConvertXlsToArray
  37.      */
  38.     private $convertXlsToArray;
  39.     /**
  40.      * @var ContainerInterface
  41.      */
  42.     private $container;
  43.     /**
  44.      * @var EntityManagerInterface
  45.      */
  46.     private $entityManager;
  47.     /**
  48.      * @var OperationDateManager
  49.      */
  50.     private $operationDateManager;
  51.     /**
  52.      * LocalOperationSubscriber constructor.
  53.      *
  54.      * @param CustomerManager $manager
  55.      * @param UrlGeneratorInterface $urlGenerator
  56.      * @param ConvertXlsToArray $convertXlsToArray
  57.      * @param ContainerInterface $container
  58.      * @param EntityManagerInterface $entityManager
  59.      * @param OperationDateManager $operationDateManager
  60.      */
  61.     public function __construct(CustomerManager $managerUrlGeneratorInterface $urlGeneratorConvertXlsToArray $convertXlsToArrayContainerInterface $containerEntityManagerInterface $entityManagerOperationDateManager $operationDateManager)
  62.     {
  63.         $this->manager $manager;
  64.         $this->urlGenerator $urlGenerator;
  65.         $this->convertXlsToArray $convertXlsToArray;
  66.         $this->container $container;
  67.         $this->entityManager $entityManager;
  68.         $this->operationDateManager $operationDateManager;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public static function getSubscribedEvents()
  74.     {
  75.         return [
  76.             CRUDEvents::NEW_COMPLETED => ['subscribeOperation'10],
  77.             CRUDEvents::NEW_SUBMITTED => ['checkCab''changeOperationDate'],
  78.             CRUDEvents::EDIT_SUBMITTED => ['checkCab''changeOperationDate'],
  79.         ];
  80.     }
  81.     /**
  82.      * @param GetResponseForControllerResultEvent $event
  83.      */
  84.     public function subscribeOperation(GetCRUDPersistResponseEvent $event)
  85.     {
  86.         $entity $event->getEntity();
  87.         if (!$entity instanceof LocalOperation) {
  88.             return;
  89.         }
  90.         $campaign $entity->getCampaign();
  91.         $entity->setImage($campaign->getImage());
  92.         $this->manager->flush();
  93.         $event->setResponse(new RedirectResponse($this->urlGenerator->generate('admin_local_operation_partners_edit', [
  94.             'operation_id' => $entity->getId(),
  95.         ])));
  96.     }
  97.     /**
  98.      * @param GetCRUDSubmittedFormEvent $event
  99.      *
  100.      * @return void
  101.      */
  102.     public function checkCab(GetCRUDSubmittedFormEvent $event)
  103.     {
  104.         if (!$event->getEntity() instanceof LocalOperation) {
  105.             return;
  106.         }
  107.         $request $event->getRequest();
  108.         /** @var UploadedFile $file */
  109.         $file $request->files->get('local_operation')['cab']['file'];
  110.         if (null === $file) {
  111.             return;
  112.         }
  113.         $filePath $file->getPathname();
  114.         $datas $this->convertXlsToArray->transformAndCheckFileToData($filePath18);
  115.         $wrongStart $datas['wrong_start'];
  116.         $errorMsg sprintf('<p>Une erreur est survenue lors de la crĂ©ation de votre opĂ©ration :</p></p><p>Information incorrecte dans le fichier de Simulateur promo (CAB) <strong>"%s"</strong></p>'$file->getClientOriginalName());
  117.         if (!$datas['valid']) {
  118.             $errorMsg .= '<ul>';
  119.             foreach ($datas['errors'] as $errorKey => $error) {
  120.                 if (!$wrongStart) {
  121.                     $errorMsg .= sprintf('<li>Colonne <strong>%s</strong><ul>'strtoupper($errorKey));
  122.                 }
  123.                 foreach ($error as $value) {
  124.                     $errorMsg .= sprintf('<li>%s</li>'$value);
  125.                 }
  126.                 $errorMsg .= '</ul>';
  127.             }
  128.             $errorMsg .= '</li></ul>';
  129.             $this->container->get('session')->getFlashBag()->add('warning'$errorMsg);
  130.             if ($event->getEntity()->getId() !== null) {
  131.                 $event->setResponse(new RedirectResponse($this->urlGenerator->generate('admin_local_operations_edit', ['id' => $event->getEntity()->getId()])));
  132.             } else {
  133.                 $event->setResponse(new RedirectResponse($this->urlGenerator->generate('admin_local_operations_new')));
  134.             }
  135.         }
  136.         if (!$wrongStart) {
  137.             foreach ($datas['participations'] as $participation) {
  138.                 $remise = isset($participation[LocalOperation::COL_PERCENT_DISCOUNT]) ?: $participation[strtoupper(LocalOperation::COL_PERCENT_DISCOUNT)];
  139.                 if ($remise || $remise 1) {
  140.                     $this->container->get('session')->getFlashBag()->add('warning'sprintf('Information incorrecte dans le fichier de Simulateur promo (CAB) "%s"'$file->getClientOriginalName()));
  141.                     if ($event->getEntity()->getId() !== null) {
  142.                         $event->setResponse(new RedirectResponse($this->urlGenerator->generate('admin_local_operations_edit', ['id' => $event->getEntity()->getId()])));
  143.                     } else {
  144.                         $event->setResponse(new RedirectResponse($this->urlGenerator->generate('admin_local_operations_new')));
  145.                     }
  146.                 }
  147.             }
  148.         }
  149.     }
  150.     public function changeOperationDate(GetCRUDSubmittedFormEvent $event)
  151.     {
  152.         $entity $event->getEntity();
  153.         if (!$entity instanceof LocalOperation) {
  154.             return;
  155.         }
  156.         $request $event->getRequest();
  157.         $attribute 'operation';
  158.         if ($entity instanceof CoopOperation) {
  159.             $attribute 'coop_operation';
  160.         }
  161.         if ($entity instanceof LocalOperation) {
  162.             $attribute 'local_operation';
  163.         }
  164.         if ($operationDate $this->entityManager->getRepository(OperationDate::class)->findOneBy([], ['id' => 'DESC'])) {
  165.             try {
  166.                 $date DateTime::createFromFormat(AbstractOperation::DATE_FORMAT$request->request->get($attribute)['endDate']);
  167.             } catch (\Exception $e) {
  168.                 return;
  169.             }
  170.             $newDate = new DateTime(date(AbstractOperation::DATETIME_FORMATstrtotime($this->operationDateManager->settingFormattedDate($operationDate), strtotime($date->format(AbstractOperation::DATETIME_FORMAT)))));
  171.             $entity->setEventClosingDate(date_time_set($newDate235959));
  172.         }
  173.     }
  174. }