Skip to content

Commit

Permalink
Merge pull request #50 from webgriffe/issue-47
Browse files Browse the repository at this point in the history
Skip not defined locales in ProductOptionValueHandler
  • Loading branch information
mmenozzi authored Feb 24, 2021
2 parents 3c31a3b + d6f9d65 commit 50a9583
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 18 deletions.
74 changes: 63 additions & 11 deletions spec/ValueHandler/ProductOptionValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sylius\Component\Product\Repository\ProductOptionRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Webgriffe\SyliusAkeneoPlugin\ApiClientInterface;
use Webgriffe\SyliusAkeneoPlugin\ValueHandler\ProductOptionValueHandler;
use Webgriffe\SyliusAkeneoPlugin\ValueHandlerInterface;
Expand All @@ -40,7 +41,11 @@ function let(
ProductOptionRepositoryInterface $productOptionRepository,
FactoryInterface $productOptionValueFactory,
FactoryInterface $productOptionValueTranslationFactory,
RepositoryInterface $productOptionValueRepository
RepositoryInterface $productOptionValueRepository,
ProductOptionValueInterface $productOptionValue,
ProductOptionValueTranslationInterface $englishProductOptionValueTranslation,
ProductOptionValueTranslationInterface $italianProductOptionValueTranslation,
TranslationLocaleProviderInterface $translationLocaleProvider
) {
$productVariant->getCode()->willReturn(self::VARIANT_CODE);
$productVariant->getProduct()->willReturn($product);
Expand All @@ -58,12 +63,22 @@ function let(
]
);
$productOptionRepository->findOneBy(['code' => self::OPTION_CODE])->willReturn($productOption);
$productOptionValueFactory->createNew()->willReturn($productOptionValue);
$productOptionValue->getTranslation('en_US')->willReturn($englishProductOptionValueTranslation);
$productOptionValue->getTranslation('it_IT')->willReturn($englishProductOptionValueTranslation);
$productOptionValueTranslationFactory->createNew()->willReturn($italianProductOptionValueTranslation);
$productOptionValue->hasTranslation($englishProductOptionValueTranslation)->willReturn(false);
$productOptionValue->hasTranslation($italianProductOptionValueTranslation)->willReturn(false);
$englishProductOptionValueTranslation->getLocale()->willReturn('en_US');
$translationLocaleProvider->getDefinedLocalesCodes()->willReturn(['en_US', 'it_IT']);

$this->beConstructedWith(
$apiClient,
$productOptionRepository,
$productOptionValueFactory,
$productOptionValueTranslationFactory,
$productOptionValueRepository
$productOptionValueRepository,
$translationLocaleProvider
);
}

Expand Down Expand Up @@ -198,8 +213,6 @@ function it_throws_an_exception_if_product_option_does_not_exists_on_sylius(
function it_creates_product_option_value_from_factory_with_all_translations_if_does_not_already_exists(
ProductVariantInterface $productVariant,
ProductOptionValueInterface $productOptionValue,
FactoryInterface $productOptionValueFactory,
FactoryInterface $productOptionValueTranslationFactory,
ProductOptionValueTranslationInterface $englishProductOptionValueTranslation,
ProductOptionValueTranslationInterface $italianProductOptionValueTranslation,
ProductOptionInterface $productOption,
Expand All @@ -212,13 +225,6 @@ function it_creates_product_option_value_from_factory_with_all_translations_if_d
'data' => self::VALUE_CODE,
],
];
$productOptionValueFactory->createNew()->willReturn($productOptionValue);
$englishProductOptionValueTranslation->getLocale()->willReturn('en_US');
$productOptionValue->getTranslation('en_US')->willReturn($englishProductOptionValueTranslation);
$productOptionValue->getTranslation('it_IT')->willReturn($englishProductOptionValueTranslation);
$productOptionValueTranslationFactory->createNew()->willReturn($italianProductOptionValueTranslation);
$productOptionValue->hasTranslation($englishProductOptionValueTranslation)->willReturn(false);
$productOptionValue->hasTranslation($italianProductOptionValueTranslation)->willReturn(false);
$productVariant->hasOptionValue($productOptionValue)->willReturn(false);

$this->handle($productVariant, self::OPTION_CODE, $value);
Expand Down Expand Up @@ -264,4 +270,50 @@ function it_updates_existing_product_option_value_and_all_translations(
$italianProductOptionValue->setValue(self::IT_LABEL)->shouldHaveBeenCalled();
$productOptionValueRepository->add($existentProductOptionValue)->shouldHaveBeenCalled();
}

