Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 98 additions & 6 deletions src/Packetery/Module/Shipping/ShippingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@

class ShippingProvider {

/**
* Cache pro dopravce
*
* @var array<string, Carrier|null>
*/
private static array $carrierCache = [];

/**
* @var FeatureFlagProvider
*/
Expand Down Expand Up @@ -225,18 +232,103 @@
*
* @return array<string, string>
*/
public function sortMethods( array $methods ): array {
public function sortMethods(array $methods): array {
// Získání všech carrier ID z metod
$carrierIds = [];
foreach ($methods as $fullyQualifiedClassname => $methodTitle) {
if (defined("$fullyQualifiedClassname::CARRIER_ID")) {
$carrierIds[] = $fullyQualifiedClassname::CARRIER_ID;
}
}

// Předběžné načtení všech dopravců najednou
if (!empty($carrierIds)) {

Check failure on line 245 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 245 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.2)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 245 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.4)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 245 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.3)

Construct empty() is not allowed. Use more strict comparison.
$this->preloadCarriers($carrierIds);
}

uasort(
$methods,
function ( $classA, $classB ) {
$objectA = new $classA();
$objectB = new $classB();
function ($fullyQualifiedClassnameA, $fullyQualifiedClassnameB) {
$methodTitleA = null;
$methodTitleB = null;

// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
return strcmp( $objectA->method_title, $objectB->method_title );
// Použití cache místo přímého volání repositáře
if (defined("$fullyQualifiedClassnameA::CARRIER_ID")) {
$carrier = $this->getCachedCarrier((int)$fullyQualifiedClassnameA::CARRIER_ID);
if ($carrier !== null) {
$methodTitleA = $carrier->getName();
}
}

if (defined("$fullyQualifiedClassnameB::CARRIER_ID")) {
$carrier = $this->getCachedCarrier((int)$fullyQualifiedClassnameB::CARRIER_ID);
if ($carrier !== null) {
$methodTitleB = $carrier->getName();
}
}

if ($methodTitleA === null || $methodTitleB === null) {
return 0;
}

return strcasecmp($methodTitleA, $methodTitleB);
}
);

return $methods;
}

/**
* Předběžně načte dopravce do cache
*
* @param string[] $carrierIds ID dopravců
* @return void
*/
private function preloadCarriers(array $carrierIds): void {
// Filtrujeme pouze ID, která ještě nemáme v cache
$missingIds = array_filter($carrierIds, function($id) {
return !isset(self::$carrierCache[$id]);
});

if (empty($missingIds)) {

Check failure on line 293 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 293 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.2)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 293 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.4)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 293 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.3)

Construct empty() is not allowed. Use more strict comparison.
return;
}

$inClause = $this->carrierRepository->getWpdbAdapter()->prepareInClause($missingIds);

Check failure on line 297 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().

Check failure on line 297 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.2)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().

Check failure on line 297 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.4)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().

Check failure on line 297 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.3)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().
$query = sprintf(
'SELECT * FROM `%s` WHERE `id` IN (%s)',
$this->carrierRepository->getWpdbAdapter()->packeteryCarrier,

Check failure on line 300 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().

Check failure on line 300 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.2)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().

Check failure on line 300 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.4)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().

Check failure on line 300 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.3)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().
$inClause
);

$results = $this->carrierRepository->getWpdbAdapter()->get_results($query, \ARRAY_A);

Check failure on line 304 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().

Check failure on line 304 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.2)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().

Check failure on line 304 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.4)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().

Check failure on line 304 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.3)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::getWpdbAdapter().

foreach ($results as $carrierData) {
$carrier = $this->carrierRepository->createEntityFromDbResult($carrierData);

Check failure on line 307 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::createEntityFromDbResult().

Check failure on line 307 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.2)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::createEntityFromDbResult().

Check failure on line 307 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.4)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::createEntityFromDbResult().

Check failure on line 307 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.3)

Call to an undefined method Packetery\Module\Carrier\EntityRepository::createEntityFromDbResult().
self::$carrierCache[$carrier->getId()] = $carrier;
}
}

/**
* Získá dopravce z cache nebo z databáze
*
* @param string $carrierId ID dopravce
* @return Carrier|null
*/
private function getCachedCarrier(int $carrierId): ?Carrier {

Check failure on line 318 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

PHPDoc tag @param for parameter $carrierId with type string is incompatible with native type int.

Check failure on line 318 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.2)

PHPDoc tag @param for parameter $carrierId with type string is incompatible with native type int.

Check failure on line 318 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.4)

PHPDoc tag @param for parameter $carrierId with type string is incompatible with native type int.

Check failure on line 318 in src/Packetery/Module/Shipping/ShippingProvider.php

View workflow job for this annotation

GitHub Actions / phpstan (8.3)

PHPDoc tag @param for parameter $carrierId with type string is incompatible with native type int.
if (!isset(self::$carrierCache[$carrierId])) {
self::$carrierCache[$carrierId] = $this->carrierRepository->getById($carrierId);
}

return self::$carrierCache[$carrierId];
}

/**
* Vyčistí cache dopravců
*
* @return void
*/
public static function clearCarrierCache(): void {
self::$carrierCache = [];
}
}
Loading