custom/plugins/CoeRechnungsMailSw6/src/Subscriber/MailSubscriber.php line 55

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CoeRechnungsMailSw6\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEntity;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Core\Content\Flow\Events\FlowSendMailActionEvent;
  6. use Shopware\Core\Framework\Event\CustomerAware;
  7. use Shopware\Core\Framework\Event\DataMappingEvent;
  8. use Shopware\Core\Framework\Event\FlowEvent;
  9. use Shopware\Core\Framework\Event\OrderAware;
  10. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
  11. use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Symfony\Contracts\EventDispatcher\Event;
  15. /**
  16.  * @package CoeRechnungsMailSw6\Subscriber
  17.  * @author Jeffry Block <jeffry.block@codeenterprise.de>
  18.  */
  19. Class MailSubscriber implements EventSubscriberInterface{
  20.     /** @var SystemConfigService $systemConfigService*/
  21.     private $systemConfigService;
  22.     /**
  23.      * ProfileSubscriber constructor.
  24.      * @param SystemConfigService $systemConfigService
  25.      */
  26.     function __construct(SystemConfigService $systemConfigService)
  27.     {
  28.         $this -> systemConfigService $systemConfigService;
  29.     }
  30.     /**
  31.      * @return array
  32.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  33.      */
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             FlowSendMailActionEvent::class => 'onFlowSendMailAction'
  38.         ];
  39.     }
  40.     /**
  41.      * Replace the recipient email address if the flow-configuration is set to documentMail.
  42.      * In this case we need to obtain the documentMail address of the customer (form the custom fields) and assign it
  43.      * to the dataBag.
  44.      * @param FlowSendMailActionEvent $event
  45.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  46.      */
  47.     public function onFlowSendMailAction(FlowSendMailActionEvent $event){
  48.         /** @var array $flowConfig */
  49.         $flowConfig $event->getFlowEvent()->getConfig();
  50.         /** @var string $recipientType */
  51.         $recipientType $flowConfig["recipient"]["type"];
  52.         if($recipientType !== "documentMail"){
  53.             return;
  54.         }
  55.         /** @var CustomerEntity|null $customer */
  56.         $customer null;
  57.         /** @var Event $triggeredEvent */
  58.         $triggeredEvent $event->getFlowEvent()->getFlowState()->event;
  59.         if($triggeredEvent instanceof OrderAware){
  60.             /** @var OrderEntity $order */
  61.             $order $triggeredEvent->getOrder();
  62.             $customer $order->getOrderCustomer()->getCustomer();
  63.         }
  64.         else if($triggeredEvent instanceof CustomerAware){
  65.             $customer $triggeredEvent->getCustomer();
  66.         }
  67.         if(!$customer || !$customer->getCustomFields() || !$customer->getCustomFields()["coe_rechnungsmail_mail"]){
  68.             return;
  69.         }
  70.         /** @var string|null $documentMail */
  71.         $documentMail $customer->getCustomFields()["coe_rechnungsmail_mail"];
  72.         $event->getDataBag()->set("recipients", [$documentMail => $documentMail]);
  73.     }
  74. }