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

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