Skip to content

Commit

Permalink
fix(products): fix product settings logic and migration
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 27, 2023
1 parent 3a2cb60 commit 96734a1
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 97 deletions.
23 changes: 4 additions & 19 deletions src/Migration/Pdk/PdkDeliveryOptionsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use MyParcelNL\PrestaShop\Migration\Util\CastValue;
use MyParcelNL\PrestaShop\Migration\Util\DataMigrator;
use MyParcelNL\PrestaShop\Migration\Util\MigratableValue;
use MyParcelNL\PrestaShop\Migration\Util\ToDeliveryTypeName;
use MyParcelNL\PrestaShop\Migration\Util\ToPackageTypeName;
use MyParcelNL\PrestaShop\Migration\Util\TransformValue;
use MyParcelNL\PrestaShop\Repository\PsOrderDataRepository;

Expand Down Expand Up @@ -67,25 +69,8 @@ private function getDeliveryOptionsTransformationMap(): Generator
})
);

yield new MigratableValue(
'deliveryType',
DeliveryOptions::DELIVERY_TYPE,
new TransformValue(function ($value) {
return in_array($value, DeliveryOptions::DELIVERY_TYPES_NAMES, true)
? $value
: DeliveryOptions::DEFAULT_DELIVERY_TYPE_NAME;
})
);

yield new MigratableValue(
'packageType',
DeliveryOptions::PACKAGE_TYPE,
new TransformValue(function ($value) {
return in_array($value, DeliveryOptions::PACKAGE_TYPES_NAMES, true)
? $value
: DeliveryOptions::DEFAULT_PACKAGE_TYPE_NAME;
})
);
yield new MigratableValue('deliveryType', DeliveryOptions::DELIVERY_TYPE, new ToDeliveryTypeName());
yield new MigratableValue('packageType', DeliveryOptions::PACKAGE_TYPE, new ToPackageTypeName());

