custom/plugins/MolliePayments/src/Compatibility/Bundles/FlowBuilder/Actions/ShipOrderAction.php line 87

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\Actions;
  3. use Kiener\MolliePayments\Components\ShipmentManager\ShipmentManagerInterface;
  4. use Kiener\MolliePayments\Service\OrderServiceInterface;
  5. use Psr\Log\LoggerInterface;
  6. use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
  7. use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\Event\FlowEvent;
  10. use Shopware\Core\Framework\Event\OrderAware;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ShipOrderAction extends FlowAction implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var LoggerInterface
  16.      */
  17.     private $logger;
  18.     /**
  19.      * @var OrderServiceInterface
  20.      */
  21.     private $orderService;
  22.     /**
  23.      * @var ShipmentManagerInterface
  24.      */
  25.     private $shipment;
  26.     /**
  27.      * @param OrderServiceInterface $orderService
  28.      * @param ShipmentManagerInterface $shipment
  29.      * @param LoggerInterface $logger
  30.      */
  31.     public function __construct(OrderServiceInterface $orderServiceShipmentManagerInterface $shipmentLoggerInterface $logger)
  32.     {
  33.         $this->orderService $orderService;
  34.         $this->shipment $shipment;
  35.         $this->logger $logger;
  36.     }
  37.     /**
  38.      * @return string
  39.      */
  40.     public static function getName(): string
  41.     {
  42.         return 'action.mollie.order.ship';
  43.     }
  44.     /**
  45.      * @return string[]
  46.      */
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [
  50.             self::getName() => 'handle',
  51.         ];
  52.     }
  53.     /**
  54.      * @return string[]
  55.      */
  56.     public function requirements(): array
  57.     {
  58.         return [OrderAware::class];
  59.     }
  60.     /**
  61.      * @param StorableFlow $flow
  62.      * @throws \Exception
  63.      * @return void
  64.      */
  65.     public function handleFlow(StorableFlow $flow): void
  66.     {
  67.         $orderId $flow->getStore('orderId');
  68.         $this->shipOrder($orderId$flow->getContext());
  69.     }
  70.     /**
  71.      * @param FlowEvent $event
  72.      * @throws \Exception
  73.      */
  74.     public function handle(FlowEvent $event): void
  75.     {
  76.         $config $event->getConfig();
  77.         if (empty($config)) {
  78.             return;
  79.         }
  80.         $baseEvent $event->getEvent();
  81.         if (!$baseEvent instanceof OrderAware) {
  82.             return;
  83.         }
  84.         $orderId $baseEvent->getOrderId();
  85.         $this->shipOrder($orderId$baseEvent->getContext());
  86.     }
  87.     /**
  88.      * @param string $orderId
  89.      * @param Context $context
  90.      * @throws \Exception
  91.      * @return void
  92.      */
  93.     private function shipOrder(string $orderIdContext $context): void
  94.     {
  95.         $orderNumber '';
  96.         try {
  97.             $order $this->orderService->getOrder($orderId$context);
  98.             $orderNumber $order->getOrderNumber();
  99.             $this->logger->info('Starting Shipment through Flow Builder Action for order: ' $orderNumber);
  100.             # ship (all or) the rest of the order without providing any specific tracking information.
  101.             # this will ensure tracking data is automatically taken from the order
  102.             $this->shipment->shipOrderRest($ordernull$context);
  103.         } catch (\Exception $ex) {
  104.             $this->logger->error(
  105.                 'Error when shipping order with Flow Builder Action',
  106.                 [
  107.                     'error' => $ex->getMessage(),
  108.                     'order' => $orderNumber,
  109.                 ]
  110.             );
  111.             throw $ex;
  112.         }
  113.     }
  114. }