<?php
namespace App\Controller\Front;
use App\Controller\Core\BaseFrontController;
use App\Entity\FormService;
use App\Entity\Service;
use App\Form\Front\FormServiceType;
use App\Manager\AfterSalesServicesManager;
use App\Manager\FormServiceManager;
use App\Manager\ServiceManager;
use App\Service\CRM\ApiCRM;
use App\Service\Mail\ServiceMail;
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 ServiceController extends BaseFrontController
{
const FORM_SERVICE = 'sd_form_service_entity';
/**
* @Route("/servicios", name="services")
* @Template("front/services/services.html.twig")
*/
public function services(
AfterSalesServicesManager $afterSalesServicesManager,
ServiceManager $serviceManager): array
{
$this->locals['sd'] = $afterSalesServicesManager->find(1);
$this->locals['services'] = $serviceManager->valid();
return $this->locals;
}
/**
* @Route("/servicio/{slug}", name="service", methods={"GET"})
* @Template("front/services/service.html.twig")
*/
public function service(string $slug, ServiceManager $serviceManager): array
{
/** @var Service|null $service */
$service = $serviceManager->bySlug($slug);
if (is_null($service)) {
throw new NotFoundHttpException('Service not found');
}
$entity = $this->getFormSession(self::FORM_SERVICE, new FormService());
$form = $this->getFormView(FormServiceType::class, $entity, 'service_action', ['slug' => $slug]);
$this->locals['service'] = $service;
$this->locals['form'] = $form;
return $this->locals;
}
/**
* @Route("/servicio/{slug}", name="service_action", methods={"POST"})
*/
public function serviceAction(Request $request, string $slug, FormServiceManager $manager, ServiceMail $mail, ApiCRM $apiCRM): RedirectResponse
{
$entity = new FormService();
$form = $this->getForm(FormServiceType::class, $entity, 'service_action', ['slug' => $slug]);
$form->handleRequest($request);
if ($form->isValid()) {
$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' => 'servicio', 'parameter' => $slug]);
}
$this->saveFormSession(self::FORM_SERVICE, $entity);
return $this->redirectToRoute('service', ['slug' => $slug]);
}
}