function it_skips_locale_not_defined_on_sylius(
ProductVariantInterface $productVariant,
ProductOptionValueInterface $productOptionValue,
ProductOptionValueTranslationInterface $englishProductOptionValueTranslation,
ProductOptionValueTranslationInterface $italianProductOptionValueTranslation,
ProductOptionInterface $productOption,
RepositoryInterface $productOptionValueRepository,
ApiClientInterface $apiClient,
FactoryInterface $productOptionValueTranslationFactory
) {
$value = [
[
'scope' => null,
'locale' => null,
'data' => self::VALUE_CODE,
],
];
$productVariant->hasOptionValue($productOptionValue)->willReturn(false);
$apiClient
->findAttributeOption(self::OPTION_CODE, self::VALUE_CODE)
->willReturn(
[
'code' => self::VALUE_CODE,
'attribute' => self::OPTION_CODE,
'sort_order' => 4,
'labels' => ['en_US' => self::EN_LABEL, 'it_IT' => self::IT_LABEL, 'de_DE' => 'German Label'],
]
)
;
$productOptionValue->getTranslation('de_DE')->willReturn($englishProductOptionValueTranslation);

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

$productOptionValue->setCode('option-code_value-code')->shouldHaveBeenCalled();
$productOptionValue->setOption($productOption)->shouldHaveBeenCalled();
$productOption->addValue($productOptionValue)->shouldHaveBeenCalled();
$englishProductOptionValueTranslation->setValue(self::EN_LABEL)->shouldHaveBeenCalled();
$italianProductOptionValueTranslation->setLocale('it_IT')->shouldHaveBeenCalled();
$italianProductOptionValueTranslation->setValue(self::IT_LABEL)->shouldHaveBeenCalled();
$productOptionValue->addTranslation($englishProductOptionValueTranslation)->shouldHaveBeenCalled();
$productOptionValue->addTranslation($italianProductOptionValueTranslation)->shouldHaveBeenCalled();
$productVariant->addOptionValue($productOptionValue)->shouldHaveBeenCalled();
$productOptionValueRepository->add($productOptionValue)->shouldHaveBeenCalled();
$productOptionValueTranslationFactory->createNew()->shouldHaveBeenCalledOnce();
}
}
4 changes: 1 addition & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->addDefaultsIfNotSet()
->children()
->enumNode('type')
->values(
array_keys(WebgriffeSyliusAkeneoExtension::$valueHandlersTypesDefinitions)
)
->values(WebgriffeSyliusAkeneoExtension::getAllowedValueHandlersTypes())
->isRequired()
->end()
->arrayNode('options')
Expand Down
88 changes: 85 additions & 3 deletions src/DependencyInjection/WebgriffeSyliusAkeneoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,80 @@ final class WebgriffeSyliusAkeneoExtension extends AbstractResourceExtension imp

private const IMPORTER_TAG = 'webgriffe_sylius_akeneo.importer';

/** @var array */
/**
* @var array
* @deprecated Do not use anymore. Use $valueHandlersTypesDefinitionsPrivate instead.
*/
public static $valueHandlersTypesDefinitions = [
'channel_pricing' => [
'class' => ChannelPricingValueHandler::class,
'arguments' => [
'sylius.factory.channel_pricing',
'sylius.repository.channel',
'sylius.repository.currency',
],
],
'generic_property' => [
'class' => GenericPropertyValueHandler::class,
'arguments' => [
'property_accessor',
],
],
'image' => [
'class' => ImageValueHandler::class,
'arguments' => [
'sylius.factory.product_image',
'sylius.repository.product_image',
'webgriffe_sylius_akeneo.api_client',
],
],
'immutable_slug' => [
'class' => ImmutableSlugValueHandler::class,
'arguments' => [
'webgriffe_sylius_akeneo.slugify',
'sylius.factory.product_translation',
'sylius.translation_locale_provider.admin',
'sylius.repository.product_translation',
],
],
'product_option' => [
'class' => ProductOptionValueHandler::class,
'arguments' => [
'webgriffe_sylius_akeneo.api_client',
'sylius.repository.product_option',
'sylius.factory.product_option_value',
'sylius.factory.product_option_value_translation',
'sylius.repository.product_option_value'
],
],
'translatable_property' => [
'class' => TranslatablePropertyValueHandler::class,
'arguments' => [
'property_accessor',
'sylius.factory.product_translation',
'sylius.factory.product_variant_translation',
'sylius.translation_locale_provider.admin',
],
],
'generic_attribute' => [
'class' => AttributeValueHandler::class,
'arguments' => [
'sylius.repository.product_attribute',
'sylius.factory.product_attribute_value',
'sylius.translation_locale_provider.admin',
],
],
'file_attribute' => [
'class' => FileAttributeValueHandler::class,
'arguments' => [
'webgriffe_sylius_akeneo.api_client',
'filesystem',
],
],
];

