custom/plugins/mmeesSlackNotifier/src/Listener/OrderStateChangeEventListener.php line 201

Open in your IDE?
  1. <?php
  2. /**
  3.  * Order state change event listeners
  4.  *
  5.  * @copyright Copyright © 2020 e-mmer. All rights reserved.
  6.  * @author    maurits@e-mmer.nl
  7.  */
  8. declare(strict_types=1);
  9. namespace Mmeester\SlackNotifier\Listener;
  10. use Mmeester\SlackNotifier\Helper\SlackHelper;
  11. use Mmeester\SlackNotifier\Helper\SettingsHelper;
  12. use Shopware\Core\Checkout\Cart\Exception\OrderDeliveryNotFoundException;
  13. use Shopware\Core\Checkout\Cart\Exception\OrderNotFoundException;
  14. use Shopware\Core\Checkout\Cart\Exception\OrderTransactionNotFoundException;
  15. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
  16. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  17. use Shopware\Core\Checkout\Order\OrderEntity;
  18. use Shopware\Core\Framework\Context;
  19. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  21. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  22. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  23. class OrderStateChangeEventListener
  24. {
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private $orderRepository;
  29.     /**
  30.      * @var EntityRepositoryInterface
  31.      */
  32.     private $orderTransactionRepository;
  33.     /**
  34.      * @var EntityRepositoryInterface
  35.      */
  36.     private $orderDeliveryRepository;
  37.     /**
  38.      * @var EventDispatcherInterface
  39.      */
  40.     private $eventDispatcher;
  41.     /**
  42.      * @var Slack
  43.      */
  44.     private $slack;
  45.     /**
  46.      * @var SettingsHelper
  47.      */
  48.     private $settings;
  49.     /**
  50.      * Count number of times state change is called to only show message once
  51.      *
  52.      * @var int
  53.      */
  54.     private $count 0;
  55.     public function __construct(
  56.         EntityRepositoryInterface $orderRepository,
  57.         EntityRepositoryInterface $orderTransactionRepository,
  58.         EntityRepositoryInterface $orderDeliveryRepository,
  59.         EventDispatcherInterface $eventDispatcher,
  60.         SlackHelper $slack,
  61.         SettingsHelper $settings
  62.     ) {
  63.         $this->orderRepository $orderRepository;
  64.         $this->orderTransactionRepository $orderTransactionRepository;
  65.         $this->orderDeliveryRepository $orderDeliveryRepository;
  66.         $this->eventDispatcher $eventDispatcher;
  67.         $this->slack $slack;
  68.         $this->settings $settings;
  69.     }
  70.     /**
  71.      * @throws OrderNotFoundException
  72.      * @throws OrderTransactionNotFoundException
  73.      */
  74.     public function onOrderTransactionStateChange(StateMachineStateChangeEvent $event): void
  75.     {
  76.         if($this->count === 0) {
  77.             $salesChannelId $event->getSalesChannelId();
  78.             if($this->settings->isOrderNotification("transactionStatus"$salesChannelId) === true) {
  79.                 $orderTransactionId $event->getTransition()->getEntityId();
  80.                 /** @var OrderTransactionEntity|null $orderTransaction */
  81.                 $orderTransaction $this->orderTransactionRepository->search(
  82.                     new Criteria([$orderTransactionId]),
  83.                     $event->getContext()
  84.                 )->first();
  85.                 if ($orderTransaction === null) {
  86.                     throw new OrderTransactionNotFoundException($orderTransactionId);
  87.                 }
  88.                 $orderId $orderTransaction->getOrderId();
  89.                 $order $this->getOrder($orderId$event->getContext());
  90.                 $customer $order->getOrderCustomer();
  91.                 $this->slack->sendMessage([
  92.                     "blocks" => [
  93.                         [
  94.                             "type" => "section",
  95.                             "text" => [
  96.                                 "type" => "mrkdwn",
  97.                                 "text" => "_Payment status_ for order (" $order->getOrderNumber() . ") changed: *" $event->getNextState()->getName() . "* \nCustomer: *" $customer->getFirstName() . " " $customer->getLastName() . "* <" $customer->getEmail() . ">"
  98.                             ]
  99.                         ],
  100.                         [
  101.                             "type" => "actions",
  102.                             "elements" => [
  103.                                 [
  104.                                     "type" => "button",
  105.                                     "style" => "primary",
  106.                                     "text" => [
  107.                                         "type" => "plain_text",
  108.                                         "text" => "View order"
  109.                                     ],
  110.                                     "url" => "https://" $_SERVER['SERVER_NAME'] . "/admin#/sw/order/detail/" $order->getId()
  111.                                 ]
  112.                             ]
  113.                         ]
  114.                     ]
  115.                 ], $salesChannelId);
  116.             }
  117.         }
  118.         $this->count++;
  119.     }
  120.     /**
  121.      * @throws OrderDeliveryNotFoundException
  122.      * @throws OrderNotFoundException
  123.      */
  124.     public function onOrderDeliveryStateChange(StateMachineStateChangeEvent $event): void
  125.     {
  126.         if($this->count === 0) {
  127.             $salesChannelId $event->getSalesChannelId();
  128.             if($this->settings->isOrderNotification("deliveryStatus"$salesChannelId) === true) {
  129.                 $orderDeliveryId $event->getTransition()->getEntityId();
  130.                 /** @var OrderDeliveryEntity|null $orderDelivery */
  131.                 $orderDelivery $this->orderDeliveryRepository->search(
  132.                     new Criteria([$orderDeliveryId]),
  133.                     $event->getContext()
  134.                 )->first();
  135.                 if ($orderDelivery === null) {
  136.                     throw new OrderDeliveryNotFoundException($orderDeliveryId);
  137.                 }
  138.                 $orderId $orderDelivery->getOrderId();
  139.                 $order $this->getOrder($orderId$event->getContext());
  140.                 $customer $order->getOrderCustomer();
  141.                 $this->slack->sendMessage([
  142.                     "blocks" => [
  143.                         [
  144.                             "type" => "section",
  145.                             "text" => [
  146.                                 "type" => "mrkdwn",
  147.                                 "text" => "_Delivery status_ for order (" $order->getOrderNumber() . ") changed: *" $event->getNextState()->getName() . "* \nCustomer: *" $customer->getFirstName() . " " $customer->getLastName() . "* <" $customer->getEmail() . ">"
  148.                             ]
  149.                         ],
  150.                         [
  151.                             "type" => "actions",
  152.                             "elements" => [
  153.                                 [
  154.                                     "type" => "button",
  155.                                     "style" => "primary",
  156.                                     "text" => [
  157.                                         "type" => "plain_text",
  158.                                         "text" => "View order"
  159.                                     ],
  160.                                     "url" => "https://" $_SERVER['SERVER_NAME'] . "/admin#/sw/order/detail/" $order->getId()
  161.                                 ]
  162.                             ]
  163.                         ]
  164.                     ]
  165.                 ], $salesChannelId);
  166.             }
  167.         }
  168.         $this->count++;
  169.     }
  170.     /**
  171.      * @throws OrderNotFoundException
  172.      */
  173.     public function onOrderStateChange(StateMachineStateChangeEvent $event): void
  174.     {
  175.         if($this->count === 0) {
  176.             $salesChannelId $event->getSalesChannelId();
  177.             if ($this->settings->isOrderNotification("orderStatus"$salesChannelId) === true) {
  178.                 $orderId $event->getTransition()->getEntityId();
  179.                 $order $this->getOrder($orderId$event->getContext());
  180.                 $customer $order->getOrderCustomer();
  181.                 $this->slack->sendMessage([
  182.                     "blocks" => [
  183.                         [
  184.                             "type" => "section",
  185.                             "text" => [
  186.                                 "type" => "mrkdwn",
  187.                                 "text" => "_Order status_ for order (" $order->getOrderNumber() . ") changed: *" $event->getNextState()->getName() . "* \nCustomer: *" $customer->getFirstName() . " " $customer->getLastName() . "* <" $customer->getEmail() . ">"
  188.                             ]
  189.                         ],
  190.                         [
  191.                             "type" => "actions",
  192.                             "elements" => [
  193.                                 [
  194.                                     "type" => "button",
  195.                                     "style" => "primary",
  196.                                     "text" => [
  197.                                         "type" => "plain_text",
  198.                                         "text" => "View order"
  199.                                     ],
  200.                                     "url" => "https://" $_SERVER['SERVER_NAME'] . "/admin#/sw/order/detail/" $order->getId()
  201.                                 ]
  202.                             ]
  203.                         ]
  204.                     ]
  205.                 ], $salesChannelId);
  206.             }
  207.         }
  208.         $this->count++;
  209.     }
  210.     /**
  211.      * @throws OrderNotFoundException
  212.      */
  213.     private function getOrder(string $orderIdContext $context): OrderEntity
  214.     {
  215.         $orderCriteria $this->getOrderCriteria($orderId);
  216.         /** @var OrderEntity|null $order */
  217.         $order $this->orderRepository->search($orderCriteria$context)->first();
  218.         if ($order === null) {
  219.             throw new OrderNotFoundException($orderId);
  220.         }
  221.         return $order;
  222.     }
  223.     private function getOrderCriteria(string $orderId): Criteria
  224.     {
  225.         $orderCriteria = new Criteria([$orderId]);
  226.         $orderCriteria->addAssociation('orderCustomer.salutation');
  227.         $orderCriteria->addAssociation('stateMachineState');
  228.         $orderCriteria->addAssociation('transactions');
  229.         $orderCriteria->addAssociation('deliveries.shippingMethod');
  230.         $orderCriteria->addAssociation('salesChannel');
  231.         return $orderCriteria;
  232.     }
  233. }