custom/plugins/NewsletterSendinblue/src/Subscriber/CartOrderSubscriber.php line 108

Open in your IDE?
  1. <?php
  2. namespace NewsletterSendinblue\Subscriber;
  3. use Monolog\Logger;
  4. use NewsletterSendinblue\Service\Cart\CartEventProducer;
  5. use NewsletterSendinblue\Service\ConfigService;
  6. use NewsletterSendinblue\Service\OrderSyncService;
  7. use NewsletterSendinblue\Traits\HelperTrait;
  8. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  9. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
  10. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  11. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
  12. use Shopware\Core\Checkout\Order\OrderDefinition;
  13. use Shopware\Core\Checkout\Order\OrderEntity;
  14. use Shopware\Core\Checkout\Order\OrderStates;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
  18. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  19. use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class CartOrderSubscriber implements EventSubscriberInterface
  22. {
  23.     use HelperTrait;
  24.     const ORDER_OPEN 'open';
  25.     /**
  26.      * @var ConfigService
  27.     */
  28.     private $configService;
  29.     /**
  30.      * @var CartEventProducer
  31.     */
  32.     private $cartEventProducer;
  33.     /**
  34.      * @var EntityRepositoryInterface
  35.      */
  36.     private $systemConfigRepository;
  37.     /**
  38.      * @var EntityRepositoryInterface
  39.      */
  40.     private $orderTransactionRepository;
  41.     /**
  42.      * @var OrderSyncService
  43.      */
  44.     private $orderSyncService;
  45.     /**
  46.      * @var Logger
  47.      */
  48.     private $logger;
  49.     /**
  50.      * @var EntityRepositoryInterface
  51.      */
  52.     private $brevoAbandonedCartRepository;
  53.     /**
  54.      * @param ConfigService $configService
  55.      * @param CartEventProducer $cartEventProducer
  56.      * @param EntityRepositoryInterface $systemConfigRepository
  57.      * @param EntityRepositoryInterface $orderTransactionRepository
  58.      * @param OrderSyncService $orderSyncService
  59.      * @param Logger $logger
  60.      */
  61.     public function __construct(
  62.         ConfigService             $configService,
  63.         CartEventProducer         $cartEventProducer,
  64.         EntityRepositoryInterface $systemConfigRepository,
  65.         EntityRepositoryInterface $orderTransactionRepository,
  66.         OrderSyncService          $orderSyncService,
  67.         Logger                    $logger,
  68.         EntityRepositoryInterface $brevoAbandonedCartRepository
  69.     )
  70.     {
  71.         $this->configService $configService;
  72.         $this->cartEventProducer $cartEventProducer;
  73.         $this->systemConfigRepository $systemConfigRepository;
  74.         $this->orderTransactionRepository $orderTransactionRepository;
  75.         $this->orderSyncService $orderSyncService;
  76.         $this->logger $logger;
  77.         $this->brevoAbandonedCartRepository $brevoAbandonedCartRepository;
  78.     }
  79.     /**
  80.      * @return string[]
  81.      */
  82.     public static function getSubscribedEvents(): array
  83.     {
  84.         return [
  85.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  86.             StateMachineTransitionEvent::class => 'onStateChanged',
  87.         ];
  88.     }
  89.     /**
  90.      * @param StateMachineTransitionEvent $event
  91.      * @return void
  92.      */
  93.     public function onStateChanged(StateMachineTransitionEvent $event): void
  94.     {
  95.         $connectionId $this->getAutoSyncConnectionId(
  96.             ConfigService::CONFIG_IS_ORDERS_AUTO_SYNC_ENABLED,
  97.             $event->getContext()
  98.         );
  99.         if (empty($connectionId)) {
  100.             return;
  101.         }
  102.         $entity $event->getEntityName();
  103.         $toState $event->getToPlace()->getTechnicalName();
  104.         if (
  105.             ($entity === OrderTransactionDefinition::ENTITY_NAME
  106.                 && in_array($toStateOrderSyncService::PAYMENT_SYNC_STATUSES))
  107.         ) {
  108.             $orderId $this->getOrderId($event);
  109.             if (!$orderId) {
  110.                 return;
  111.             }
  112.             $order $this->orderSyncService->getOrder($orderId$event->getContext());
  113.             if (!$order instanceof OrderEntity) {
  114.                 return;
  115.             }
  116.             if ($toState === OrderTransactionStates::STATE_CHARGEBACK) {
  117.                 $toState OrderTransactionStates::STATE_OPEN;
  118.             }
  119.             $this->orderSyncService->sync($order$connectionId$toState$event->getContext());
  120.         }
  121.     }
  122.     /**
  123.      * @param CheckoutOrderPlacedEvent $event
  124.      * @return void
  125.      */
  126.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  127.     {
  128.         $this->configService->setSalesChannelId($event->getSalesChannelId());
  129.         if ($this->configService->isAutoSyncEnabled()) {
  130.             $this->cartEventProducer->createTransactionalOrder(
  131.                 $event->getOrder(), $event->getContext(), $event->getSalesChannelId()
  132.             );
  133.         }
  134.         // ORDER AUTO SYNC
  135.         $connectionId $this->getAutoSyncConnectionId(
  136.             ConfigService::CONFIG_IS_ORDERS_AUTO_SYNC_ENABLED,
  137.             $event->getContext()
  138.         );
  139.         if (!empty($connectionId)) {
  140.             $order $event->getOrder();
  141.             $toState self::ORDER_OPEN;
  142.             if ($order->getTransactions()->first() instanceof OrderTransactionEntity
  143.                 && $order->getTransactions()->first()->getStateMachineState() instanceof StateMachineStateEntity
  144.             ) {
  145.                 $toState $order->getTransactions()->first()->getStateMachineState()->getTechnicalName();
  146.             }
  147.             $this->orderSyncService->sync($order$connectionId$toState$event->getContext());
  148.         }
  149.         if ($this->configService->isAbandonedCartTrackingEnabled()) {
  150.             if ($event->getOrder()->getOrderCustomer() instanceof OrderCustomerEntity) {
  151.                 try {
  152.                     $this->brevoAbandonedCartRepository->delete([
  153.                         ['id' => md5($event->getOrder()->getOrderCustomer()->getEmail())]
  154.                     ], $event->getContext());
  155.                 } catch (\Throwable $e) {
  156.                 }
  157.             }
  158.         }
  159.     }
  160.     /**
  161.      * @param StateMachineTransitionEvent $event
  162.      * @return string|null
  163.      */
  164.     private function getOrderId(StateMachineTransitionEvent $event): ?string
  165.     {
  166.         $orderId null;
  167.         switch ($event->getEntityName()) {
  168.             case OrderDefinition::ENTITY_NAME:
  169.                 $orderId $event->getEntityId();
  170.                 break;
  171.             case OrderTransactionDefinition::ENTITY_NAME:
  172.                 $transactions $this->orderTransactionRepository->search(new Criteria([$event->getEntityId()]), $event->getContext());
  173.                 if (count($transactions) > 0) {
  174.                     $orderId $transactions->first()->getOrderId();
  175.                 }
  176.                 break;
  177.         }
  178.         return $orderId;
  179.     }
  180. }