-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vitalij Mik
committed
Nov 27, 2023
1 parent
d5d81db
commit 8f63450
Showing
8 changed files
with
125 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Service; | ||
|
||
use Kiener\MolliePayments\Struct\MollieApi\ShipmentTrackingInfoStruct; | ||
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity; | ||
|
||
class TrackingInfoStructFactory | ||
{ | ||
public function createFromDelivery(OrderDeliveryEntity $orderDeliveryEntity):?ShipmentTrackingInfoStruct | ||
{ | ||
$trackingCodes = $orderDeliveryEntity->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MolliePayments\Tests\Service; | ||
|
||
use Kiener\MolliePayments\Service\TrackingInfoStructFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Kiener\MolliePayments\Service\TrackingInfoStructFactory | ||
*/ | ||
class TrackingInfoStructFactoryTest extends TestCase | ||
{ | ||
/** | ||
* @var TrackingInfoStructFactory | ||
*/ | ||
private $factory; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->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'], | ||
['some<code'], | ||
['some>code'], | ||
['some#code'], | ||
]; | ||
} | ||
} |