Skip to content

Commit

Permalink
fix: auto-translate older carrier ids to the current ones (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen authored and EdieLemoine committed Nov 15, 2021
1 parent 5bdae81 commit 5f9a037
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/Service/CarrierConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,31 @@ class CarrierConfigurationProvider
*/
private static $configuration;

/**
* @var array
*/
private static $legacyCarrierIdMap;

/**
* @var string
*/
private static $table = Table::TABLE_CARRIER_CONFIGURATION;

/**
* @param int $carrierId
* @param string $name
* @param null $default
* @param int $carrierId
* @param string $name
* @param null $default
*
* @return null|mixed
* @throws \PrestaShopDatabaseException
*/
public static function get(int $carrierId, string $name, $default = null)
{
self::fetchLegacyCarrierIdMap();
if (! isset(static::$configuration[$carrierId][$name])) {
$query = (new DbQuery())
->select('name, value')
$query = (new DbQuery())->select('name, value')
->from(self::$table)
->where('id_carrier = ' . $carrierId);
->where('id_carrier = ' . (static::$legacyCarrierIdMap[$carrierId] ?? 0));

$result = Db::getInstance()
->executeS($query);
Expand All @@ -48,9 +53,9 @@ public static function get(int $carrierId, string $name, $default = null)
}

/**
* @param int $carrierId
* @param string $name
* @param string $value
* @param int $carrierId
* @param string $name
* @param string $value
*/
public static function updateValue(int $carrierId, string $name, string $value): void
{
Expand All @@ -61,4 +66,28 @@ public static function updateValue(int $carrierId, string $name, string $value):
'id_carrier = ' . $carrierId . ' AND name = "' . pSQL($name) . '" '
);
}

/**
* @throws \PrestaShopDatabaseException
*/
private static function fetchLegacyCarrierIdMap(): void
{
if (isset(static::$legacyCarrierIdMap)) {
return;
}

$query = new DbQuery();
$query->select('current.id_carrier as current_carrier_id, old.id_carrier as old_carrier_id');
$query->from(Table::withPrefix('carrier'), 'current');
$query->innerJoin(Table::withPrefix('carrier'), 'old', 'old.name = current.name');
$query->where('current.active=1');
$query->where('current.deleted=0');
$query->orderBy('current.id_carrier DESC');

$rows = Db::getInstance(_PS_USE_SQL_SLAVE_)
->executeS($query);
foreach ($rows as $row) {
static::$legacyCarrierIdMap[$row['old_carrier_id']] = $row['current_carrier_id'];
}
}
}

0 comments on commit 5f9a037

Please sign in to comment.