custom/plugins/System4ShopTheme/src/Subscriber/MailSubscriber.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace System4ShopTheme\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class MailSubscriber implements EventSubscriberInterface {
  9.     /**
  10.      * @var SystemConfigService
  11.      */
  12.     private $systemConfigService;
  13.     /**
  14.      * @var EntityRepository
  15.      */
  16.     private $templateRepository;
  17.     /**
  18.      * @param SystemConfigService $systemConfigService
  19.      * @param EntityRepository $templateRepository
  20.      */
  21.     public function __construct(SystemConfigService $systemConfigServiceEntityRepository $templateRepository)
  22.     {
  23.         $this->systemConfigService $systemConfigService;
  24.         $this->templateRepository $templateRepository;
  25.     }
  26.     /**
  27.      * @inheritDoc
  28.      */
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             MailBeforeSentEvent::class => 'onMailSend'
  33.         ];
  34.     }
  35.     /**
  36.      * @param MailBeforeSentEvent $event
  37.      * @return void
  38.      */
  39.     public function onMailSend(MailBeforeSentEvent $event)
  40.     {
  41.         $salesChannelId $event->getData()['salesChannelId'];
  42.         
  43.         if (!$this->isConfigEmpty('customMailTemplates'$salesChannelId)) {
  44.             //if mail entity is selected in config
  45.             $template $this->getTemplate($event);
  46.             if (in_array($template->getMailTemplateTypeId(), $this->getConfig('customMailTemplates'$salesChannelId))) {
  47.                 $email $this->getConfig("emailCc"$salesChannelId);
  48.                 $event->getMessage()->addCc($email);
  49.             }
  50.         }
  51.     }
  52.     /**
  53.      * @param $path
  54.      * @param $salesChannelId
  55.      * @return array|bool|float|int|string|null
  56.      */
  57.     private function getConfig($path$salesChannelId) {
  58.         return $this->systemConfigService->get('System4ShopTheme.config.'.$path$salesChannelId);
  59.     }
  60.     /**
  61.      * @param MailBeforeSentEvent $event
  62.      * @return mixed|null
  63.      */
  64.     private function getTemplate(MailBeforeSentEvent $event) {
  65.         $templateId $event->getData()['templateId'];
  66.         $templates $this->templateRepository->search((new Criteria([$templateId])), $event->getContext());
  67.         return $templates->first();
  68.     }
  69.     /**
  70.      * @param $path
  71.      * @param $salesChannelId
  72.      * @return bool
  73.      */
  74.     private function isConfigEmpty($path$salesChannelId): bool
  75.     {
  76.         if(empty($this->getConfig($path$salesChannelId))) {
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81. }