<?php
namespace App\Controller\Front;
use App\Controller\Core\BaseFrontController;
use App\Entity\FormQuotationTruck;
use App\Form\Front\FormQuotationTruckType;
use App\Manager\FormQuotationTruckManager;
use App\Manager\ModelManager;
use App\Manager\ModelsManager;
use App\Service\CRM\ApiCRM;
use App\Service\Mail\QuotationTruckMail;
use App\Service\Recaptcha\Validator as RecaptchaValidator;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class TruckController extends BaseFrontController
{
const FORM_QUOTATION_TRUCK = 'sd_form_quotation_truck_entity';
/**
* @Route("/camiones", name="trucks")
* @Template("front/trucks/trucks.html.twig")
*/
public function trucks(ModelsManager $modelsManager, ModelManager $modelManager): array
{
$this->locals['models'] = $modelManager->valid();
$this->locals['sd'] = $modelsManager->find(1);
return $this->locals;
}
/**
* @Route("/camiones/{slug}", name="truck", methods={"GET"})
* @Template("front/trucks/truck.html.twig")
*/
public function truck(string $slug, ModelManager $modelManager): array
{
$model = $modelManager->bySlug($slug);
if (is_null($model)) {
throw new NotFoundHttpException('Modelo no encontrado');
}
$entity = $this->getFormSession(self::FORM_QUOTATION_TRUCK, new FormQuotationTruck());
$form = $this->getFormView(FormQuotationTruckType::class, $entity, 'truck_action', ['slug' => $slug]);
$this->locals['form'] = $form;
$this->locals['model'] = $model;
return $this->locals;
}
/**
* @Route("/camiones/{slug}", name="truck_action", methods={"POST"})
*/
public function truckAction(Request $request, string $slug, FormQuotationTruckManager $manager, QuotationTruckMail $mail, ApiCRM $apiCRM, RecaptchaValidator $recaptchaValidator): RedirectResponse
{
$entity = new FormQuotationTruck();
$form = $this->getForm(FormQuotationTruckType::class, $entity, 'truck_action', ['slug' => $slug]);
$form->handleRequest($request);
if ($form->isValid() and $recaptchaValidator->validate($this->getRecaptchaToken(), $this->getClientIp())) {
$manager->save($entity);
$mail->load($entity)->send();
$crmLead = $entity->leadToCRM();
$result = $apiCRM->send($crmLead);
$entity->setCrmBody($crmLead->toCRM());
$entity->setCrmResult($result);
$manager->save($entity);
return $this->redirectToRoute('thanks_slug_parameter', ['slug' => 'cotizar', 'parameter' => $slug]);
}
$this->saveFormSession(self::FORM_QUOTATION_TRUCK, $entity);
return $this->redirectToRoute('truck', ['slug' => $slug]);
}
}