src/Controller/Front/TruckController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Controller\Core\BaseFrontController;
  4. use App\Entity\FormQuotationTruck;
  5. use App\Form\Front\FormQuotationTruckType;
  6. use App\Manager\FormQuotationTruckManager;
  7. use App\Manager\ModelManager;
  8. use App\Manager\ModelsManager;
  9. use App\Service\CRM\ApiCRM;
  10. use App\Service\Mail\QuotationTruckMail;
  11. use App\Service\Recaptcha\Validator as RecaptchaValidator;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. class TruckController extends BaseFrontController
  18. {
  19.     const FORM_QUOTATION_TRUCK 'sd_form_quotation_truck_entity';
  20.     /**
  21.      * @Route("/camiones", name="trucks")
  22.      * @Template("front/trucks/trucks.html.twig")
  23.      */
  24.     public function trucks(ModelsManager $modelsManagerModelManager $modelManager): array
  25.     {
  26.         $this->locals['models'] = $modelManager->valid();
  27.         $this->locals['sd'] = $modelsManager->find(1);
  28.         return $this->locals;
  29.     }
  30.     /**
  31.      * @Route("/camiones/{slug}", name="truck", methods={"GET"})
  32.      * @Template("front/trucks/truck.html.twig")
  33.      */
  34.     public function truck(string $slugModelManager $modelManager): array
  35.     {
  36.         $model $modelManager->bySlug($slug);
  37.         if (is_null($model)) {
  38.             throw new NotFoundHttpException('Modelo no encontrado');
  39.         }
  40.         $entity $this->getFormSession(self::FORM_QUOTATION_TRUCK, new FormQuotationTruck());
  41.         $form $this->getFormView(FormQuotationTruckType::class, $entity'truck_action', ['slug' => $slug]);
  42.         $this->locals['form'] = $form;
  43.         $this->locals['model'] = $model;
  44.         return $this->locals;
  45.     }
  46.     /**
  47.      * @Route("/camiones/{slug}", name="truck_action", methods={"POST"})
  48.      */
  49.     public function truckAction(Request $requeststring $slugFormQuotationTruckManager $managerQuotationTruckMail $mailApiCRM $apiCRMRecaptchaValidator $recaptchaValidator): RedirectResponse
  50.     {
  51.         $entity = new FormQuotationTruck();
  52.         $form $this->getForm(FormQuotationTruckType::class, $entity'truck_action', ['slug' => $slug]);
  53.         $form->handleRequest($request);
  54.         if ($form->isValid() and $recaptchaValidator->validate($this->getRecaptchaToken(), $this->getClientIp())) {
  55.             $manager->save($entity);
  56.             $mail->load($entity)->send();
  57.             $crmLead $entity->leadToCRM();
  58.             $result $apiCRM->send($crmLead);
  59.             $entity->setCrmBody($crmLead->toCRM());
  60.             $entity->setCrmResult($result);
  61.             $manager->save($entity);
  62.             return $this->redirectToRoute('thanks_slug_parameter', ['slug' => 'cotizar''parameter' => $slug]);
  63.         }
  64.         $this->saveFormSession(self::FORM_QUOTATION_TRUCK$entity);
  65.         return $this->redirectToRoute('truck', ['slug' => $slug]);
  66.     }
  67. }