custom/plugins/MolliePayments/src/Subscriber/OrderLinesUpdatedSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Kiener\MolliePayments\Subscriber;
  4. use Kiener\MolliePayments\Event\OrderLinesUpdatedEvent;
  5. use Mollie\Api\Resources\OrderLine;
  6. use Mollie\Api\Types\OrderLineType;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class OrderLinesUpdatedSubscriber implements EventSubscriberInterface
  10. {
  11.     private LoggerInterface $logger;
  12.     public function __construct(LoggerInterface $logger)
  13.     {
  14.         $this->logger $logger;
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             OrderLinesUpdatedEvent::class => 'onOrderLinesUpdated',
  20.         ];
  21.     }
  22.     public function onOrderLinesUpdated(OrderLinesUpdatedEvent $event): void
  23.     {
  24.         $mollieOrder $event->getMollieOrder();
  25.         $shippingOptions = [];
  26.         $mollieLines $mollieOrder->lines();
  27.         /**
  28.          * @var OrderLine $line
  29.          */
  30.         foreach ($mollieLines as $line) {
  31.             if ($line->type === OrderLineType::TYPE_SHIPPING_FEE && $line->shippableQuantity 0) {
  32.                 $shippingOptions[] = [
  33.                     'id' => $line->id,
  34.                     'quantity' => $line->quantity,
  35.                 ];
  36.             }
  37.         }
  38.         if (count($shippingOptions) === 0) {
  39.             return;
  40.         }
  41.         try {
  42.             $mollieOrder->createShipment(['lines' => $shippingOptions]);
  43.         } catch (\Exception $exception) {
  44.             $this->logger->error('Failed to update shipping costs', ['message' => $exception->getMessage(), 'options' => $shippingOptions]);
  45.         }
  46.     }
  47. }