vendor/fri0z/mldev-form-bundle/src/EventSubscriber/FormConfirmSuccessSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. namespace MLDev\FormBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use MLDev\FormBundle\Entity\MailMessage;
  5. use MLDev\FormBundle\Event\FormConfirmSuccessEvent;
  6. use MLDev\FormBundle\Service\FormProvider;
  7. use MLDev\FormBundle\Service\MailerProvider;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\File\UploadedFile;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Twig\Environment;
  12. class FormConfirmSuccessSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var \Symfony\Component\HttpFoundation\Request|null
  16.      */
  17.     private $request;
  18.     /**
  19.      * @var Environment
  20.      */
  21.     private $templating;
  22.     /**
  23.      * @var EntityManagerInterface
  24.      */
  25.     private $entityManager;
  26.     /**
  27.      * @var FormProvider
  28.      */
  29.     private $formProvider;
  30.     /**
  31.      * @var MailerProvider
  32.      */
  33.     private $mailerProvider;
  34.     /**
  35.      * FormConfirmSuccessSubscriber constructor.
  36.      */
  37.     public function __construct(
  38.         EntityManagerInterface $entityManager,
  39.         RequestStack $requestStack,
  40.         Environment $twig,
  41.         FormProvider $formProvider,
  42.         MailerProvider $mailerProvider
  43.     ) {
  44.         $this->request $requestStack->getCurrentRequest();
  45.         $this->templating $twig;
  46.         $this->entityManager $entityManager;
  47.         $this->formProvider $formProvider;
  48.         $this->mailerProvider $mailerProvider;
  49.     }
  50.     public function doSaveMessageInDatabase(FormConfirmSuccessEvent $event)
  51.     {
  52.         $name $event->getName();
  53.         $data $event->getData();
  54.         $config $this->formProvider->getFormConfigByName($name);
  55.         $subject $config['settings']['subject'] ?? 'unknown';
  56.         $data array_filter($data, function ($value) {
  57.             return !$value instanceof UploadedFile;
  58.         });
  59.         $entity = new MailMessage($subject$data$this->request->cookies->all());
  60.         $this->entityManager->persist($entity);
  61.         $this->entityManager->flush();
  62.     }
  63.     public function doNotificationToEmail(FormConfirmSuccessEvent $event)
  64.     {
  65.         $name $event->getName();
  66.         $data $event->getData();
  67.         list('settings' => $settings) = $this->formProvider->getFormConfigByName($name);
  68.         list('subject' => $subject'emails' => $recipients'templates' => $templates) = $settings;
  69.         /** @var UploadedFile[] $files */
  70.         $files array_filter($data, function ($value) {
  71.             return $value instanceof UploadedFile;
  72.         });
  73.         $body $this->templating->render(
  74.             $templates['letter'], [
  75.                 'config' => $settings,
  76.                 'formData' => $data
  77.             ]
  78.         );
  79.         $message $this->mailerProvider->createMessage($subject$recipients);
  80.         $message->addPart(
  81.             $body,
  82.             'text/plain'
  83.         );
  84.         if (count($files) > 0) {
  85.             foreach ($files as $uploadedFile) {
  86.                 $message->attach(
  87.                     \Swift_Attachment::fromPath($uploadedFile->getRealPath())->setFilename(
  88.                         $uploadedFile->getClientOriginalName()
  89.                     )
  90.                 );
  91.             }
  92.         }
  93.         $this->mailerProvider->send($message);
  94.     }
  95.     public static function getSubscribedEvents()
  96.     {
  97.         return [
  98.             FormConfirmSuccessEvent::EVENT_NAME => [
  99.                 ['doSaveMessageInDatabase'20],
  100.                 ['doNotificationToEmail'10],
  101.             ]
  102.         ];
  103.     }
  104. }