<?php
namespace DcSiteBundle\Controller\Subaru;
use CoreBundle\Component\CoreFormFactory;
use CoreBundle\Component\FormManager;
use CoreBundle\Component\Request\Curl;
use CoreBundle\Entity\Forms;
use CoreBundle\Entity\Model;
use CoreBundle\Entity\ViDiDepartment;
use CoreBundle\Factory\Vehicle as VehicleFactory;
use CoreBundle\Model\Api\OnlineService\ApiServer1C;
use CoreBundle\Model\Vehicles\Repository;
use CoreBundle\Model\ViDiDepartmentModel;
use CoreBundle\Services\MediaExtensionVidi;
use DcSiteBundle\Model\Form\ServicesOrderForm;
use DcSiteBundle\Services\VehicleService;
use Doctrine\ORM\EntityManagerInterface;
use PortalBundle\Model\SeoMetaTag;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
class ServiceController extends MainController
{
const SUBARU_URL = 'https://vin.subaru.ua/';
public function __construct(CoreFormFactory $coreFormFactory, SeoMetaTag $seoMetaTag, RequestStack $requestStack, RouterInterface $router, FormManager $formManager, EntityManagerInterface $em, ApiServer1C $apiServer1C, SessionInterface $session, Filesystem $filesystem, MediaExtensionVidi $mediaExtensionVidi, Repository $vehicleRepository, VehicleFactory $vehicleFactory, Environment $twig)
{
parent::__construct($coreFormFactory, $seoMetaTag, $requestStack, $router, $formManager, $em, $apiServer1C, $session, $filesystem, $mediaExtensionVidi, $vehicleRepository, $vehicleFactory, $twig);
}
public function accessories(): ?Response
{
return $this->baseSubaruRender('@DcSite/Subaru/Service/accessories.html.twig', ['buyPartsForm' => $this->CoreFormFactory()->buyPartsForm($this->getDealer())->createView()]);
}
public function assistance(): ?Response
{
return $this->baseSubaruRender('@DcSite/Subaru/Service/assistance.html.twig');
}
public function bodyRepair(): ?Response
{
$repairPhotoForm = $this->CoreFormFactory()->repairPhotoForm();
return $this->baseSubaruRender('@DcSite/Subaru/Service/body-repair.html.twig', [
'repairPhotoForm' => $repairPhotoForm->createView(),
]);
}
public function orderTo(): ?Response
{
$serviceForm = $this->CoreFormFactory()->serviceForm();
return $this->baseSubaruRender('@DcSite/Subaru/Service/order-to.html.twig', [
'serviceForm' => $serviceForm->createView(),
'dealerName' => $this->getDealer()->getBrand()->getName()
]);
}
public function regulationsTo(VehicleService $vehicleService): ?Response
{
$models = $vehicleService->getModelsForRegulationsTo($this->getDealer());
return $this->baseSubaruRender('@DcSite/Subaru/Service/regulations-to.html.twig', [
'models' => $models,
]);
}
public function regulationsToModel($model): ?Response
{
$model = $this->em->getRepository(Model::class)->findOneBy(['url' => $model]);
if (!$model) {
throw new NotFoundHttpException();
}
return $this->baseSubaruRender('@DcSite/Subaru/Service/regulations-to-model.html.twig', [
'model' => $model->getId(),
'modelTitle' => $model->getTitle(),
]);
}
public function spareParts(): ?Response
{
$quest = new Forms();
$department = $this->em->getRepository(ViDiDepartment::class)->findOneBy(['dealer' => $this->getDealer(), 'type' => ViDiDepartmentModel::DEPARTMENT_TYPE_PARTS]);
$quest->setDepartment($department);
return $this->baseSubaruRender('@DcSite/Subaru/Service/spare-parts.html.twig', [
'questionsp' => $this->CoreFormFactory()->feedbackQuestionForm($quest, $this->getDealer())->createView(),
]);
}
public function tireHotel(Request $request): ?Response
{
return $this->baseSubaruRender('@DcSite/Subaru/Service/tires.html.twig', [
'serviceOrderForm' => $this->CoreFormFactory()->servicesOrderForm(ServicesOrderForm::TIRE_HOTEL, $request->getLocale())->createView(),
]);
}
public function serviceCompany(): ?Response
{
return $this->baseSubaruRender('@DcSite/Subaru/Service/service-company.html.twig');
}
public function checkVin(Request $request, Curl $curl): JsonResponse
{
$vin = $request->request->get('vin');
$headers = [
'Content-Type: application/json; charset=UTF-8',
];
$curl->init(self::SUBARU_URL . '?vin=' . $vin)
->setOpt(CURLOPT_CUSTOMREQUEST, "GET")
->setOpt(CURLOPT_HTTPHEADER, $headers)
->setOpt(CURLOPT_RETURNTRANSFER, true);
$result = $curl->send();
$result = json_decode(trim($this->removeBOM($result)), true);
if ($curl->getResultCode() === 400) {
return new JsonResponse(['status' => false, 'error_message' => $result['error_message']]);
}
$firstElement = current($result);
$model = $firstElement['model_name'] . ' ' . $firstElement['model_year'];
return new JsonResponse(['success' => true, 'data' => $result, 'model' => $model]);
}
public function multiConsultationEnter(): ?Response
{
return $this->baseSubaruRender('@DcSite/Subaru/Service/consultation.html.twig');
}
public function multiConsultationForm(): ?Response
{
return $this->baseSubaruRender('@DcSite/Subaru/Service/consultation-form.html.twig');
}
public function multiConsultationFormOnline(): ?Response
{
return $this->baseSubaruRender('@DcSite/Subaru/Service/consultation-form-online.html.twig');
}
public function multiConsultationTestdriveForm(): ?Response
{
return $this->baseSubaruRender('@DcSite/Subaru/Service/consultation-testdrive-form.html.twig');
}
public function nightServiceAgreement(): ?Response
{
return $this->baseSubaruRender('@DcSite/Subaru/Service/night-service-agreement.html.twig', [
'dealer' => $this->getDealer()
]);
}
}