|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Core\Controller; |
| 4 | + |
| 5 | +use App\Core\Service\System\WebConfigurator\WebConfiguratorService; |
| 6 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 7 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 8 | +use Symfony\Component\HttpFoundation\Request; |
| 9 | +use Symfony\Component\HttpFoundation\Response; |
| 10 | +use Symfony\Component\Routing\Annotation\Route; |
| 11 | + |
| 12 | +class FirstConfigurationController extends AbstractController |
| 13 | +{ |
| 14 | + public function __construct( |
| 15 | + private readonly WebConfiguratorService $webConfiguratorService, |
| 16 | + ) {} |
| 17 | + |
| 18 | + #[Route('/first-configuration', name: 'first_configuration')] |
| 19 | + public function index( |
| 20 | + Request $request, |
| 21 | + ): Response |
| 22 | + { |
| 23 | + $this->validateConfiguratorAccess(); |
| 24 | + |
| 25 | + return $this->render( |
| 26 | + '@core_templates/configurator/configurator.html.twig', |
| 27 | + $this->webConfiguratorService->getDataForFirstConfiguration($request), |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + #[Route('/first-configuration/validate-step', name: 'first_configuration_validate_step', methods: ['POST'])] |
| 32 | + public function validateStep( |
| 33 | + Request $request, |
| 34 | + ): JsonResponse |
| 35 | + { |
| 36 | + $this->validateConfiguratorAccess(); |
| 37 | + |
| 38 | + $isStepValidated = $this->webConfiguratorService->validateStep($request->request->all()); |
| 39 | + $responseStatus = $isStepValidated->isVerificationSuccessful ? Response::HTTP_OK : Response::HTTP_BAD_REQUEST; |
| 40 | + |
| 41 | + return new JsonResponse( |
| 42 | + data: [ |
| 43 | + 'message' => $isStepValidated->message, |
| 44 | + ], |
| 45 | + status: $responseStatus, |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + #[Route('/first-configuration/finish', name: 'first_configuration_finish', methods: ['POST'])] |
| 50 | + public function finishConfiguration( |
| 51 | + Request $request, |
| 52 | + ): JsonResponse |
| 53 | + { |
| 54 | + $this->validateConfiguratorAccess(); |
| 55 | + |
| 56 | + $isSuccessfulFinished = $this->webConfiguratorService->finishConfiguration($request->request->all()); |
| 57 | + $responseStatus = $isSuccessfulFinished->isVerificationSuccessful ? Response::HTTP_OK : Response::HTTP_BAD_REQUEST; |
| 58 | + |
| 59 | + return new JsonResponse( |
| 60 | + data: $isSuccessfulFinished->message, |
| 61 | + status: $responseStatus |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + private function validateConfiguratorAccess(): void |
| 66 | + { |
| 67 | + $isConfiguratorEnabled = $this->webConfiguratorService->isConfiguratorEnabled(); |
| 68 | + |
| 69 | + if (!$isConfiguratorEnabled || $this->getUser()) { |
| 70 | + throw $this->createNotFoundException(); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments