vendor/store.shopware.com/molliepayments/src/Subscriber/ShippingSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Kiener\MolliePayments\Event\MollieOrderShipmentTrackingEvent;
  4. use Kiener\MolliePayments\Exception\CouldNotExtractMollieOrderIdException;
  5. use Kiener\MolliePayments\Facade\MollieShipment;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class ShippingSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var MollieShipment
  12.      */
  13.     private $shipmentFacade;
  14.     /**
  15.      * @var LoggerInterface
  16.      */
  17.     private $logger;
  18.     public function __construct(
  19.         MollieShipment  $shipmentFacade,
  20.         LoggerInterface $logger
  21.     ) {
  22.         $this->shipmentFacade $shipmentFacade;
  23.         $this->logger $logger;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             MollieOrderShipmentTrackingEvent::class => 'onShipOrderWithTracking',
  29.         ];
  30.     }
  31.     /**
  32.      * @param MollieOrderShipmentTrackingEvent $event
  33.      */
  34.     public function onShipOrderWithTracking(MollieOrderShipmentTrackingEvent $event): void
  35.     {
  36.         try {
  37.             $this->shipmentFacade->shipOrderByOrderId(
  38.                 $event->getOrderId(),
  39.                 $event->getTrackingCarrier(),
  40.                 $event->getTrackingCode(),
  41.                 $event->getTrackingUrl(),
  42.                 $event->getContext()
  43.             );
  44.         } catch (CouldNotExtractMollieOrderIdException $e) {
  45.             // We need to catch CouldNotExtractMollieOrderIdException, because if it's not a Mollie Order
  46.             // it obviously cannot get shipped with Mollie. We also don't have to log this, except for debugging.
  47.             // But if we don't catch it, the rest of the process might break.
  48.             $this->logger->debug($e->getMessage(), [
  49.                 'orderId' => $event->getOrderId(),
  50.                 'trackingCarrier' => $event->getTrackingCarrier(),
  51.                 'trackingCode' => $event->getTrackingCode(),
  52.                 'trackingUrl' => $event->getTrackingUrl(),
  53.             ]);
  54.         } catch (\Exception $e) {
  55.             // We log the error, but don't rethrow so the rest of the proces can continue.
  56.             $this->logger->error(
  57.                 sprintf(
  58.                     "Error when shipping order from Mollie Event: \"%s\" in \"%s\" on line %s",
  59.                     $e->getMessage(),
  60.                     $e->getFile(),
  61.                     $e->getLine()
  62.                 ),
  63.                 [
  64.                     'orderId' => $event->getOrderId(),
  65.                     'trackingCarrier' => $event->getTrackingCarrier(),
  66.                     'trackingCode' => $event->getTrackingCode(),
  67.                     'trackingUrl' => $event->getTrackingUrl(),
  68.                 ]
  69.             );
  70.         }
  71.     }
  72. }