Skip to content

Commit

Permalink
Introduce Product Option Helper trait and move code resolver method t…
Browse files Browse the repository at this point in the history
…o it (#187)
  • Loading branch information
lruozzi9 committed Nov 17, 2023
1 parent 7f54da7 commit 14625a9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/AttributeOptions/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Webgriffe\SyliusAkeneoPlugin\Event\IdentifiersModifiedSinceSearchBuilderBuiltEvent;
use Webgriffe\SyliusAkeneoPlugin\ImporterInterface;
use Webgriffe\SyliusAkeneoPlugin\ProductOptionHelperTrait;
use Webmozart\Assert\Assert;

/**
Expand All @@ -29,6 +30,8 @@
*/
final class Importer implements ImporterInterface
{
use ProductOptionHelperTrait;

private const SIMPLESELECT_TYPE = 'pim_catalog_simpleselect';

private const MULTISELECT_TYPE = 'pim_catalog_multiselect';
Expand Down Expand Up @@ -224,7 +227,7 @@ private function importOptionValues(string $attributeCode, ProductOptionInterfac
$attributeOptions = $this->getSortedAkeneoAttributeOptionsByAttributeCode($attributeCode);

foreach ($attributeOptions as $attributeOption) {
$optionValueCode = $attributeCode . '_' . $attributeOption['code'];
$optionValueCode = $this->getSyliusProductOptionValueCode($attributeCode, $attributeOption['code']);
$optionValue = null;
foreach ($option->getValues() as $value) {
if ($value->getCode() === $optionValueCode) {
Expand Down
17 changes: 17 additions & 0 deletions src/ProductOptionHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusAkeneoPlugin;

trait ProductOptionHelperTrait
{
protected function getSyliusProductOptionValueCode(string ...$pieces): string
{
$slugifiedPieces = array_map(static function (string $word): string {
return str_replace(['.', ','], '', $word);
}, $pieces);

return implode('_', $slugifiedPieces);
}
}
18 changes: 6 additions & 12 deletions src/ValueHandler/ProductOptionValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webgriffe\SyliusAkeneoPlugin\ProductOptionHelperTrait;
use Webgriffe\SyliusAkeneoPlugin\ValueHandlerInterface;
use Webmozart\Assert\Assert;

final class ProductOptionValueHandler implements ValueHandlerInterface
{
use ProductOptionHelperTrait;

/**
* @param FactoryInterface<ProductOptionValueInterface> $productOptionValueFactory
* @param FactoryInterface<ProductOptionValueTranslationInterface> $productOptionValueTranslationFactory
Expand Down Expand Up @@ -128,7 +131,7 @@ public function handle($productVariant, string $optionCode, array $akeneoValue):

private function handleSelectOption(ProductOptionInterface $productOption, string $optionCode, string $akeneoValue, ProductInterface $product, ProductVariantInterface $productVariant): void
{
$optionValueCode = $this->createOptionValueCode($optionCode, $akeneoValue);
$optionValueCode = $this->getSyliusProductOptionValueCode($optionCode, $akeneoValue);

$optionValue = $this->getOrCreateProductOptionValue($optionValueCode, $productOption);

Expand Down Expand Up @@ -184,7 +187,7 @@ private function handleMetricOption(ProductOptionInterface $productOption, strin
throw new LogicException('Unit key not found');
}
$unit = (string) $akeneoDataValue['unit'];
$optionValueCode = $this->createOptionValueCode($optionCode, $floatAmount, $unit);
$optionValueCode = $this->getSyliusProductOptionValueCode($optionCode, $floatAmount, $unit);

$optionValue = $this->getOrCreateProductOptionValue($optionValueCode, $productOption);

Expand All @@ -202,7 +205,7 @@ private function handleMetricOption(ProductOptionInterface $productOption, strin

private function handleBooleanOption(ProductOptionInterface $productOption, string $optionCode, bool $akeneoDataValue, ProductVariantInterface $productVariant): void
{
$optionValueCode = $this->createOptionValueCode($optionCode, (string) $akeneoDataValue);
$optionValueCode = $this->getSyliusProductOptionValueCode($optionCode, (string) $akeneoDataValue);

$optionValue = $this->getOrCreateProductOptionValue($optionValueCode, $productOption);

Expand Down Expand Up @@ -277,15 +280,6 @@ private function addOptionValueTranslation(
return $optionValue;
}

private function createOptionValueCode(string ...$pieces): string
{
$slugifiedPieces = array_map(static function (string $word): string {
return str_replace(['.', ','], '', $word);
}, $pieces);

return implode('_', $slugifiedPieces);
}

private function isVariantOption(ProductVariantInterface $productVariant, string $attribute): bool
{
$product = $productVariant->getProduct();
Expand Down

0 comments on commit 14625a9

Please sign in to comment.