diff --git a/src/Facade/MollieShipment.php b/src/Facade/MollieShipment.php index 7fb507aec..1021bf181 100644 --- a/src/Facade/MollieShipment.php +++ b/src/Facade/MollieShipment.php @@ -11,6 +11,7 @@ use Kiener\MolliePayments\Service\MolliePaymentExtractor; use Kiener\MolliePayments\Service\OrderDeliveryService; use Kiener\MolliePayments\Service\OrderService; +use Kiener\MolliePayments\Service\TrackingInfoStructFactory; use Kiener\MolliePayments\Service\Transition\DeliveryTransitionServiceInterface; use Kiener\MolliePayments\Struct\MollieApi\ShipmentTrackingInfoStruct; use Psr\Log\LoggerInterface; @@ -62,11 +63,17 @@ class MollieShipment implements MollieShipmentInterface */ private $orderDataExtractor; + /** + * @var TrackingInfoStructFactory + */ + private $trackingInfoStructFactory; + /** * @var LoggerInterface */ private $logger; + /** * @param MolliePaymentExtractor $extractor * @param DeliveryTransitionServiceInterface $deliveryTransitionService @@ -77,7 +84,7 @@ class MollieShipment implements MollieShipmentInterface * @param OrderDataExtractor $orderDataExtractor * @param LoggerInterface $logger */ - public function __construct(MolliePaymentExtractor $extractor, DeliveryTransitionServiceInterface $deliveryTransitionService, Order $mollieApiOrderService, Shipment $mollieApiShipmentService, OrderDeliveryService $orderDeliveryService, OrderService $orderService, OrderDataExtractor $orderDataExtractor, LoggerInterface $logger) + public function __construct(MolliePaymentExtractor $extractor, DeliveryTransitionServiceInterface $deliveryTransitionService, Order $mollieApiOrderService, Shipment $mollieApiShipmentService, OrderDeliveryService $orderDeliveryService, OrderService $orderService, OrderDataExtractor $orderDataExtractor, TrackingInfoStructFactory $trackingInfoStructFactory, LoggerInterface $logger) { $this->extractor = $extractor; $this->deliveryTransitionService = $deliveryTransitionService; @@ -87,6 +94,7 @@ public function __construct(MolliePaymentExtractor $extractor, DeliveryTransitio $this->orderService = $orderService; $this->orderDataExtractor = $orderDataExtractor; $this->logger = $logger; + $this->trackingInfoStructFactory = $trackingInfoStructFactory; } /** @@ -161,7 +169,6 @@ public function setShipment(string $orderDeliveryId, Context $context): bool $lastTransaction = $this->extractor->extractLastMolliePayment($order->getTransactions()); if (!$lastTransaction instanceof OrderTransactionEntity) { - $this->logger->info( sprintf( 'The last transaction of the order (%s) is not a mollie payment! No shipment will be sent to mollie', @@ -172,7 +179,7 @@ public function setShipment(string $orderDeliveryId, Context $context): bool return false; } - $trackingInfoStruct = $this->createTrackingInfoStructFromDelivery($delivery); + $trackingInfoStruct = $this->trackingInfoStructFactory->createFromDelivery($delivery); $addedMollieShipment = $this->mollieApiOrderService->setShipment($mollieOrderId, $trackingInfoStruct, $order->getSalesChannelId()); @@ -254,7 +261,7 @@ public function shipOrder( $shipment = $this->mollieApiShipmentService->shipOrder( $mollieOrderId, $order->getSalesChannelId(), - $this->createTrackingInfoStruct($trackingCarrier, $trackingCode, $trackingUrl) + $this->trackingInfoStructFactory->create($trackingCarrier, $trackingCode, $trackingUrl) ); $delivery = $this->orderDataExtractor->extractDelivery($order, $context); @@ -375,7 +382,7 @@ public function shipItem( $order->getSalesChannelId(), $mollieOrderLineId, $quantity, - $this->createTrackingInfoStruct($trackingCarrier, $trackingCode, $trackingUrl) + $this->trackingInfoStructFactory->create($trackingCarrier, $trackingCode, $trackingUrl) ); $delivery = $this->orderDataExtractor->extractDelivery($order, $context); @@ -467,38 +474,4 @@ private function findMatchingLineItems(OrderEntity $order, string $itemIdentifie return false; }); } - private function createTrackingInfoStructFromDelivery(OrderDeliveryEntity $orderDeliveryEntity):?ShipmentTrackingInfoStruct{ - $trackingCodes = $orderDeliveryEntity->getTrackingCodes(); - $shippingMethod = $orderDeliveryEntity->getShippingMethod(); - if($shippingMethod === null){ - return null; - } - if (count($trackingCodes) !== 1) { - return null; - } - - return $this->createTrackingInfoStruct((string)$shippingMethod->getName(),$trackingCodes[0], (string)$shippingMethod->getTrackingUrl()); - } - - private function createTrackingInfoStruct(string $trackingCarrier, string $trackingCode, string $trackingUrl): ?ShipmentTrackingInfoStruct - { - if (empty($trackingCarrier) && empty($trackingCode)) { - return null; - } - - if (empty($trackingCarrier)) { - throw new \InvalidArgumentException('Missing Argument for Tracking Carrier!'); - } - - if (empty($trackingCode)) { - throw new \InvalidArgumentException('Missing Argument for Tracking Code!'); - } - - $trackingUrl = trim(sprintf($trackingUrl, $trackingCode)); - if (filter_var($trackingUrl, FILTER_VALIDATE_URL) === false) { - $trackingUrl = ''; - } - - return new ShipmentTrackingInfoStruct($trackingCarrier, $trackingCode, $trackingUrl); - } } diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index f822b00ad..1c529d7fd 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -178,6 +178,9 @@ %env(default::APP_URL)% + + + diff --git a/src/Resources/config/services/facades.xml b/src/Resources/config/services/facades.xml index b9b93894d..941f4edef 100644 --- a/src/Resources/config/services/facades.xml +++ b/src/Resources/config/services/facades.xml @@ -13,9 +13,11 @@ + + diff --git a/src/Service/MollieApi/Order.php b/src/Service/MollieApi/Order.php index b377fe896..3e1a7e145 100644 --- a/src/Service/MollieApi/Order.php +++ b/src/Service/MollieApi/Order.php @@ -398,7 +398,7 @@ public function setShipment(string $mollieOrderId, ?ShipmentTrackingInfoStruct $ { $mollieOrder = $this->getMollieOrder($mollieOrderId, $salesChannelId); $shipment = []; - if($trackingInfoStruct instanceof ShipmentTrackingInfoStruct){ + if ($trackingInfoStruct instanceof ShipmentTrackingInfoStruct) { $shipment['tracking'] = $trackingInfoStruct->toArray(); } /** @var OrderLine $orderLine */ diff --git a/src/Service/TrackingInfoStructFactory.php b/src/Service/TrackingInfoStructFactory.php new file mode 100644 index 000000000..06cec1949 --- /dev/null +++ b/src/Service/TrackingInfoStructFactory.php @@ -0,0 +1,54 @@ +getTrackingCodes(); + $shippingMethod = $orderDeliveryEntity->getShippingMethod(); + if ($shippingMethod === null) { + return null; + } + if (count($trackingCodes) !== 1) { + return null; + } + + return $this->create((string)$shippingMethod->getName(), $trackingCodes[0], (string)$shippingMethod->getTrackingUrl()); + } + + public function create(string $trackingCarrier, string $trackingCode, string $trackingUrl): ?ShipmentTrackingInfoStruct + { + if (empty($trackingCarrier) && empty($trackingCode)) { + return null; + } + + if (empty($trackingCarrier)) { + throw new \InvalidArgumentException('Missing Argument for Tracking Carrier!'); + } + + if (empty($trackingCode)) { + throw new \InvalidArgumentException('Missing Argument for Tracking Code!'); + } + + $trackingUrl = trim($trackingUrl . $trackingCode); + + if (filter_var($trackingUrl, FILTER_VALIDATE_URL) === false) { + $trackingUrl = ''; + } + + /** + * following characters are not allowed in the tracking URL {,},<,>,# + */ + if (preg_match_all('/[{}<>#]/m', $trackingUrl)) { + $trackingUrl = ''; + } + + return new ShipmentTrackingInfoStruct($trackingCarrier, $trackingCode, $trackingUrl); + } +} diff --git a/tests/PHPUnit/Facade/MollieShipment/CreateTrackingStructTest.php b/tests/PHPUnit/Facade/MollieShipment/CreateTrackingStructTest.php index 0664c1208..5ca7dc9ae 100644 --- a/tests/PHPUnit/Facade/MollieShipment/CreateTrackingStructTest.php +++ b/tests/PHPUnit/Facade/MollieShipment/CreateTrackingStructTest.php @@ -10,6 +10,7 @@ use Kiener\MolliePayments\Service\MolliePaymentExtractor; use Kiener\MolliePayments\Service\OrderDeliveryService; use Kiener\MolliePayments\Service\OrderService; +use Kiener\MolliePayments\Service\TrackingInfoStructFactory; use Kiener\MolliePayments\Service\Transition\DeliveryTransitionService; use Kiener\MolliePayments\Struct\MollieApi\ShipmentTrackingInfoStruct; use Mollie\Api\Resources\Shipment as ShipmentResource; @@ -71,6 +72,7 @@ public function setUp(): void 'extractDelivery' => $this->delivery ]); + $this->shipmentFacade = new MollieShipment( $this->createMock(MolliePaymentExtractor::class), $this->createMock(DeliveryTransitionService::class), @@ -79,6 +81,7 @@ public function setUp(): void $this->createMock(OrderDeliveryService::class), $this->orderService, $this->orderDataExtractor, + new TrackingInfoStructFactory(), new NullLogger(), ); diff --git a/tests/PHPUnit/Facade/MollieShipment/SetShipmentTest.php b/tests/PHPUnit/Facade/MollieShipment/SetShipmentTest.php index deee49636..bef93d763 100644 --- a/tests/PHPUnit/Facade/MollieShipment/SetShipmentTest.php +++ b/tests/PHPUnit/Facade/MollieShipment/SetShipmentTest.php @@ -11,6 +11,7 @@ use Kiener\MolliePayments\Service\MolliePaymentExtractor; use Kiener\MolliePayments\Service\OrderDeliveryService; use Kiener\MolliePayments\Service\OrderService; +use Kiener\MolliePayments\Service\TrackingInfoStructFactory; use Kiener\MolliePayments\Service\Transition\DeliveryTransitionService; use Monolog\Logger; use PHPUnit\Framework\TestCase; @@ -100,6 +101,8 @@ public function setup(): void $this->createMock(CustomerService::class) ); + $trackingStructFactory = new TrackingInfoStructFactory(); + $this->mollieShipment = new MollieShipment( $this->extractor, $this->deliveryTransitionService, @@ -108,6 +111,7 @@ public function setup(): void $this->orderDeliveryService, $this->orderService, $this->orderDataExtractor, + $trackingStructFactory, $this->logger ); $this->orderNumber = 'fooOrderNumber'; diff --git a/tests/PHPUnit/Service/TrackingInfoStructFactoryTest.php b/tests/PHPUnit/Service/TrackingInfoStructFactoryTest.php new file mode 100644 index 000000000..4fee1cdcf --- /dev/null +++ b/tests/PHPUnit/Service/TrackingInfoStructFactoryTest.php @@ -0,0 +1,46 @@ +factory = new TrackingInfoStructFactory(); + } + + /** + * @dataProvider invalidCodes + * @param string $url + * @param string $trackingCode + * @return void + */ + public function testInvalidTrackingCodeCharacter(string $trackingCode):void{ + + $trackingInfoStruct = $this->factory->create('test',$trackingCode,'https://foo.bar'); + $expected =''; + $this->assertSame($expected,$trackingInfoStruct->getUrl()); + + } + public function invalidCodes():array{ + return [ + ['some{code'], + ['some}code'], + ['somecode'], + ['some#code'], + ]; + } +} \ No newline at end of file