src/EventListener/FormAnswerCreatedListener.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Service\MailerService;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Entity\FormAnswer;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\Validator\Validator\ValidatorInterface;
  12. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  13. class FormAnswerCreatedListener implements EventSubscriberInterface
  14. {
  15.     private $mailerService;
  16.     private EntityManagerInterface $entityManager;
  17.     private $validator;
  18.     public function __construct(MailerService $mailerServiceEntityManagerInterface $entityManagerValidatorInterface $validator)
  19.     {
  20.         $this->mailerService $mailerService;
  21.         $this->entityManager $entityManager;
  22.         $this->validator $validator;
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             KernelEvents::VIEW => ['saveFormAnswer'EventPriorities::POST_WRITE],
  28.         ];
  29.     }
  30.     public function saveFormAnswer(ViewEvent $event)
  31.     {
  32.         $mainFormAnswer $event->getControllerResult();
  33.         $method $event->getRequest()->getMethod();
  34.         if (!$mainFormAnswer instanceof FormAnswer) {
  35.             return;
  36.         }
  37.         $answerFields $mainFormAnswer->getFormAnswerFields();
  38.         $content "";
  39.         foreach($answerFields as $answerField){
  40.             $content .= $answerField->getFormField()->getLabel() . " - " $answerField->getValue() . "\n";
  41.         }
  42.         $recipientEmail $mainFormAnswer->getForm()->getSendMailAnswet();
  43.         $meiles explode(',',$recipientEmail);
  44.         $subject "Нова відповідь на форму - " $mainFormAnswer->getForm()->getName();
  45.         
  46.         foreach($meiles as $meil){
  47.             $this->mailerService->sendRegistrationEmail(trim($meil), $subject$content);
  48.         }
  49.        
  50.     }
  51. }