/** @var array<string, array{class: string, arguments: string[]}> */
private static $valueHandlersTypesDefinitionsPrivate = [
'channel_pricing' => [
'class' => ChannelPricingValueHandler::class,
'arguments' => [
Expand Down Expand Up @@ -68,6 +140,7 @@ final class WebgriffeSyliusAkeneoExtension extends AbstractResourceExtension imp
'sylius.factory.product_option_value',
'sylius.factory.product_option_value_translation',
'sylius.repository.product_option_value',
'sylius.translation_locale_provider.admin'
],
],
'translatable_property' => [
Expand Down Expand Up @@ -126,11 +199,20 @@ public function process(ContainerBuilder $container): void
$this->registerTemporaryDirectoryParameter($container);
}

/**
* @return string[]
*/
public static function getAllowedValueHandlersTypes(): array
{
return array_keys(self::$valueHandlersTypesDefinitionsPrivate);
}

private function createValueHandlersDefinitionsAndPriorities(array $valueHandlers): array
{
$definitions = [];
foreach ($valueHandlers as $key => $valueHandler) {
$type = $valueHandler['type'];
Assert::string($type);
$options = $valueHandler['options'] ?? [];
$priority = $valueHandler['priority'] ?? 0;

Expand All @@ -139,12 +221,12 @@ private function createValueHandlersDefinitionsAndPriorities(array $valueHandler
static function (string $argument): Reference {
return new Reference($argument);
},
self::$valueHandlersTypesDefinitions[$type]['arguments']
self::$valueHandlersTypesDefinitionsPrivate[$type]['arguments']
),
array_values($options)
);
$id = sprintf('webgriffe_sylius_akeneo.value_handler.product.%s_value_handler', $key);
$definition = new Definition(self::$valueHandlersTypesDefinitions[$type]['class'], $arguments);
$definition = new Definition(self::$valueHandlersTypesDefinitionsPrivate[$type]['class'], $arguments);
$definition->addTag(self::PRODUCT_VALUE_HANDLER_TAG, ['priority' => $priority]);
$definitions[$id] = $definition;
}
Expand Down
21 changes: 20 additions & 1 deletion src/ValueHandler/ProductOptionValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Sylius\Component\Product\Repository\ProductOptionRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Webgriffe\SyliusAkeneoPlugin\ApiClientInterface;
use Webgriffe\SyliusAkeneoPlugin\ValueHandlerInterface;
use Webmozart\Assert\Assert;
Expand All @@ -33,18 +34,32 @@ final class ProductOptionValueHandler implements ValueHandlerInterface
/** @var RepositoryInterface */
private $productOptionValueRepository;

/** @var TranslationLocaleProviderInterface|null */
private $translationLocaleProvider;

public function __construct(
ApiClientInterface $apiClient,
ProductOptionRepositoryInterface $productOptionRepository,
FactoryInterface $productOptionValueFactory,
FactoryInterface $productOptionValueTranslationFactory,
RepositoryInterface $productOptionValueRepository
RepositoryInterface $productOptionValueRepository,
TranslationLocaleProviderInterface $translationLocaleProvider = null
) {
$this->apiClient = $apiClient;
$this->productOptionRepository = $productOptionRepository;
$this->productOptionValueFactory = $productOptionValueFactory;
$this->productOptionValueTranslationFactory = $productOptionValueTranslationFactory;
$this->productOptionValueRepository = $productOptionValueRepository;
if ($translationLocaleProvider === null) {
trigger_deprecation(
'webgriffe/sylius-akeneo-plugin',
'1.6',
'Not passing a translation locale provider to %s is deprecated and will not be possible anymore in %s',
__CLASS__,
'2.0'
);
}
$this->translationLocaleProvider = $translationLocaleProvider;
}

/**
Expand Down Expand Up @@ -127,6 +142,10 @@ public function handle($productVariant, string $optionCode, array $akeneoValue):
$productOption->addValue($optionValue);
}
foreach ($akeneoAttributeOption['labels'] as $localeCode => $label) {
if ($this->translationLocaleProvider !== null &&
!in_array($localeCode, $this->translationLocaleProvider->getDefinedLocalesCodes(), true)) {
continue;
}
$optionValueTranslation = $optionValue->getTranslation($localeCode);
if ($optionValueTranslation->getLocale() !== $localeCode) {
/** @var ProductOptionValueTranslationInterface $optionValueTranslation */
Expand Down

0 comments on commit 50a9583

Please sign in to comment.