vendor/bitbag/mailtemplate-plugin/src/EmailSender/Sender.php line 79

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * You can find more information about us on https://bitbag.io and write us
  6.  * an email on hello@bitbag.io.
  7.  */
  8. declare(strict_types=1);
  9. namespace BitBag\SyliusMailTemplatePlugin\EmailSender;
  10. use BitBag\SyliusMailTemplatePlugin\Repository\EmailTemplateTranslationRepositoryInterface;
  11. use Sylius\Component\Mailer\Provider\DefaultSettingsProviderInterface;
  12. use Sylius\Component\Mailer\Provider\EmailProviderInterface;
  13. use Sylius\Component\Mailer\Renderer\Adapter\AdapterInterface as RendererAdapterInterface;
  14. use Sylius\Component\Mailer\Sender\Adapter\AdapterInterface as SenderAdapterInterface;
  15. use Sylius\Component\Mailer\Sender\SenderInterface;
  16. final class Sender implements SenderInterface
  17. {
  18.     public const CUSTOM_EMAIL_TEMPLATE_PATH '@BitBagSyliusMailTemplatePlugin/Admin/Email/customTemplate.html.twig';
  19.     public const TEMPLATE 'template';
  20.     public const LOCALE_CODE_KEY 'localeCode';
  21.     private RendererAdapterInterface $rendererAdapter;
  22.     private SenderAdapterInterface $senderAdapter;
  23.     private EmailProviderInterface $provider;
  24.     private DefaultSettingsProviderInterface $defaultSettingsProvider;
  25.     private EmailTemplateTranslationRepositoryInterface $templateTranslationRepository;
  26.     public function __construct(
  27.         RendererAdapterInterface $rendererAdapter,
  28.         SenderAdapterInterface $senderAdapter,
  29.         EmailProviderInterface $provider,
  30.         DefaultSettingsProviderInterface $defaultSettingsProvider,
  31.         EmailTemplateTranslationRepositoryInterface $templateTranslationRepository
  32.     ) {
  33.         $this->senderAdapter $senderAdapter;
  34.         $this->rendererAdapter $rendererAdapter;
  35.         $this->provider $provider;
  36.         $this->defaultSettingsProvider $defaultSettingsProvider;
  37.         $this->templateTranslationRepository $templateTranslationRepository;
  38.     }
  39.     public function send(
  40.         string $code,
  41.         array $recipients,
  42.         array $data = [],
  43.         array $attachments = [],
  44.         array $replyTo = []
  45.     ): void {
  46.         $email $this->provider->getEmail($code);
  47.         if (!$email->isEnabled()) {
  48.             return;
  49.         }
  50.         $customTemplate $this->templateTranslationRepository->findOneByLocaleCodeAndType(
  51.             $data[self::LOCALE_CODE_KEY] ?? '',
  52.             $code
  53.         );
  54.         if (null !== $customTemplate) {
  55.             $email->setTemplate(self::CUSTOM_EMAIL_TEMPLATE_PATH);
  56.             $data[self::TEMPLATE] = $customTemplate;
  57.         }
  58.         $senderAddress $email->getSenderAddress() ?? $this->defaultSettingsProvider->getSenderAddress();
  59.         $senderName $email->getSenderName() ?? $this->defaultSettingsProvider->getSenderName();
  60.         $renderedEmail $this->rendererAdapter->render($email$data);
  61.         $this->senderAdapter->send(
  62.             $recipients/** @phpstan-ignore-line */
  63.             $senderAddress,
  64.             $senderName,
  65.             $renderedEmail,
  66.             $email,
  67.             $data,
  68.             $attachments,
  69.             $replyTo
  70.         );
  71.     }
  72. }