custom/plugins/MolliePayments/src/Subscriber/OrderDeliverySubscriber.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Kiener\MolliePayments\Facade\MollieShipment;
  4. use Kiener\MolliePayments\Service\SettingsService;
  5. use Psr\Log\LoggerInterface;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionActions;
  8. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class OrderDeliverySubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var SettingsService
  14.      */
  15.     private $settings;
  16.     /**
  17.      * @var MollieShipment
  18.      */
  19.     private $mollieShipment;
  20.     /**
  21.      * @var LoggerInterface
  22.      */
  23.     private $logger;
  24.     /**
  25.      * @param SettingsService $settings
  26.      * @param MollieShipment $mollieShipment
  27.      * @param LoggerInterface $logger
  28.      */
  29.     public function __construct(SettingsService $settingsMollieShipment $mollieShipmentLoggerInterface $logger)
  30.     {
  31.         $this->settings $settings;
  32.         $this->mollieShipment $mollieShipment;
  33.         $this->logger $logger;
  34.     }
  35.     /**
  36.      * @return array<mixed>
  37.      */
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             'state_machine.order_delivery.state_changed' => 'onOrderDeliveryChanged',
  42.         ];
  43.     }
  44.     /**
  45.      * @param StateMachineStateChangeEvent $event
  46.      */
  47.     public function onOrderDeliveryChanged(StateMachineStateChangeEvent $event): void
  48.     {
  49.         if ($event->getTransitionSide() !== StateMachineStateChangeEvent::STATE_MACHINE_TRANSITION_SIDE_ENTER) {
  50.             return;
  51.         }
  52.         $transitionName $event->getTransition()->getTransitionName();
  53.         if ($transitionName !== StateMachineTransitionActions::ACTION_SHIP) {
  54.             return;
  55.         }
  56.         # get the configuration of the sales channel from the order
  57.         $configSalesChannel $this->settings->getSettings($event->getSalesChannelId());
  58.         # if we don't even configure automatic shipping
  59.         # then don't even look into our order to find out if we should actually starts
  60.         if (!$configSalesChannel->getAutomaticShipping()) {
  61.             return;
  62.         }
  63.         /** @var ?OrderEntity $mollieOrder */
  64.         $mollieOrder $this->mollieShipment->isMollieOrder($event->getTransition()->getEntityId(), $event->getContext());
  65.         # don't do anything for orders of other PSPs.
  66.         # the code below would also create logs until we refactor it, which is wrong for other PSPs
  67.         if (!$mollieOrder instanceof OrderEntity) {
  68.             return;
  69.         }
  70.         
  71.         $this->logger->info('Starting Shipment through Order Delivery Transition for order: ' $mollieOrder->getOrderNumber());
  72.         $this->mollieShipment->setShipment($event->getTransition()->getEntityId(), $event->getContext());
  73.     }
  74. }