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

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