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

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\Struct\PluginConfigStruct;
  11. use Shopware\Core\Checkout\Order\OrderEntity;
  12. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class Mail implements EventSubscriberInterface
  15. {
  16.     private PluginConfigStruct  $config;
  17.     private OrderVoucherService $orderVoucherService;
  18.     public function __construct(PluginConfigStruct $configOrderVoucherService $orderVoucherService)
  19.     {
  20.         $this->config              $config;
  21.         $this->orderVoucherService $orderVoucherService;
  22.     }
  23.     /**
  24.      * @return string[]
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             MailBeforeValidateEvent::class => 'beforeMailValidate',
  30.         ];
  31.     }
  32.     /**
  33.      * @param MailBeforeValidateEvent $event
  34.      *
  35.      * @return void
  36.      * @throws \Exception
  37.      */
  38.     public function beforeMailValidate(MailBeforeValidateEvent $event): void
  39.     {
  40.         if (!(
  41.             $this->config->isActive()
  42.             && isset($event->getTemplateData()['order'])
  43.             && $event->getTemplateData()['order'] instanceof OrderEntity
  44.         )) {
  45.             return;
  46.         }
  47.         $this->orderVoucherService->addPurchaseVouchersToOrder($event->getTemplateData()['order'], $event->getContext());
  48.         $this->orderVoucherService->addCashedVouchersToOrder($event->getTemplateData()['order'], $event->getContext());
  49.     }
  50. }