<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020, Net Inventors GmbH
* @category Shopware
* @author mpeters
*/
namespace NetInventors\NetiNextEasyCoupon\Subscriber;
use NetInventors\NetiNextEasyCoupon\Service\OrderVoucherService;
use NetInventors\NetiNextEasyCoupon\Service\PluginConfigFactory;
use NetInventors\NetiNextEasyCoupon\Struct\PluginConfigStruct;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Mail implements EventSubscriberInterface
{
private PluginConfigStruct $config;
private OrderVoucherService $orderVoucherService;
private PluginConfigFactory $pluginConfigFactory;
public function __construct(
PluginConfigStruct $config,
OrderVoucherService $orderVoucherService,
PluginConfigFactory $pluginConfigFactory
) {
$this->config = $config;
$this->orderVoucherService = $orderVoucherService;
$this->pluginConfigFactory = $pluginConfigFactory;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
MailBeforeValidateEvent::class => 'beforeMailValidate',
];
}
/**
* @param MailBeforeValidateEvent $event
*
* @return void
* @throws \Exception
*/
public function beforeMailValidate(MailBeforeValidateEvent $event): void
{
$templateData = $event->getTemplateData();
if (!(
isset($templateData['order'])
&& $templateData['order'] instanceof OrderEntity
)) {
return;
}
$order = $templateData['order'];
$orderPluginConfig = $this->pluginConfigFactory->create($order->getSalesChannelId());
if (!$orderPluginConfig->isActive()) {
return;
}
$this->orderVoucherService->addPurchaseVouchersToOrder($order, $event->getContext());
$this->orderVoucherService->addCashedVouchersToOrder($order, $event->getContext());
}
}