Skip to content

Commit

Permalink
MOL-1196: extract to factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Nov 27, 2023
1 parent d5d81db commit 8f63450
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 40 deletions.
51 changes: 12 additions & 39 deletions src/Facade/MollieShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,11 +63,17 @@ class MollieShipment implements MollieShipmentInterface
*/
private $orderDataExtractor;

/**
* @var TrackingInfoStructFactory
*/
private $trackingInfoStructFactory;

/**
* @var LoggerInterface
*/
private $logger;


/**
* @param MolliePaymentExtractor $extractor
* @param DeliveryTransitionServiceInterface $deliveryTransitionService
Expand All @@ -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;
Expand All @@ -87,6 +94,7 @@ public function __construct(MolliePaymentExtractor $extractor, DeliveryTransitio
$this->orderService = $orderService;
$this->orderDataExtractor = $orderDataExtractor;
$this->logger = $logger;
$this->trackingInfoStructFactory = $trackingInfoStructFactory;
}

/**
Expand Down Expand Up @@ -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',
Expand All @@ -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());

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@
<argument key="$envAppUrl">%env(default::APP_URL)%</argument>
</service>

<service id="Kiener\MolliePayments\Service\TrackingInfoStructFactory"/>


</services>

</container>
2 changes: 2 additions & 0 deletions src/Resources/config/services/facades.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
<argument type="service" id="Kiener\MolliePayments\Service\OrderDeliveryService"/>
<argument type="service" id="Kiener\MolliePayments\Service\OrderService"/>
<argument type="service" id="Kiener\MolliePayments\Service\MollieApi\OrderDataExtractor"/>
<argument type="service" id="Kiener\MolliePayments\Service\TrackingInfoStructFactory"/>
<argument type="service" id="mollie_payments.logger"/>
</service>


<service id="Kiener\MolliePayments\Facade\MolliePaymentDoPay" public="true">
<argument type="service" id="Kiener\MolliePayments\Service\MollieApi\OrderDataExtractor"/>
<argument type="service" id="Kiener\MolliePayments\Service\MollieApi\Builder\MollieOrderBuilder"/>
Expand Down
2 changes: 1 addition & 1 deletion src/Service/MollieApi/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
54 changes: 54 additions & 0 deletions src/Service/TrackingInfoStructFactory.php
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,6 +72,7 @@ public function setUp(): void
'extractDelivery' => $this->delivery
]);


$this->shipmentFacade = new MollieShipment(
$this->createMock(MolliePaymentExtractor::class),
$this->createMock(DeliveryTransitionService::class),
Expand All @@ -79,6 +81,7 @@ public function setUp(): void
$this->createMock(OrderDeliveryService::class),
$this->orderService,
$this->orderDataExtractor,
new TrackingInfoStructFactory(),
new NullLogger(),
);

Expand Down
4 changes: 4 additions & 0 deletions tests/PHPUnit/Facade/MollieShipment/SetShipmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,6 +101,8 @@ public function setup(): void
$this->createMock(CustomerService::class)
);

$trackingStructFactory = new TrackingInfoStructFactory();

$this->mollieShipment = new MollieShipment(
$this->extractor,
$this->deliveryTransitionService,
Expand All @@ -108,6 +111,7 @@ public function setup(): void
$this->orderDeliveryService,
$this->orderService,
$this->orderDataExtractor,
$trackingStructFactory,
$this->logger
);
$this->orderNumber = 'fooOrderNumber';
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPUnit/Service/TrackingInfoStructFactoryTest.php
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'],
];
}
}

0 comments on commit 8f63450

Please sign in to comment.