custom/plugins/BilliePaymentSW6/src/Components/StateMachine/Subscriber/TransitionSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (c) Billie GmbH
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace Billie\BilliePayment\Components\StateMachine\Subscriber;
  10. use Billie\BilliePayment\Components\BillieApi\Service\OperationService;
  11. use Billie\BilliePayment\Components\Order\Model\Extension\OrderExtension;
  12. use Billie\BilliePayment\Components\Order\Model\OrderDataEntity;
  13. use Billie\BilliePayment\Components\PluginConfig\Service\ConfigService;
  14. use Billie\BilliePayment\Util\CriteriaHelper;
  15. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryDefinition;
  16. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
  17. use Shopware\Core\Checkout\Order\OrderDefinition;
  18. use Shopware\Core\Checkout\Order\OrderEntity;
  19. use Shopware\Core\Framework\Context;
  20. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  22. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. class TransitionSubscriber implements EventSubscriberInterface
  25. {
  26.     private ConfigService $configService;
  27.     /**
  28.      * @var EntityRepository
  29.      * the interface has been deprecated, but shopware is using the Interface in a decorator for the repository.
  30.      * so it will crash, if we are only using EntityRepository, cause an object of the decorator got injected into the constructor.
  31.      * After Shopware has removed the decorator, we can replace this by a normal definition
  32.      * TODO remove comment on Shopware Version 6.5.0.0 & readd type hint & change constructor argument type
  33.      */
  34.     private object $orderDeliveryRepository;
  35.     /**
  36.      * @var EntityRepository
  37.      * the interface has been deprecated, but shopware is using the Interface in a decorator for the repository.
  38.      * so it will crash, if we are only using EntityRepository, cause an object of the decorator got injected into the constructor.
  39.      * After Shopware has removed the decorator, we can replace this by a normal definition
  40.      * TODO remove comment on Shopware Version 6.5.0.0 & readd type hint & change constructor argument type
  41.      */
  42.     private object $orderRepository;
  43.     private OperationService $operationService;
  44.     public function __construct(
  45.         object $orderDeliveryRepository,
  46.         object $orderRepository,
  47.         ConfigService $configService,
  48.         OperationService $operationService
  49.     ) {
  50.         $this->orderDeliveryRepository $orderDeliveryRepository;
  51.         $this->orderRepository $orderRepository;
  52.         $this->configService $configService;
  53.         $this->operationService $operationService;
  54.     }
  55.     public static function getSubscribedEvents(): array
  56.     {
  57.         return [
  58.             StateMachineTransitionEvent::class => 'onTransition',
  59.         ];
  60.     }
  61.     public function onTransition(StateMachineTransitionEvent $event): void
  62.     {
  63.         if (!$this->configService->isStateWatchingEnabled()) {
  64.             return;
  65.         }
  66.         if ($event->getEntityName() === OrderDeliveryDefinition::ENTITY_NAME) {
  67.             /** @var OrderDeliveryEntity $orderDelivery */
  68.             $orderDelivery $this->orderDeliveryRepository->search(new Criteria([$event->getEntityId()]), $event->getContext())->first();
  69.             $order $this->getOrder($orderDelivery->getOrderId(), $event->getContext());
  70.         } elseif ($event->getEntityName() === OrderDefinition::ENTITY_NAME) {
  71.             $order $this->getOrder($event->getEntityId(), $event->getContext());
  72.         } else {
  73.             return;
  74.         }
  75.         /** @var OrderDataEntity|null $billieData */
  76.         $billieData $order->getExtension(OrderExtension::EXTENSION_NAME);
  77.         if (!$billieData instanceof OrderDataEntity) {
  78.             // this is not a billie order - or if it is, the order data is broken
  79.             return;
  80.         }
  81.         switch ($event->getToPlace()->getTechnicalName()) {
  82.             case $this->configService->getStateForShip():
  83.                 $this->operationService->ship($order);
  84.                 break;
  85.             case $this->configService->getStateCancel():
  86.                 $this->operationService->cancel($order);
  87.                 break;
  88.             case $this->configService->getStateReturn():
  89.                 $this->operationService->return($order);
  90.                 break;
  91.         }
  92.     }
  93.     protected function getOrder(string $orderIdContext $context): OrderEntity
  94.     {
  95.         $criteria CriteriaHelper::getCriteriaForOrder($orderId);
  96.         $criteria->addAssociation('documents.documentType');
  97.         return $this->orderRepository->search($criteria$context)->first();
  98.     }
  99. }