src/Controller/HomeController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ContactType;
  4. use App\Entity\Contact;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  11. use Symfony\Component\Mailer\MailerInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Validator\Constraints\Email;
  14. use Symfony\Component\Validator\Constraints\EmailValidator;
  15. use Symfony\Component\Validator\Constraints\NotBlank;
  16. use Symfony\Component\Validator\Validation;
  17. #[Route('/')]
  18. class HomeController extends AbstractController
  19. {
  20.     #[Route('/'name'app_home'methods: ['GET'])]
  21.     public function index(EntityManagerInterface $em): Response
  22.     {
  23.     return $this->redirectToRoute("app_dashboard");
  24.     }
  25.     #[Route('/contact'name'app_home_contact'methods: ['POST'])]
  26.     public function contact(EntityManagerInterface $emRequest $requestMailerInterface $mailer): Response
  27.     {
  28.     return $this->json(false);
  29.         $payload json_decode($request->getContent(), true);
  30.         
  31.         // check payload data
  32.         $valuesSet = isset($payload['name']) && 
  33.             isset($payload['email']) &&
  34.             isset($payload['message']);
  35.         if (!$valuesSet) {
  36.             return $this->json([
  37.                 'error' => 'Invalid payload.'
  38.             ]);
  39.         }
  40.         // check if values are valid
  41.         $validator Validation::createValidator();
  42.         $nameValidator $validator->validate($payload['name'], [
  43.             new NotBlank(),
  44.         ]);
  45.         $emailValidator $validator->validate($payload['email'], [
  46.             new Email(),
  47.         ]);
  48.         $messageValidator $validator->validate($payload['message'], [
  49.             new NotBlank(),
  50.         ]);
  51.     
  52.         if ($nameValidator->count() > || 
  53.             $emailValidator->count() > ||
  54.             $messageValidator->count() > 0
  55.             ) {
  56.             return $this->json([
  57.                 'error' => 'Invalid payload'
  58.                 ]);
  59.         }
  60.             
  61.         $email = (new TemplatedEmail())
  62.         ->from('btwebadmin@smart-device.ch')
  63.         ->to('aris.konstantinidis@smart-advice.ch')
  64.         ->subject('New bodytrip.app message')
  65.         ->htmlTemplate('contact/contact.html.twig')
  66.         ->context([
  67.             'name' => $payload['name'],
  68.             'message' => $payload['message'],
  69.             'emailAddress' => $payload['email'],
  70.         ]);
  71.         try {
  72.             $mailer->send($email);
  73.         } catch (TransportExceptionInterface $e) {
  74.             return $this->json([
  75.                 'error' => 'Something went wrong. Please contact us directly at aris.konstantinidis@smart-advice.ch.'
  76.             ]);
  77.         }
  78.         return $this->json([
  79.             'success' => 'Thank you for your message. We will contact you asap'
  80.         ]);
  81.     }
  82. }