custom/plugins/SwagCmsExtensions/src/Form/Action/FormMailSubscriber.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\CmsExtensions\Form\Action;
  8. use Shopware\Core\Content\Mail\Service\AbstractMailService;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldCollection;
  11. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldEntity;
  12. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\Type\Email;
  13. use Swag\CmsExtensions\Form\Event\CustomFormEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class FormMailSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var SystemConfigService
  19.      */
  20.     private $systemConfigService;
  21.     /**
  22.      * @var AbstractMailService
  23.      */
  24.     private $mailService;
  25.     public function __construct(SystemConfigService $systemConfigServiceAbstractMailService $mailService)
  26.     {
  27.         $this->systemConfigService $systemConfigService;
  28.         $this->mailService $mailService;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             CustomFormEvent::EVENT_NAME => 'sendMail',
  34.         ];
  35.     }
  36.     public function sendMail(CustomFormEvent $event): void
  37.     {
  38.         $receivers $event->getForm()->getReceivers();
  39.         if (empty($receivers)) {
  40.             $receivers[] = $this->systemConfigService->get('core.basicInformation.email'$event->getSalesChannelId());
  41.         }
  42.         $groups $event->getForm()->getGroups();
  43.         $fields $groups === null ? new FormGroupFieldCollection() : $groups->getFields();
  44.         $mailTemplate $event->getForm()->getMailTemplate();
  45.         $data $mailTemplate !== null $mailTemplate->jsonSerialize() : [];
  46.         $data['salesChannelId'] = $event->getSalesChannelId();
  47.         if ($sender $this->getSenderMail($fields$event->getFormData())) {
  48.             $data['replyTo'] = $sender;
  49.         }
  50.         $templateData = [
  51.             'form' => $event->getForm(),
  52.             'fields' => $fields,
  53.             'formData' => $event->getFormData(),
  54.             'salesChannel' => $event->getSalesChannelContext()->getSalesChannel(),
  55.         ];
  56.         foreach ($receivers as $mail) {
  57.             if (!\is_string($mail)) {
  58.                 continue;
  59.             }
  60.             $data['recipients'] = [$mail => $mail];
  61.             $this->mailService->send($data$event->getContext(), $templateData);
  62.         }
  63.     }
  64.     private function getSenderMail(FormGroupFieldCollection $fieldCollection, array $formData): ?string
  65.     {
  66.         $mailField $fieldCollection->filter(static function (FormGroupFieldEntity $field) {
  67.             return $field->getType() === Email::NAME;
  68.         })->first();
  69.         if ($mailField === null) {
  70.             return null;
  71.         }
  72.         return $formData[$mailField->getTechnicalName()] ?? null;
  73.     }
  74. }