custom/plugins/UandiEfbLoginById/src/Service/SendMailActionDecorator.php line 66

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uandi\UandiEfbLoginById\Service;
  3. use Exception;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
  7. use Shopware\Core\Content\MailTemplate\Exception\MailEventConfigurationException;
  8. use Shopware\Core\Content\MailTemplate\Exception\SalesChannelNotFoundException;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\Event\FlowEvent;
  15. use Shopware\Core\Framework\Event\MailAware;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Contracts\EventDispatcher\Event;
  18. class SendMailActionDecorator extends FlowAction implements EventSubscriberInterface
  19. {
  20.     private LoggerInterface $logger;
  21.     private EntityRepositoryInterface $customerRepository;
  22.     private FlowAction $decoratedService;
  23.     /**
  24.      * @param LoggerInterface $logger
  25.      * @param EntityRepositoryInterface $customerRepository
  26.      * @param EventSubscriberInterface $decoratedService
  27.      */
  28.     public function __construct(
  29.         LoggerInterface $logger,
  30.         EntityRepositoryInterface $customerRepository,
  31.         FlowAction $decoratedService
  32.     ) {
  33.         $this->logger $logger;
  34.         $this->customerRepository $customerRepository;
  35.         $this->decoratedService $decoratedService;
  36.     }
  37.     public function getDecorated(): FlowAction
  38.     {
  39.         return $this->decoratedService;
  40.     }
  41.     public static function getName(): string
  42.     {
  43.         return 'action.mail.send';
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             self::getName() => 'handle',
  49.         ];
  50.     }
  51.     /**
  52.      * Intercept send mail action to replace recipient if needed
  53.      *
  54.      * @throws MailEventConfigurationException
  55.      * @throws SalesChannelNotFoundException
  56.      * @throws InconsistentCriteriaIdsException
  57.      */
  58.     public function handle(Event $event): void
  59.     {
  60.         if (!$event instanceof FlowEvent) {
  61.             return;
  62.         }
  63.         $mailEvent $event->getEvent();
  64.         if ($mailEvent instanceof MailAware) {
  65.             //Load default recipients
  66.             $recipients $mailEvent->getMailStruct()->getRecipients();
  67.             //Check if there is email in customFields
  68.             if ($customEmail $this->getCustomEmail($event->getEvent(), $recipients)) {
  69.                 $mailEvent->getMailStruct()->setRecipients($customEmail);
  70.             }
  71.         }
  72.         $this->decoratedService->handle($event);
  73.     }
  74.     /**
  75.      * Get custom email from customFields
  76.      *
  77.      * @param MailAware $event
  78.      * @param array $recipients
  79.      * @return array|null
  80.      */
  81.     private function getCustomEmail(MailAware $event, array $recipients): ?array
  82.     {
  83.         try {
  84.             $customer $this->getCustomer($event);
  85.             if (isset($customer) && !empty($customer->getCustomFields())) {
  86.                 $customFields $customer->getCustomFields() ?? [];
  87.                 if (isset($customFields['contactEmail']) && !empty($customFields['contactEmail'])
  88.                     && $this->isValidEmail($customFields['contactEmail'])
  89.                 ) {
  90.                     return [$customFields['contactEmail'] => current($recipients)];
  91.                 }
  92.             }
  93.         } catch (Exception $exception) {
  94.             $this->logger->error('Not possible to find custom email: ' $exception->getMessage());
  95.         }
  96.         return null;
  97.     }
  98.     /**
  99.      * Get custom from different objects
  100.      *
  101.      * @param MailAware $event
  102.      * @return CustomerEntity|null
  103.      */
  104.     private function getCustomer(MailAware $event) :?CustomerEntity
  105.     {
  106.         if (true === method_exists($event,'getCustomer')) {
  107.             return $event->getCustomer();
  108.         }
  109.         if (true === method_exists($event,'getOrder')) {
  110.             return $event->getOrder()->getOrderCustomer()->getCustomer();
  111.         }
  112.         if (true === method_exists($event,'getNewsletterRecipient')) {
  113.             return $this->getCustomerByEmail($event->getNewsletterRecipient()->getEmail(), $event->getContext());
  114.         }
  115.         return null;
  116.     }
  117.     private function getCustomerByEmail(string $emailContext $context): ?CustomerEntity
  118.     {
  119.         $criteria = new Criteria();
  120.         $criteria->addFilter(new EqualsFilter('email'$email));
  121.         return $this->customerRepository->search($criteria$context)->first();
  122.     }
  123.     /**
  124.      * @param string $email
  125.      * @return bool
  126.      */
  127.     private function isValidEmail(string $email): bool
  128.     {
  129.         //Assume it's valid if we can't validate it
  130.         if (!function_exists('filter_var')) {
  131.             return true;
  132.         }
  133.         return false !== filter_var($emailFILTER_VALIDATE_EMAIL);
  134.     }
  135.     public function requirements(): array
  136.     {
  137.         return $this->decoratedService->requirements();
  138.     }
  139. }