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

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\Content\Flow\Dispatching\StorableFlow;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\Event\FlowEvent;
  12. use Shopware\Core\Framework\Event\OrderAware;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ShipOrderAction extends FlowAction implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var LoggerInterface
  18.      */
  19.     private $logger;
  20.     /**
  21.      * @var OrderServiceInterface
  22.      */
  23.     private $orderService;
  24.     /**
  25.      * @var MollieShipmentInterface
  26.      */
  27.     private $shipmentFacade;
  28.     /**
  29.      * @param OrderServiceInterface $orderService
  30.      * @param MollieShipmentInterface $shipment
  31.      * @param LoggerInterface $logger
  32.      */
  33.     public function __construct(OrderServiceInterface $orderServiceMollieShipmentInterface $shipmentLoggerInterface $logger)
  34.     {
  35.         $this->orderService $orderService;
  36.         $this->shipmentFacade $shipment;
  37.         $this->logger $logger;
  38.     }
  39.     /**
  40.      * @return string
  41.      */
  42.     public static function getName(): string
  43.     {
  44.         return 'action.mollie.order.ship';
  45.     }
  46.     /**
  47.      * @return string[]
  48.      */
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             self::getName() => 'handle',
  53.         ];
  54.     }
  55.     /**
  56.      * @return string[]
  57.      */
  58.     public function requirements(): array
  59.     {
  60.         return [OrderAware::class];
  61.     }
  62.     /**
  63.      * @param StorableFlow $flow
  64.      * @throws \Exception
  65.      * @return void
  66.      */
  67.     public function handleFlow(StorableFlow $flow): void
  68.     {
  69.         $orderId $flow->getStore('orderId');
  70.         $this->shipOrder($orderId$flow->getContext());
  71.     }
  72.     /**
  73.      * @param FlowEvent $event
  74.      * @throws \Exception
  75.      */
  76.     public function handle(FlowEvent $event): void
  77.     {
  78.         $config $event->getConfig();
  79.         if (empty($config)) {
  80.             return;
  81.         }
  82.         $baseEvent $event->getEvent();
  83.         if (!$baseEvent instanceof OrderAware) {
  84.             return;
  85.         }
  86.         $orderId $baseEvent->getOrderId();
  87.         $this->shipOrder($orderId$baseEvent->getContext());
  88.     }
  89.     /**
  90.      * @param string $orderId
  91.      * @param Context $context
  92.      * @throws \Exception
  93.      * @return void
  94.      */
  95.     private function shipOrder(string $orderIdContext $context): void
  96.     {
  97.         $orderNumber '';
  98.         try {
  99.             $order $this->orderService->getOrder($orderId$context);
  100.             $orderNumber $order->getOrderNumber();
  101.             $this->logger->info('Starting Shipment through Flow Builder Action for order: ' $orderNumber);
  102.             $this->shipmentFacade->shipOrder(
  103.                 $order,
  104.                 '',
  105.                 '',
  106.                 '',
  107.                 $context
  108.             );
  109.         } catch (\Exception $ex) {
  110.             $this->logger->error(
  111.                 'Error when shipping order with Flow Builder Action',
  112.                 [
  113.                     'error' => $ex->getMessage(),
  114.                     'order' => $orderNumber,
  115.                 ]
  116.             );
  117.             throw $ex;
  118.         }
  119.     }
  120. }