src/Controller/ContactUsController.php line 25

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"}, options={"sitemap" = true})
  21.      */
  22.     public function index(): Response
  23.     {
  24.         return $this->render('contact_us/index.html.twig', [
  25.             'form' => $this->createForm(ContactUsType::class)->createView(),
  26.         ]);
  27.     }
  28.     /**
  29.      * @Route("/send", name="contact_us_send", methods={"POST"})
  30.      */
  31.     public function send(Request $requestBrcClient $clientLoggerInterface $errorsLogger): JsonResponse
  32.     {
  33.         try {
  34.             $contact = new Contact();
  35.             $form $this->createForm(ContactUsType::class, $contact);
  36.             $form->handleRequest($request);
  37.             if (!$form->isValid()) {
  38.                 $error FormErrorHelper::getAll($form);
  39.                 return $this->json(ApiResponse::notValidParam('KO'$error)->toArray());
  40.             }
  41.             $contact->setCountry($this->getParameter('locale'));
  42.             $client->createContact($contact->toArray());
  43.             $response ApiResponse::success('OK', [
  44.                 'content' => $this->renderView('contact_us/_success.html.twig')
  45.             ]);
  46.             return $this->json($response->toArray());
  47.         } catch (\Throwable $ex) {
  48.             $errorsLogger->error($ex->getMessage());
  49.             return $this->json(ApiResponse::internalServerError());
  50.         }
  51.     }
  52. }