src/Controller/ContactUsController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Api\BrcBackend\BrcClient;
  4. use App\DTO\Contact;
  5. use App\Form\ContactUsType;
  6. use App\Response\ApiResponse;
  7. use App\Util\FormErrorHelper;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route("/contact-us")
  16.  */
  17. final class ContactUsController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/", name="contact_us", methods={"GET"})
  21.      *
  22.      * @return Response
  23.      */
  24.     public function index(): Response
  25.     {
  26.         return $this->render('contact_us/index.html.twig', [
  27.             'form' => $this->createForm(ContactUsType::class)->createView(),
  28.         ]);
  29.     }
  30.     /**
  31.      * @Route("/send", name="contact_us_send", methods={"POST"})
  32.      *
  33.      * @param Request $request
  34.      * @param BrcClient $client
  35.      * @param LoggerInterface $errorsLogger
  36.      *
  37.      * @return JsonResponse
  38.      */
  39.     public function send(Request $requestBrcClient $clientLoggerInterface $errorsLogger): JsonResponse
  40.     {
  41.         try {
  42.             $contact = new Contact();
  43.             $form $this->createForm(ContactUsType::class, $contact);
  44.             $form->handleRequest($request);
  45.             if (!$form->isValid()) {
  46.                 $error FormErrorHelper::getAll($form);
  47.                 return $this->json(ApiResponse::notValidParam('KO'$error)->toArray());
  48.             }
  49.             $contact->setCountry($this->getParameter('locale'));
  50.             $client->createContact($contact->toArray());
  51.             $response ApiResponse::success('OK', [
  52.                 'content' => $this->renderView('contact_us/_success.html.twig')
  53.             ]);
  54.             return $this->json($response->toArray());
  55.         } catch (\Throwable $ex) {
  56.             $errorsLogger->error($ex->getMessage());
  57.             return $this->json(ApiResponse::internalServerError());
  58.         }
  59.     }
  60. }