custom/plugins/WbfkOffer/src/Subscriber/MailSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\Offer\Subscriber;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Checkout\Order\OrderEntity;
  6. use Shopware\Core\Content\Flow\Events\FlowSendMailActionEvent;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Event\OrderAware;
  12. use Shopware\Core\System\User\UserEntity;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Wbfk\Offer\Core\Entity\Offer\OfferEntity;
  15. class MailSubscriber implements EventSubscriberInterface
  16. {
  17.     protected const ACCOUNT_MANAGER 'wbfkAccountManager';
  18.     protected const ASSIGNEE_OF_ORDER 'wbfkAssigneeOfOrder';
  19.     protected const CUSTOMER_AND_ACCOUNT_MANAGER 'wbfkCustomerAndAccountManager';
  20.     protected const CUSTOMER_AND_ASSIGNEE_OF_ORDER 'wbfkCustomerAndAssigneeOfOrder';
  21.     protected const RECIPIENT_TYPES = [
  22.         self::ACCOUNT_MANAGER,
  23.         self::ASSIGNEE_OF_ORDER,
  24.         self::CUSTOMER_AND_ACCOUNT_MANAGER,
  25.         self::CUSTOMER_AND_ASSIGNEE_OF_ORDER,
  26.     ];
  27.     public function __construct(
  28.         protected readonly EntityRepository $customerRepository,
  29.         protected readonly EntityRepository $offerRepository
  30.     )
  31.     {
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             FlowSendMailActionEvent::class => 'onFlowSendMailAction',
  37.         ];
  38.     }
  39.     public function onFlowSendMailAction(FlowSendMailActionEvent $event): void
  40.     {
  41.         $context $event->getContext();
  42.         $flowConfig $event->getFlowEvent()->getConfig();
  43.         $recipientType $flowConfig['recipient']['type'];
  44.         if(!in_array($recipientTypeself::RECIPIENT_TYPES)) {
  45.             return;
  46.         }
  47.         $triggeredEvent $event->getFlowEvent()->getEvent();
  48.         /** @var OrderEntity|null $order */
  49.         $order null;
  50.         if($triggeredEvent instanceof OrderAware) {
  51.             $order $triggeredEvent->getOrder();
  52.         }
  53.         if(!isset($order)) {
  54.             return;
  55.         }
  56.         $event->getDataBag()->set('recipients', []);
  57.         $this->handleAccountManager($recipientType$event$order);
  58.         $this->handleAssigneeOfOrder($recipientType$event$order);
  59.         $this->handleCustomer($recipientType$event$order);
  60.     }
  61.     protected function handleAccountManager(
  62.         string $recipientType,
  63.         FlowSendMailActionEvent $event,
  64.         OrderEntity $order
  65.     ): void
  66.     {
  67.         if ($recipientType !== self::ACCOUNT_MANAGER &&
  68.             $recipientType !== self::CUSTOMER_AND_ACCOUNT_MANAGER) {
  69.             return;
  70.         }
  71.         $customer $order->getOrderCustomer()?->getCustomer();
  72.         if(!isset($customer)) {
  73.             return;
  74.         }
  75.         $accountManager $this->getAccountManagerFromCustomer($customer$event->getContext());
  76.         if(!isset($accountManager)) {
  77.             return;
  78.         }
  79.         $email $accountManager->getEmail();
  80.         if($recipientType === self::CUSTOMER_AND_ACCOUNT_MANAGER) {
  81.             $event->getDataBag()->set('recipientsBcc'$email);
  82.             return;
  83.         }
  84.         $name $accountManager->getFirstName().' '.$accountManager->getLastName();
  85.         $this->addRecipient($event$email$name);
  86.     }
  87.     protected function handleAssigneeOfOrder(
  88.         string $recipientType,
  89.         FlowSendMailActionEvent $event,
  90.         OrderEntity $order
  91.     ): void
  92.     {
  93.         if ($recipientType !== self::ASSIGNEE_OF_ORDER &&
  94.             $recipientType !== self::CUSTOMER_AND_ASSIGNEE_OF_ORDER) {
  95.             return;
  96.         }
  97.         $offer $this->getOfferFromOrder($order$event->getContext());
  98.         $assignee $offer?->getAssignee();
  99.         if(!isset($offer) || !isset($assignee)) {
  100.             return;
  101.         }
  102.         $name $assignee->getFirstName().' '.$assignee->getLastName();
  103.         $this->addRecipient($event$assignee->getEmail(), $name);
  104.     }
  105.     protected function handleCustomer(
  106.         string $recipientType,
  107.         FlowSendMailActionEvent $event,
  108.         OrderEntity $order
  109.     ): void
  110.     {
  111.         if ($recipientType !== self::CUSTOMER_AND_ACCOUNT_MANAGER &&
  112.             $recipientType !== self::CUSTOMER_AND_ASSIGNEE_OF_ORDER) {
  113.             return;
  114.         }
  115.         $orderCustomer $order->getOrderCustomer();
  116.         if(!isset($orderCustomer)) {
  117.             return;
  118.         }
  119.         $name $orderCustomer->getFirstName().' '.$orderCustomer->getLastName();
  120.         $this->addRecipient($event$orderCustomer->getEmail(), $name);
  121.     }
  122.     protected function getOfferFromOrder(OrderEntity $orderContext $context): OfferEntity|null
  123.     {
  124.         $criteria = new Criteria();
  125.         $criteria->addFilter(new EqualsFilter('orderId'$order->getId()));
  126.         $criteria->addAssociation('assignee');
  127.         $criteria->setLimit(1);
  128.         return $this->offerRepository->search(
  129.             $criteria,
  130.             $context
  131.         )->first();
  132.     }
  133.     protected function getAccountManagerFromCustomer(CustomerEntity $customerContext $context): UserEntity|null
  134.     {
  135.         $criteria = new Criteria([$customer->getId()]);
  136.         $criteria->addAssociation('wbfkCustomerExtension.accountManager');
  137.         $criteria->setLimit(1);
  138.         $customer $this->customerRepository->search(
  139.             $criteria,
  140.             $context
  141.         )->first();
  142.         return $customer?->getExtension('wbfkCustomerExtension')?->getAccountManager();
  143.     }
  144.     protected function addRecipient(FlowSendMailActionEvent $eventstring $emailstring $name): void
  145.     {
  146.         $recipients $event->getDataBag()->get('recipients') ?? [];
  147.         $recipients[$email] = $name;
  148.         $event->getDataBag()->set('recipients'$recipients);
  149.     }
  150. }