Skip to content

Commit

Permalink
Move product option value translations logic to trait (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Nov 17, 2023
1 parent 14625a9 commit 649e4db
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 36 deletions.
45 changes: 27 additions & 18 deletions src/AttributeOptions/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,27 +247,10 @@ private function importOptionValues(string $attributeCode, ProductOptionInterfac
}

// We can assume that if we are here is because the option repository has been injected, so event these services should be!
$translationLocaleProvider = $this->translationLocaleProvider;
Assert::isInstanceOf($translationLocaleProvider, TranslationLocaleProviderInterface::class);
$definedLocalesCodes = $translationLocaleProvider->getDefinedLocalesCodes();

$productOptionValueTranslationFactory = $this->productOptionValueTranslationFactory;
Assert::isInstanceOf($productOptionValueTranslationFactory, FactoryInterface::class);

foreach ($attributeOption['labels'] as $localeCode => $label) {
if (!in_array($localeCode, $definedLocalesCodes, true)) {
continue;
}
$optionValueTranslation = $optionValue->getTranslation($localeCode);
if ($optionValueTranslation->getLocale() !== $localeCode) {
$optionValueTranslation = $productOptionValueTranslationFactory->createNew();
$optionValueTranslation->setLocale($localeCode);
}
$optionValueTranslation->setValue($label ?? $optionValue->getCode());
if (!$optionValue->hasTranslation($optionValueTranslation)) {
$optionValue->addTranslation($optionValueTranslation);
}
}
$this->importProductOptionValueTranslations($attributeOption, $optionValue);
}
}

Expand Down Expand Up @@ -320,4 +303,30 @@ private function updateProductOption(ProductOptionInterface $productOption): voi
$productOption->addTranslation($newProductOptionTranslation);
}
}

/**
* This method should be called only if the productOptionRepository is injected, so we can assume
* that this factory is injected too.
*/
private function getDefinedLocaleCodes(): array
{
$translationLocaleProvider = $this->translationLocaleProvider;
Assert::isInstanceOf($translationLocaleProvider, TranslationLocaleProviderInterface::class);

return $translationLocaleProvider->getDefinedLocalesCodes();
}

/**
* This method should be called only if the productOptionRepository is injected, so we can assume
* that this factory is injected too.
*
* @return FactoryInterface<ProductOptionValueTranslationInterface>
*/
private function getProductOptionValueTranslationFactory(): FactoryInterface
{
$productOptionValueTranslationFactory = $this->productOptionValueTranslationFactory;
Assert::isInstanceOf($productOptionValueTranslationFactory, FactoryInterface::class);

return $productOptionValueTranslationFactory;
}
}
40 changes: 40 additions & 0 deletions src/ProductOptionHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@

namespace Webgriffe\SyliusAkeneoPlugin;

use Sylius\Component\Product\Model\ProductOptionValueInterface;
use Sylius\Component\Product\Model\ProductOptionValueTranslationInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

/**
* @psalm-type AkeneoAttributeOption array{_links: array, code: string, attribute: string, sort_order: int, labels: array<string, ?string>}
*/
trait ProductOptionHelperTrait
{
/**
* @return string[]
*/
abstract private function getDefinedLocaleCodes(): array;

/**
* @return FactoryInterface<ProductOptionValueTranslationInterface>
*/
abstract private function getProductOptionValueTranslationFactory(): FactoryInterface;

protected function getSyliusProductOptionValueCode(string ...$pieces): string
{
$slugifiedPieces = array_map(static function (string $word): string {
Expand All @@ -14,4 +31,27 @@ protected function getSyliusProductOptionValueCode(string ...$pieces): string

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

/**
* @param AkeneoAttributeOption $akeneoAttributeOption
*/
protected function importProductOptionValueTranslations(array $akeneoAttributeOption, ProductOptionValueInterface $optionValue): void
{
$productOptionValueTranslationFactory = $this->getProductOptionValueTranslationFactory();

foreach ($akeneoAttributeOption['labels'] as $localeCode => $label) {
if (!in_array($localeCode, $this->getDefinedLocaleCodes(), true)) {
continue;
}
$productOptionValueTranslation = $optionValue->getTranslation($localeCode);
if ($productOptionValueTranslation->getLocale() !== $localeCode) {
$productOptionValueTranslation = $productOptionValueTranslationFactory->createNew();
$productOptionValueTranslation->setLocale($localeCode);
}
$productOptionValueTranslation->setValue($label ?? $optionValue->getCode());
if (!$optionValue->hasTranslation($productOptionValueTranslation)) {
$optionValue->addTranslation($productOptionValueTranslation);
}
}
}
}
37 changes: 19 additions & 18 deletions src/ValueHandler/ProductOptionValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
use Webgriffe\SyliusAkeneoPlugin\ValueHandlerInterface;
use Webmozart\Assert\Assert;

/**
* @psalm-type AkeneoAttributeOption array{_links: array, code: string, attribute: string, sort_order: int, labels: array<string, ?string>}
*/
final class ProductOptionValueHandler implements ValueHandlerInterface
{
use ProductOptionHelperTrait;
Expand Down Expand Up @@ -136,6 +139,7 @@ private function handleSelectOption(ProductOptionInterface $productOption, strin
$optionValue = $this->getOrCreateProductOptionValue($optionValueCode, $productOption);

try {
/** @var AkeneoAttributeOption $akeneoAttributeOption */
$akeneoAttributeOption = $this->apiClient->getAttributeOptionApi()->get($optionCode, $akeneoValue);
} catch (HttpException $e) {
$response = $e->getResponse();
Expand All @@ -154,24 +158,8 @@ private function handleSelectOption(ProductOptionInterface $productOption, strin

throw $e;
}
/**
* @var string $localeCode
* @var ?string $label
*/
foreach ($akeneoAttributeOption['labels'] as $localeCode => $label) {
if (!in_array($localeCode, $this->translationLocaleProvider->getDefinedLocalesCodes(), true)) {
continue;
}
$optionValueTranslation = $optionValue->getTranslation($localeCode);
if ($optionValueTranslation->getLocale() !== $localeCode) {
$optionValueTranslation = $this->productOptionValueTranslationFactory->createNew();
$optionValueTranslation->setLocale($localeCode);
}
$optionValueTranslation->setValue($label ?? $akeneoValue);
if (!$optionValue->hasTranslation($optionValueTranslation)) {
$optionValue->addTranslation($optionValueTranslation);
}
}

$this->importProductOptionValueTranslations($akeneoAttributeOption, $optionValue);
if (!$productVariant->hasOptionValue($optionValue)) {
$productVariant->addOptionValue($optionValue);
}
Expand Down Expand Up @@ -292,4 +280,17 @@ private function isVariantOption(ProductVariantInterface $productVariant, string

return false;
}

private function getDefinedLocaleCodes(): array
{
return $this->translationLocaleProvider->getDefinedLocalesCodes();
}

/**
* @return FactoryInterface<ProductOptionValueTranslationInterface>
*/
private function getProductOptionValueTranslationFactory(): FactoryInterface
{
return $this->productOptionValueTranslationFactory;
}
}

0 comments on commit 649e4db

Please sign in to comment.