<?php declare(strict_types=1);
namespace CoeRechnungsMailSw6\Subscriber;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Content\Flow\Events\FlowSendMailActionEvent;
use Shopware\Core\Framework\Event\CustomerAware;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Shopware\Core\Framework\Event\FlowEvent;
use Shopware\Core\Framework\Event\OrderAware;
use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Contracts\EventDispatcher\Event;
/**
* @package CoeRechnungsMailSw6\Subscriber
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
Class MailSubscriber implements EventSubscriberInterface{
/** @var SystemConfigService $systemConfigService*/
private $systemConfigService;
/**
* ProfileSubscriber constructor.
* @param SystemConfigService $systemConfigService
*/
function __construct(SystemConfigService $systemConfigService)
{
$this -> systemConfigService = $systemConfigService;
}
/**
* @return array
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
public static function getSubscribedEvents(): array
{
return [
FlowSendMailActionEvent::class => 'onFlowSendMailAction'
];
}
/**
* Replace the recipient email address if the flow-configuration is set to documentMail.
* In this case we need to obtain the documentMail address of the customer (form the custom fields) and assign it
* to the dataBag.
* @param FlowSendMailActionEvent $event
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
public function onFlowSendMailAction(FlowSendMailActionEvent $event){
/** @var array $flowConfig */
$flowConfig = $event->getFlowEvent()->getConfig();
/** @var string $recipientType */
$recipientType = $flowConfig["recipient"]["type"];
if($recipientType !== "documentMail"){
return;
}
/** @var CustomerEntity|null $customer */
$customer = null;
/** @var Event $triggeredEvent */
$triggeredEvent = $event->getFlowEvent()->getFlowState()->event;
if($triggeredEvent instanceof OrderAware){
/** @var OrderEntity $order */
$order = $triggeredEvent->getOrder();
$customer = $order->getOrderCustomer()->getCustomer();
}
else if($triggeredEvent instanceof CustomerAware){
$customer = $triggeredEvent->getCustomer();
}
if(!$customer || !$customer->getCustomFields() || !$customer->getCustomFields()["coe_rechnungsmail_mail"]){
return;
}
/** @var string|null $documentMail */
$documentMail = $customer->getCustomFields()["coe_rechnungsmail_mail"];
$event->getDataBag()->set("recipients", [$documentMail => $documentMail]);
}
}