Skip to content

Commit

Permalink
MOL-1196: update comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Dec 4, 2023
1 parent 2dbf5af commit 6757864
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
32 changes: 23 additions & 9 deletions src/Service/TrackingInfoStructFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,32 @@ class TrackingInfoStructFactory
*/
const MAX_TRACKING_CODE_LENGTH = 99;

public function createFromDelivery(OrderDeliveryEntity $orderDeliveryEntity):?ShipmentTrackingInfoStruct
public function createFromDelivery(OrderDeliveryEntity $orderDeliveryEntity): ?ShipmentTrackingInfoStruct
{
$trackingCodes = $orderDeliveryEntity->getTrackingCodes();
$shippingMethod = $orderDeliveryEntity->getShippingMethod();
if ($shippingMethod === null) {
return null;
}
/**
* mollie accepts only one tracking struct with code per shippment
* Currently we create one shipping in mollie for one order. one shipping object can have only one tracking code.
* When we have multiple Tracking Codes, we do not know which tracking code we should send to mollie. So we just dont send any tracking information at all
*
* https://docs.mollie.com/reference/v2/shipments-api/create-shipment
*/
if (count($trackingCodes) !== 1) {
return null;
}

return $this->create((string)$shippingMethod->getName(), $trackingCodes[0], (string)$shippingMethod->getTrackingUrl());
return $this->createInfoStruct((string)$shippingMethod->getName(), $trackingCodes[0], (string)$shippingMethod->getTrackingUrl());
}

public function create(string $trackingCarrier, string $trackingCode, string $trackingUrl): ?ShipmentTrackingInfoStruct
{
return $this->createInfoStruct($trackingCarrier, $trackingCode, $trackingUrl);
}

private function createInfoStruct(string $trackingCarrier, string $trackingCode, string $trackingUrl): ?ShipmentTrackingInfoStruct
{
if (empty($trackingCarrier) && empty($trackingCode)) {
return null;
Expand All @@ -46,20 +52,28 @@ public function create(string $trackingCarrier, string $trackingCode, string $tr
throw new \InvalidArgumentException('Missing Argument for Tracking Code!');
}

if (strpos($trackingUrl, '%s') === false) {
throw new \InvalidArgumentException('Missing %s as code placeholder in Tracking URL');
# we just have to completely remove those codes, so that no tracking happens, but a shipping works.
# still, if we find multiple codes (because separators exist), then we use the first one only
if (mb_strlen($trackingCode) > self::MAX_TRACKING_CODE_LENGTH) {
if (strpos($trackingCode, ',') !== false) {
$trackingCode = trim(explode(',', $trackingCode)[0]);
} elseif (strpos($trackingCode, ';') !== false) {
$trackingCode = trim(explode(';', $trackingCode)[0]);
}

# if we are still too long, then simply remove the code
if (mb_strlen($trackingCode) > self::MAX_TRACKING_CODE_LENGTH) {
return new ShipmentTrackingInfoStruct($trackingCarrier, '', '');
}
}


$trackingUrl = trim(sprintf($trackingUrl, $trackingCode));

if (filter_var($trackingUrl, FILTER_VALIDATE_URL) === false) {
$trackingUrl = '';
}

if (mb_strlen($trackingCode) > self::MAX_TRACKING_CODE_LENGTH) {
$trackingUrl = '';
}

/**
* following characters are not allowed in the tracking URL {,},<,>,#
*/
Expand Down
42 changes: 37 additions & 5 deletions tests/PHPUnit/Service/TrackingInfoStructFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,43 @@ public function testInfoStructCreatedByArguments(): void

}

public function testExceptionIsThrownWithoutPlaceholderInUrl(): void
{
$this->expectException(\InvalidArgumentException::class);
$trackingInfoStruct = $this->factory->create('Test', 'testCode', 'https://test.foo?code');
public function testInfoStructWithCommaSeparator():void{
$expectedCode = '1234';
$givenCode = $expectedCode.','.str_repeat('-',100);
$expectedCarrier = 'Test carrier';
$trackingInfoStruct = $this->factory->create($expectedCarrier, $givenCode, 'https://test.foo?code=%s');
$expectedUrl = 'https://test.foo?code=1234';

$this->assertNull($trackingInfoStruct);
$this->assertNotNull($trackingInfoStruct);
$this->assertSame($expectedCode, $trackingInfoStruct->getCode());
$this->assertSame($expectedUrl, $trackingInfoStruct->getUrl());
$this->assertSame($expectedCarrier, $trackingInfoStruct->getCarrier());
}

public function testInfoStructWithSemicolonSeparator():void{
$expectedCode = '1234';
$givenCode = $expectedCode.';'.str_repeat('-',100);
$expectedCarrier = 'Test carrier';
$trackingInfoStruct = $this->factory->create($expectedCarrier, $givenCode, 'https://test.foo?code=%s');
$expectedUrl = 'https://test.foo?code=1234';

$this->assertNotNull($trackingInfoStruct);
$this->assertSame($expectedCode, $trackingInfoStruct->getCode());
$this->assertSame($expectedUrl, $trackingInfoStruct->getUrl());
$this->assertSame($expectedCarrier, $trackingInfoStruct->getCarrier());
}

public function testCommaSeparatorHasHigherPriority():void{
$expectedCode = '1234';
$givenCode = $expectedCode.',5678;'.str_repeat('-',100);
$expectedCarrier = 'Test carrier';
$trackingInfoStruct = $this->factory->create($expectedCarrier, $givenCode, 'https://test.foo?code=%s');
$expectedUrl = 'https://test.foo?code=1234';

$this->assertNotNull($trackingInfoStruct);
$this->assertSame($expectedCode, $trackingInfoStruct->getCode());
$this->assertSame($expectedUrl, $trackingInfoStruct->getUrl());
$this->assertSame($expectedCarrier, $trackingInfoStruct->getCarrier());
}

/**
Expand All @@ -114,6 +145,7 @@ public function invalidCodes(): array
['some<code'],
['some>code'],
['some#code'],
['some#<>{},'.str_repeat('1',200)],
[str_repeat('1', 200)],
];
}
Expand Down

0 comments on commit 6757864

Please sign in to comment.