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.         ];
  55.         foreach ($receivers as $mail) {
  56.             if (!\is_string($mail)) {
  57.                 continue;
  58.             }
  59.             $data['recipients'] = [$mail => $mail];
  60.             $this->mailService->send($data$event->getContext(), $templateData);
  61.         }
  62.     }
  63.     /**
  64.      * @param array<string, string|null> $formData
  65.      */
  66.     private function getSenderMail(FormGroupFieldCollection $fieldCollection, array $formData): ?string
  67.     {
  68.         $mailField $fieldCollection->filter(static function (FormGroupFieldEntity $field) {
  69.             return $field->getType() === Email::NAME;
  70.         })->first();
  71.         if ($mailField === null) {
  72.             return null;
  73.         }
  74.         return $formData[$mailField->getTechnicalName()] ?? null;
  75.     }
  76. }