Skip to content

Commit

Permalink
Move product option translation factory logic to trait (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Nov 20, 2023
1 parent e7d4b30 commit d50a250
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/AttributeOptions/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,20 @@ private function getDefinedLocaleCodes(): array
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<ProductOptionTranslationInterface>
*/
private function getProductOptionTranslationFactory(): FactoryInterface
{
$productOptionTranslationFactory = $this->productOptionTranslationFactory;
Assert::isInstanceOf($productOptionTranslationFactory, FactoryInterface::class);

return $productOptionTranslationFactory;
}

/**
* This method should be called only if the productOptionRepository is injected, so we can assume
* that this factory is injected too.
Expand Down
51 changes: 38 additions & 13 deletions src/Product/ProductOptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
use RuntimeException;
use Sylius\Component\Product\Model\ProductOptionInterface;
use Sylius\Component\Product\Model\ProductOptionTranslationInterface;
use Sylius\Component\Product\Model\ProductOptionValueInterface;
use Sylius\Component\Product\Model\ProductOptionValueTranslationInterface;
use Sylius\Component\Product\Repository\ProductOptionRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Webgriffe\SyliusAkeneoPlugin\ProductOptionHelperTrait;

/**
* @psalm-type AkeneoAttribute array{code: string, type: string, labels: array<string, ?string>}
* @psalm-type AkeneoFamilyVariant array{code: string, labels: array<string, ?string>, variant_attribute_sets: list<array{level: int, axes: list<string>, attributes: list<string>}>}
*/
final class ProductOptionsResolver implements ProductOptionsResolverInterface
{
use ProductOptionHelperTrait;

/**
* @param FactoryInterface<ProductOptionInterface> $productOptionFactory
* @param FactoryInterface<ProductOptionTranslationInterface> $productOptionTranslationFactory
Expand Down Expand Up @@ -87,7 +93,8 @@ public function resolve(array $akeneoProduct): array
$productOption->setPosition($position);

try {
$attributeResponse = $this->apiClient->getAttributeApi()->get($attributeCode);
/** @var AkeneoAttribute $akeneoAttribute */
$akeneoAttribute = $this->apiClient->getAttributeApi()->get($attributeCode);
} catch (HttpException $e) {
if ($e->getResponse()->getStatusCode() === 404) {
throw new RuntimeException(
Expand All @@ -102,22 +109,40 @@ public function resolve(array $akeneoProduct): array

throw $e;
}
foreach ($attributeResponse['labels'] as $locale => $label) {
$productOptionTranslation = $productOption->getTranslation($locale);
if ($productOptionTranslation->getLocale() === $locale) {
$productOptionTranslation->setName($label);

continue;
}
$newProductOptionTranslation = $this->productOptionTranslationFactory->createNew();
$newProductOptionTranslation->setLocale($locale);
$newProductOptionTranslation->setName($label);
$productOption->addTranslation($newProductOptionTranslation);
}
$this->importProductOptionTranslations($akeneoAttribute, $productOption);
$this->productOptionRepository->add($productOption);
$productOptions[] = $productOption;
}

return $productOptions;
}

private function getDefinedLocaleCodes(): array
{
throw new RuntimeException('This method should not be invoked in this context.');
}

/**
* @return FactoryInterface<ProductOptionTranslationInterface>
*/
private function getProductOptionTranslationFactory(): FactoryInterface
{
return $this->productOptionTranslationFactory;
}

/**
* @return FactoryInterface<ProductOptionValueTranslationInterface>
*/
private function getProductOptionValueTranslationFactory(): FactoryInterface
{
throw new RuntimeException('This method should not be invoked in this context.');
}

/**
* @return FactoryInterface<ProductOptionValueInterface>
*/
private function getProductOptionValueFactory(): FactoryInterface
{
throw new RuntimeException('This method should not be invoked in this context.');
}
}
26 changes: 26 additions & 0 deletions src/ProductOptionHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
namespace Webgriffe\SyliusAkeneoPlugin;

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

/**
* @psalm-type AkeneoAttribute array{code: string, type: string, labels: array<string, ?string>}
* @psalm-type AkeneoAttributeOption array{_links: array, code: string, attribute: string, sort_order: int, labels: array<string, ?string>}
*/
trait ProductOptionHelperTrait
Expand All @@ -19,6 +21,11 @@ trait ProductOptionHelperTrait
*/
abstract private function getDefinedLocaleCodes(): array;

/**
* @return FactoryInterface<ProductOptionTranslationInterface>
*/
abstract private function getProductOptionTranslationFactory(): FactoryInterface;

/**
* @return FactoryInterface<ProductOptionValueTranslationInterface>
*/
Expand Down Expand Up @@ -72,4 +79,23 @@ private function createNewOptionValue(

return $optionValue;
}

/**
* @param AkeneoAttribute $akeneoAttribute
*/
protected function importProductOptionTranslations(array $akeneoAttribute, ProductOptionInterface $productOption): void
{
foreach ($akeneoAttribute['labels'] as $locale => $label) {
$productOptionTranslation = $productOption->getTranslation($locale);
if ($productOptionTranslation->getLocale() === $locale) {
$productOptionTranslation->setName($label);

continue;
}
$newProductOptionTranslation = $this->getProductOptionTranslationFactory()->createNew();
$newProductOptionTranslation->setLocale($locale);
$newProductOptionTranslation->setName($label);
$productOption->addTranslation($newProductOptionTranslation);
}
}
}
9 changes: 9 additions & 0 deletions src/ValueHandler/ProductOptionValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Product\Model\ProductOptionInterface;
use Sylius\Component\Product\Model\ProductOptionTranslationInterface;
use Sylius\Component\Product\Model\ProductOptionValueInterface;
use Sylius\Component\Product\Model\ProductOptionValueTranslationInterface;
use Sylius\Component\Product\Repository\ProductOptionRepositoryInterface;
Expand Down Expand Up @@ -298,4 +299,12 @@ private function getProductOptionValueFactory(): FactoryInterface
{
return $this->productOptionValueFactory;
}

/**
* @return FactoryInterface<ProductOptionTranslationInterface>
*/
private function getProductOptionTranslationFactory(): FactoryInterface
{
throw new RuntimeException('This method should not be invoked in this context.');
}
}

0 comments on commit d50a250

Please sign in to comment.