custom/plugins/NetiNextEasyCoupon/src/Subscriber/Mail.php line 53

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @copyright Copyright (c) 2020, Net Inventors GmbH
  5.  * @category  Shopware
  6.  * @author    mpeters
  7.  */
  8. namespace NetInventors\NetiNextEasyCoupon\Subscriber;
  9. use NetInventors\NetiNextEasyCoupon\Service\OrderVoucherService;
  10. use NetInventors\NetiNextEasyCoupon\Service\PluginConfigFactory;
  11. use NetInventors\NetiNextEasyCoupon\Struct\PluginConfigStruct;
  12. use Shopware\Core\Checkout\Order\OrderEntity;
  13. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class Mail implements EventSubscriberInterface
  16. {
  17.     private PluginConfigStruct  $config;
  18.     private OrderVoucherService $orderVoucherService;
  19.     private PluginConfigFactory $pluginConfigFactory;
  20.     public function __construct(
  21.         PluginConfigStruct  $config,
  22.         OrderVoucherService $orderVoucherService,
  23.         PluginConfigFactory $pluginConfigFactory
  24.     ) {
  25.         $this->config              $config;
  26.         $this->orderVoucherService $orderVoucherService;
  27.         $this->pluginConfigFactory $pluginConfigFactory;
  28.     }
  29.     /**
  30.      * @return string[]
  31.      */
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             MailBeforeValidateEvent::class => 'beforeMailValidate',
  36.         ];
  37.     }
  38.     /**
  39.      * @param MailBeforeValidateEvent $event
  40.      *
  41.      * @return void
  42.      * @throws \Exception
  43.      */
  44.     public function beforeMailValidate(MailBeforeValidateEvent $event): void
  45.     {
  46.         $templateData $event->getTemplateData();
  47.         if (!(
  48.             isset($templateData['order'])
  49.             && $templateData['order'] instanceof OrderEntity
  50.         )) {
  51.             return;
  52.         }
  53.         $order             $templateData['order'];
  54.         $orderPluginConfig $this->pluginConfigFactory->create($order->getSalesChannelId());
  55.         if (!$orderPluginConfig->isActive()) {
  56.             return;
  57.         }
  58.         $this->orderVoucherService->addPurchaseVouchersToOrder($order$event->getContext());
  59.         $this->orderVoucherService->addCashedVouchersToOrder($order$event->getContext());
  60.     }
  61. }