<?php
namespace App\Controller\Front;
use App\Controller\Core\BaseFrontController;
use App\Entity\FormAfterSales;
use App\Form\Front\FormAfterSalesType;
use App\Manager\AfterSalesManager;
use App\Manager\AfterSalesServicesManager;
use App\Manager\AfterSalesSparePartsManager;
use App\Manager\FormAfterSalesManager;
use App\Service\CRM\ApiCRM;
use App\Service\Mail\AfterSalesMail;
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\Routing\Annotation\Route;
class AfterSalesController extends BaseFrontController
{
const FORM_AFTER_SALES = 'sd_form_after_sales_entity';
/**
* @Route("/postventa", name="after_sales")
* @Template("front/after_sales/after_sales.html.twig")
*/
public function afterSales(
AfterSalesManager $afterSalesManager,
AfterSalesSparePartsManager $afterSalesSparePartsManager,
AfterSalesServicesManager $afterSalesServicesManager
): array
{
$this->locals['spareParts'] = $afterSalesSparePartsManager->find(1);
$this->locals['services'] = $afterSalesServicesManager->find(1);
$this->locals['sd'] = $afterSalesManager->find(1);
return $this->locals;
}
/**
* @Route("/repuestos", name="spare_parts", methods={"GET"})
* @Template("front/after_sales/spare_parts.html.twig")
*/
public function spareParts(AfterSalesSparePartsManager $afterSalesSparePartsManager): array
{
$entity = $this->getFormSession(self::FORM_AFTER_SALES, new FormAfterSales());
$form = $this->getFormView(FormAfterSalesType::class, $entity, 'spare_parts_action');
$this->locals['sd'] = $afterSalesSparePartsManager->find(1);
$this->locals['form'] = $form;
return $this->locals;
}
/**
* @Route("/repuestos", name="spare_parts_action", methods={"POST"})
*/
public function sparePartsAction(Request $request, FormAfterSalesManager $manager, AfterSalesMail $mail, ApiCRM $apiCRM, RecaptchaValidator $recaptchaValidator): RedirectResponse
{
$entity = new FormAfterSales();
$form = $this->getForm(FormAfterSalesType::class, $entity, 'spare_parts_action');
$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', ['slug' => 'post-venta']);
}
$this->saveFormSession(self::FORM_AFTER_SALES, $entity);
return $this->redirectToRoute('spare_parts_action');
}
}