Skip to content

Commit

Permalink
Handle boolean attribute option values (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed May 23, 2022
1 parent 126111a commit aef822b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
43 changes: 43 additions & 0 deletions spec/ValueHandler/ProductOptionValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,47 @@ public function it_supports_product_option_metrical_value(
$productVariant->addOptionValue($productOptionValue)->shouldHaveBeenCalled();
$productOptionValueTranslationFactory->createNew()->shouldHaveBeenCalledOnce();
}

public function it_supports_product_option_boolean_value(
ProductVariantInterface $productVariant,
ProductOptionValueInterface $productOptionValue,
ProductOptionValueTranslationInterface $englishProductOptionValueTranslation,
ProductOptionValueTranslationInterface $italianProductOptionValueTranslation,
ProductOptionInterface $productOption,
RepositoryInterface $productOptionValueRepository,
ApiClientInterface $apiClient,
FactoryInterface $productOptionValueTranslationFactory,
TranslatorInterface $translator
): void {
$apiClient->findAttribute(self::OPTION_CODE)->willReturn(
[
'code' => self::OPTION_CODE,
'type' => 'pim_catalog_boolean'
]
);
$value = [
[
'scope' => null,
'locale' => null,
'data' => true,
],
];
$translator->trans('sylius.ui.yes_label', [], null, 'en_US')->shouldBeCalledOnce()->willReturn('Yes');
$translator->trans('sylius.ui.yes_label', [], null, 'it_IT')->shouldBeCalledOnce()->willReturn('Si');
$productVariant->hasOptionValue($productOptionValue)->willReturn(false);
$productOptionValueRepository->findOneBy(['code' => 'option-code_1'])->willReturn(null);

$this->handle($productVariant, self::OPTION_CODE, $value);

$productOptionValue->setCode('option-code_1')->shouldHaveBeenCalled();
$productOptionValue->setOption($productOption)->shouldHaveBeenCalled();
$productOption->addValue($productOptionValue)->shouldHaveBeenCalled();
$englishProductOptionValueTranslation->setValue('Yes')->shouldHaveBeenCalled();
$italianProductOptionValueTranslation->setLocale('it_IT')->shouldHaveBeenCalled();
$italianProductOptionValueTranslation->setValue('Si')->shouldHaveBeenCalled();
$productOptionValue->addTranslation($englishProductOptionValueTranslation)->shouldHaveBeenCalled();
$productOptionValue->addTranslation($italianProductOptionValueTranslation)->shouldHaveBeenCalled();
$productVariant->addOptionValue($productOptionValue)->shouldHaveBeenCalled();
$productOptionValueTranslationFactory->createNew()->shouldHaveBeenCalledOnce();
}
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/WebgriffeSyliusAkeneoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ final class WebgriffeSyliusAkeneoExtension extends AbstractResourceExtension imp
'$productOptionValueTranslationFactory' => 'sylius.factory.product_option_value_translation',
'$productOptionValueRepository' => 'sylius.repository.product_option_value',
'$translationLocaleProvider' => 'sylius.translation_locale_provider.admin',
'$translator' => 'translator'
'$translator' => 'translator',
],
],
'translatable_property' => [
Expand Down
45 changes: 45 additions & 0 deletions src/ValueHandler/ProductOptionValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public function handle($productVariant, string $optionCode, array $akeneoValue):
Assert::isArray($akeneoValueData);
$this->handleMetricOption($productOption, $optionCode, $akeneoValueData, $product, $productVariant);

break;
case 'pim_catalog_boolean':
Assert::boolean($akeneoValueData);
$this->handleBooleanOption($productOption, $optionCode, $akeneoValueData, $product, $productVariant);

break;
default:
throw new LogicException(sprintf('The Akeneo attribute type "%s" is not supported from the "%s"', $attributeType, self::class));
Expand Down Expand Up @@ -254,6 +259,46 @@ private function handleMetricOption(ProductOptionInterface $productOption, strin
}
}

private function handleBooleanOption(ProductOptionInterface $productOption, string $optionCode, bool $akeneoDataValue, ProductInterface $product, ProductVariantInterface $productVariant): void
{
$optionValueCode = $optionCode . '_' . $akeneoDataValue;

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

/** @var string[] $locales */
$locales = [];
if ($this->translationLocaleProvider !== null) {
$locales = $this->translationLocaleProvider->getDefinedLocalesCodes();
} else {
foreach ($product->getTranslations() as $translation) {
$locale = $translation->getLocale();
if ($locale === null) {
continue;
}
$locales[] = $locale;
}
}
foreach ($locales as $localeCode) {
$label = (string) $akeneoDataValue;
if ($this->translator !== null) {
$label = $akeneoDataValue ? $this->translator->trans('sylius.ui.yes_label', [], null, $localeCode) : $this->translator->trans('sylius.ui.no_label', [], null, $localeCode);
}
$optionValueTranslation = $optionValue->getTranslation($localeCode);
if ($optionValueTranslation->getLocale() !== $localeCode) {
/** @var ProductOptionValueTranslationInterface $optionValueTranslation */
$optionValueTranslation = $this->productOptionValueTranslationFactory->createNew();
$optionValueTranslation->setLocale($localeCode);
}
$optionValueTranslation->setValue($label);
if (!$optionValue->hasTranslation($optionValueTranslation)) {
$optionValue->addTranslation($optionValueTranslation);
}
}
if (!$productVariant->hasOptionValue($optionValue)) {
$productVariant->addOptionValue($optionValue);
}
}

private function getProductOption(string $optionCode, ProductVariantInterface $productVariant, ProductInterface $product): ProductOptionInterface
{
/** @var ProductOptionInterface|null $productOption */
Expand Down

0 comments on commit aef822b

Please sign in to comment.