yield new MigratableValue(
'date',
Expand Down
149 changes: 113 additions & 36 deletions src/Migration/Pdk/PdkProductSettingsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@

namespace MyParcelNL\PrestaShop\Migration\Pdk;

use Generator;
use MyParcelNL\Pdk\Base\Service\CountryCodes;
use MyParcelNL\Pdk\Facade\Logger;
use MyParcelNL\Pdk\Settings\Model\ProductSettings;
use MyParcelNL\Pdk\Types\Service\TriStateService;
use MyParcelNL\PrestaShop\Facade\EntityManager;
use MyParcelNL\PrestaShop\Migration\AbstractLegacyPsMigration;
use MyParcelNL\PrestaShop\Migration\Util\DataMigrator;
use MyParcelNL\PrestaShop\Migration\Util\MigratableValue;
use MyParcelNL\PrestaShop\Migration\Util\ToPackageTypeName;
use MyParcelNL\PrestaShop\Migration\Util\ToTriStateValue;
use MyParcelNL\PrestaShop\Migration\Util\TransformValue;
use MyParcelNL\PrestaShop\Repository\PsProductSettingsRepository;
use MyParcelNL\Sdk\src\Support\Collection;

final class PdkProductSettingsMigration extends AbstractPsPdkMigration
{
private const LEGACY_PRODUCT_SETTINGS_MAP = [
'MYPARCELNL_PACKAGE_TYPE' => 'packageType',
'MYPARCELNL_CUSTOMS_ORIGIN' => 'countryOfOrigin',
'MYPARCELNL_CUSTOMS_CODE' => 'customsCode',
'MYPARCELNL_INSURANCE' => 'exportInsurance',
'MYPARCELNL_SIGNATURE_REQUIRED' => 'exportSignature',
'MYPARCELNL_RETURN_PACKAGE' => 'exportReturn',
'MYPARCELNL_PACKAGE_FORMAT' => 'exportLargeFormat',
'MYPARCELNL_ONLY_RECIPIENT' => 'exportOnlyRecipient',
'MYPARCELNL_AGE_CHECK' => 'exportAgeCheck',
];

/**
* @var \MyParcelNL\PrestaShop\Repository\PsProductSettingsRepository
*/
Expand All @@ -45,7 +44,6 @@ public function __construct(PsProductSettingsRepository $productSettingsReposito

/**
* @return void
* @throws \Doctrine\ORM\ORMException
* @throws \PrestaShopDatabaseException
*/
public function up(): void
Expand All @@ -54,36 +52,115 @@ public function up(): void
}

/**
* @throws \PrestaShopDatabaseException
* @return \Generator<\MyParcelNL\PrestaShop\Migration\Util\MigratableValue>
*/
private function getProductSettingsTransformationMap(): Generator
{
yield new MigratableValue(
'MYPARCELNL_PACKAGE_TYPE',
ProductSettings::PACKAGE_TYPE,
new ToPackageTypeName(TriStateService::INHERIT)
);

yield new MigratableValue(
'MYPARCELNL_AGE_CHECK',
ProductSettings::EXPORT_AGE_CHECK,
new ToTriStateValue()
);

yield new MigratableValue(
'MYPARCELNL_RECIPIENT_ONLY',
ProductSettings::EXPORT_ONLY_RECIPIENT,
new ToTriStateValue()
);

yield new MigratableValue(
'MYPARCELNL_RETURN_PACKAGE',
ProductSettings::EXPORT_RETURN,
new ToTriStateValue()
);

yield new MigratableValue(
'MYPARCELNL_SIGNATURE_REQUIRED',
ProductSettings::EXPORT_SIGNATURE,
new ToTriStateValue()
);

yield new MigratableValue(
'MYPARCELNL_INSURANCE',
ProductSettings::EXPORT_INSURANCE,
new ToTriStateValue()
);

yield new MigratableValue(
'MYPARCELNL_PACKAGE_FORMAT',
ProductSettings::EXPORT_LARGE_FORMAT,
new TransformValue(function ($value) {
switch ((int) $value) {
case 1:
return TriStateService::DISABLED;

case 2:
return TriStateService::ENABLED;

default:
return TriStateService::INHERIT;
}
})
);

yield new MigratableValue(
'MYPARCELNL_CUSTOMS_CODE',
ProductSettings::CUSTOMS_CODE,
new ToTriStateValue(TriStateService::TYPE_STRING)
);

yield new MigratableValue(
'MYPARCELNL_CUSTOMS_ORIGIN',
ProductSettings::COUNTRY_OF_ORIGIN,
new TransformValue(function ($value) {
// Assume the user did not intend to set Afghanistan as the country of origin. It's the default value
// that is saved whenever the user saves any product, even without opening the module settings.
if (CountryCodes::CC_AF === $value) {
return TriStateService::INHERIT;
}

return in_array($value, CountryCodes::ALL, true)
? $value
: TriStateService::INHERIT;
})
);
}

/**
* @return void
* @throws \Doctrine\ORM\ORMException
* @throws \PrestaShopDatabaseException
*/
private function migrateProductSettings(): void
{
$oldProductSettings = $this->getAllRows(AbstractLegacyPsMigration::LEGACY_TABLE_PRODUCT_CONFIGURATION);
$allRows = $this->getAllRows(AbstractLegacyPsMigration::LEGACY_TABLE_PRODUCT_CONFIGURATION);
$rowsByProduct = $allRows->groupBy('id_product');

$productsWithSettings = [];
$rowsByProduct->each(function (Collection $rows, int $productId) {
if ($this->productSettingsRepository->findOneBy(['productId' => $productId])) {
Logger::info("Product settings for product $productId already exist, skipping");

foreach ($oldProductSettings as $oldProductSetting) {
if (! array_key_exists($oldProductSetting['name'], self::LEGACY_PRODUCT_SETTINGS_MAP)) {
continue;
return;
}

$productsWithSettings[$oldProductSetting['id_product']][self::LEGACY_PRODUCT_SETTINGS_MAP[$oldProductSetting['name']]] =
$oldProductSetting['value'];
}

foreach ($productsWithSettings as $productId => $productSettings) {
$this->productSettingsRepository->updateOrCreate(
[
'productId' => (int) $productId,
],
[
'data' => json_encode([
'id' => 'product',
'data' => $productSettings,
]),
]
);
}
$oldSettings = $rows
->pluck('value', 'name')
->toArrayWithoutNull();

$newSettings = $this->valueMigrator->transform($oldSettings, $this->getProductSettingsTransformationMap());

$this->productSettingsRepository->create([
'productId' => $productId,
'data' => json_encode(['settings' => $newSettings]),
]);
});

EntityManager::flush();
}
}
2 changes: 1 addition & 1 deletion src/Migration/Pdk/PdkSettingsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private function getSettingsTransformationMap(): Generator
'MYPARCELNL_LABEL_SIZE',
implode('.', [LabelSettings::ID, LabelSettings::FORMAT]),
new TransformValue(function ($value): string {
return 'A6' === $value ? 'a6' : 'a4';
return 'A6' === $value ? LabelSettings::FORMAT_A6 : LabelSettings::FORMAT_A4;
})
);

Expand Down
28 changes: 16 additions & 12 deletions src/Migration/Util/ToDeliveryTypeName.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Migration\Util;

use MyParcelNL\Pdk\Base\Support\Utils;
use MyParcelNL\Pdk\Shipment\Model\DeliveryOptions;

final class ToDeliveryTypeName extends TransformValue
{
public function __construct()
/**
* @var mixed
*/
private $defaultValue;

/**
* @param mixed $defaultValue
*/
public function __construct($defaultValue = DeliveryOptions::DEFAULT_DELIVERY_TYPE_NAME)
{
parent::__construct([$this, 'convert']);
$this->defaultValue = $defaultValue;
}

/**
Expand All @@ -16,16 +30,6 @@ public function __construct()
*/
protected function convert($value): string
{
if (is_numeric($value)) {
if (in_array((int) $value, DeliveryOptions::DELIVERY_TYPES_IDS, true)) {
return array_flip(DeliveryOptions::DELIVERY_TYPES_NAMES_IDS_MAP)[$value];
}

return DeliveryOptions::DEFAULT_DELIVERY_TYPE_NAME;
}

return in_array($value, DeliveryOptions::DELIVERY_TYPES_NAMES, true)
? $value
: DeliveryOptions::DEFAULT_DELIVERY_TYPE_NAME;
return Utils::convertToName($value, DeliveryOptions::DELIVERY_TYPES_NAMES_IDS_MAP) ?? $this->defaultValue;
}
}
39 changes: 26 additions & 13 deletions src/Migration/Util/ToPackageTypeName.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Migration\Util;

use MyParcelNL\Pdk\Base\Support\Utils;
use MyParcelNL\Pdk\Shipment\Model\DeliveryOptions;

final class ToPackageTypeName extends TransformValue
{
public function __construct()
{
parent::__construct(static function ($value) {
if (is_numeric($value)) {
if (in_array((int) $value, DeliveryOptions::PACKAGE_TYPES_IDS, true)) {
return array_flip(DeliveryOptions::PACKAGE_TYPES_NAMES_IDS_MAP)[$value];
}
/**
* @var mixed
*/
private $defaultValue;

return DeliveryOptions::DEFAULT_PACKAGE_TYPE_NAME;
}
/**
* @param mixed $defaultValue
*/
public function __construct($defaultValue = DeliveryOptions::DEFAULT_PACKAGE_TYPE_NAME)
{
parent::__construct([$this, 'convert']);
$this->defaultValue = $defaultValue;
}

return in_array($value, DeliveryOptions::PACKAGE_TYPES_NAMES, true)
? $value
: DeliveryOptions::DEFAULT_PACKAGE_TYPE_NAME;
});
/**
* @param mixed $value
*
* @return mixed
*/
protected function convert($value)
{
return Utils::convertToName($value, DeliveryOptions::PACKAGE_TYPES_NAMES_IDS_MAP) ?? $this->defaultValue;
}
}

31 changes: 28 additions & 3 deletions src/Migration/Util/ToTriStateValue.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Migration\Util;

use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Types\Contract\TriStateServiceInterface;
use MyParcelNL\Pdk\Types\Service\TriStateService;

final class ToTriStateValue extends TransformValue
{
public function __construct()
/**
* @var string
*/
private $type;

public function __construct(string $type = TriStateService::TYPE_COERCED)
{
parent::__construct([$this, 'convert']);

$this->type = $type;
}

/**
Expand All @@ -16,8 +29,20 @@ public function __construct()
*/
protected function convert($value)
{
$triStateService = Pdk::get(TriStateServiceInterface::class);
/** @var TriStateServiceInterface $service */
$service = Pdk::get(TriStateServiceInterface::class);

switch ($this->type) {
case TriStateService::TYPE_COERCED:
return $service->coerce($value);

case TriStateService::TYPE_STRICT:
return $service->cast($value);

case TriStateService::TYPE_STRING:
return $service->coerceString($value);
}

return $triStateService->cast($value);
return $value;
}
}
2 changes: 1 addition & 1 deletion src/Migration/Util/TransformValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MyParcelNL\PrestaShop\Migration\Util;

final class TransformValue implements ValueModifierInterface
class TransformValue implements ValueModifierInterface
{
/**
* @var callable-string|callable
Expand Down
Loading

0 comments on commit 96734a1

Please sign in to comment.