Skip to content

Commit

Permalink
PISHPS-351: Extended logging for TrackingInfoStructFactory (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muxfeld-diw authored Sep 9, 2024
1 parent 24ba49d commit 4a8bfb0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@

<service id="Kiener\MolliePayments\Service\TrackingInfoStructFactory">
<argument type="service" id="Kiener\MolliePayments\Service\UrlParsingService"/>
<argument type="service" id="mollie_payments.logger"/>
</service>


Expand Down
36 changes: 34 additions & 2 deletions src/Service/TrackingInfoStructFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Kiener\MolliePayments\Components\ShipmentManager\Exceptions\NoDeliveriesFoundException;
use Kiener\MolliePayments\Struct\MollieApi\ShipmentTrackingInfoStruct;
use Kiener\MolliePayments\Traits\StringTrait;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryCollection;
use Shopware\Core\Checkout\Order\OrderEntity;

Expand All @@ -18,9 +19,17 @@ class TrackingInfoStructFactory
*/
private $urlParsingService;

public function __construct(UrlParsingService $urlParsingService)
{
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(
UrlParsingService $urlParsingService,
LoggerInterface $logger
) {
$this->urlParsingService = $urlParsingService;
$this->logger = $logger;
}


Expand Down Expand Up @@ -63,6 +72,8 @@ public function trackingFromOrder(OrderEntity $order): ?ShipmentTrackingInfoStru
return null;
}

$this->logger->info(sprintf('Creating tracking information for shipment with shipping method %s. Order: "%s"', $shippingMethod->getName(), $order->getOrderNumber()));

return $this->createInfoStruct(
(string)$shippingMethod->getName(),
$trackingCodes[0],
Expand Down Expand Up @@ -90,6 +101,7 @@ public function create(string $trackingCarrier, string $trackingCode, string $tr
private function createInfoStruct(string $trackingCarrier, string $trackingCode, string $trackingUrl): ?ShipmentTrackingInfoStruct
{
if (empty($trackingCarrier) && empty($trackingCode)) {
$this->logger->info('No tracking information provided for shipment.');
return null;
}

Expand All @@ -101,22 +113,42 @@ private function createInfoStruct(string $trackingCarrier, string $trackingCode,
throw new \InvalidArgumentException('Missing Argument for Tracking Code!');
}

$this->logger->info('Creating tracking information for shipment.', [
'trackingCarrier' => $trackingCarrier,
'trackingCode' => $trackingCode,
'trackingUrl' => $trackingUrl
]);

// determine if the provided tracking code is actually a tracking URL
if (empty($trackingUrl) === true || $this->urlParsingService->isUrl($trackingCode)) {
$this->logger->info('Tracking code is a URL, parsing tracking code from URL.', [
'trackingCode' => $trackingCode,
'trackingUrl' => $trackingUrl
]);

[$trackingCode, $trackingUrl] = $this->urlParsingService->parseTrackingCodeFromUrl($trackingCode);

$this->logger->info('Parsed tracking code from URL.', [
'trackingCode' => $trackingCode,
'trackingUrl' => $trackingUrl
]);
}

# 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) {
$this->logger->info('Tracking code is too long, truncating.', ['trackingCode' => $trackingCode]);
if (strpos($trackingCode, ',') !== false) {
$trackingCode = trim(explode(',', $trackingCode)[0]);
} elseif (strpos($trackingCode, ';') !== false) {
$trackingCode = trim(explode(';', $trackingCode)[0]);
}

$this->logger->info('Truncated tracking code.', ['trackingCode' => $trackingCode]);

# if we are still too long, then simply remove the code
if (mb_strlen($trackingCode) > self::MAX_TRACKING_CODE_LENGTH) {
$this->logger->info('Tracking code is still too long, removing.', ['trackingCode' => $trackingCode]);
return new ShipmentTrackingInfoStruct($trackingCarrier, '', '');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function setUp(): void
$orderService,
$deliveryExtractor,
new OrderItemsExtractor(),
new TrackingInfoStructFactory(new UrlParsingService())
new TrackingInfoStructFactory(new UrlParsingService(), new NullLogger())
);

$this->context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
Expand Down
3 changes: 2 additions & 1 deletion tests/PHPUnit/Service/TrackingInfoStructFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Kiener\MolliePayments\Service\TrackingInfoStructFactory;
use Kiener\MolliePayments\Service\UrlParsingService;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryCollection;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
Expand All @@ -24,7 +25,7 @@ class TrackingInfoStructFactoryTest extends TestCase

public function setUp(): void
{
$this->factory = new TrackingInfoStructFactory(new UrlParsingService());
$this->factory = new TrackingInfoStructFactory(new UrlParsingService(), new NullLogger());
}


Expand Down

0 comments on commit 4a8bfb0

Please sign in to comment.