From 6caaacf3d970cf1bb61790ed35f0ac442ee996cb Mon Sep 17 00:00:00 2001 From: Lorenzo Ruozzi Date: Wed, 10 Jul 2024 17:17:21 +0200 Subject: [PATCH] Restore as previous temporarily --- src/Attribute/Importer.php | 4 +- src/AttributeOptions/Importer.php | 64 +++++++++++++++-- src/ProductAttributeHelperTrait.php | 79 ++------------------- src/SyliusProductAttributeHelperTrait.php | 81 ++++++++++++++++++++++ src/ValueHandler/AttributeValueHandler.php | 8 +++ 5 files changed, 154 insertions(+), 82 deletions(-) create mode 100644 src/SyliusProductAttributeHelperTrait.php diff --git a/src/Attribute/Importer.php b/src/Attribute/Importer.php index e15a1e3..8c5e0dc 100644 --- a/src/Attribute/Importer.php +++ b/src/Attribute/Importer.php @@ -18,8 +18,8 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Webgriffe\SyliusAkeneoPlugin\Event\IdentifiersModifiedSinceSearchBuilderBuiltEvent; use Webgriffe\SyliusAkeneoPlugin\ImporterInterface; -use Webgriffe\SyliusAkeneoPlugin\ProductAttributeHelperTrait; use Webgriffe\SyliusAkeneoPlugin\ProductOptionHelperTrait; +use Webgriffe\SyliusAkeneoPlugin\SyliusProductAttributeHelperTrait; /** * @psalm-type AkeneoAttribute array{code: string, type: string, labels: array} @@ -34,7 +34,7 @@ final class Importer implements ImporterInterface public const METRIC_TYPE = 'pim_catalog_metric'; - use ProductOptionHelperTrait, ProductAttributeHelperTrait; + use ProductOptionHelperTrait, SyliusProductAttributeHelperTrait; public const AKENEO_ENTITY = 'Attribute'; diff --git a/src/AttributeOptions/Importer.php b/src/AttributeOptions/Importer.php index d5549da..86a74e2 100644 --- a/src/AttributeOptions/Importer.php +++ b/src/AttributeOptions/Importer.php @@ -23,9 +23,9 @@ use Webgriffe\SyliusAkeneoPlugin\Attribute\Importer as AttributeImporter; use Webgriffe\SyliusAkeneoPlugin\Event\IdentifiersModifiedSinceSearchBuilderBuiltEvent; use Webgriffe\SyliusAkeneoPlugin\ImporterInterface; -use Webgriffe\SyliusAkeneoPlugin\ProductAttributeHelperTrait; use Webgriffe\SyliusAkeneoPlugin\ProductOptionHelperTrait; use Webgriffe\SyliusAkeneoPlugin\ProductOptionValueHelperTrait; +use Webgriffe\SyliusAkeneoPlugin\SyliusProductAttributeHelperTrait; use Webmozart\Assert\Assert; /** @@ -36,7 +36,7 @@ final class Importer implements ImporterInterface { use ProductOptionHelperTrait, ProductOptionValueHelperTrait, - ProductAttributeHelperTrait; + SyliusProductAttributeHelperTrait; /** * @param RepositoryInterface $attributeRepository @@ -144,6 +144,37 @@ public function import(string $identifier): void $this->importOptionValues($attributeResponse, $option); } + private function importAttributeConfiguration(string $attributeCode, ProductAttributeInterface $attribute): void + { + /** @var array{choices: array>, multiple: bool, min: ?int, max: ?int} $configuration */ + $configuration = $attribute->getConfiguration(); + $configuration['choices'] = $this->convertAkeneoAttributeOptionsIntoSyliusChoices( + $this->getSortedAkeneoAttributeOptionsByAttributeCode($attributeCode), + ); + $attribute->setConfiguration($configuration); + + $this->attributeRepository->add($attribute); + } + + /** + * @param array $attributeOptions + * + * @return array> + */ + private function convertAkeneoAttributeOptionsIntoSyliusChoices(array $attributeOptions): array + { + $choices = []; + foreach ($attributeOptions as $attributeOption) { + $attributeOptionLabelsNotNull = array_filter( + $attributeOption['labels'], + static fn (?string $label): bool => $label !== null, + ); + $choices[$attributeOption['code']] = $attributeOptionLabelsNotNull; + } + + return $choices; + } + /** * @param AkeneoAttribute $akeneoAttribute */ @@ -181,6 +212,30 @@ private function importOptionValues(array $akeneoAttribute, ProductOptionInterfa } } + /** + * @return array + */ + private function getSortedAkeneoAttributeOptionsByAttributeCode(string $attributeCode): array + { + $attributeOptionsOrdered = []; + /** + * @psalm-suppress TooManyTemplateParams + * + * @var ResourceCursorInterface $attributeOptions + */ + $attributeOptions = $this->apiClient->getAttributeOptionApi()->all($attributeCode); + /** @var AkeneoAttributeOption $attributeOption */ + foreach ($attributeOptions as $attributeOption) { + $attributeOptionsOrdered[] = $attributeOption; + } + usort( + $attributeOptionsOrdered, + static fn (array $option1, array $option2): int => $option1['sort_order'] <=> $option2['sort_order'], + ); + + return $attributeOptionsOrdered; + } + /** * This method should be called only if the productOptionRepository is injected, so we can assume * that this factory is injected too. @@ -263,11 +318,6 @@ private function getProductOptionValueFromOption( return $productOptionValue; } - private function getAkeneoPimClient(): AkeneoPimClientInterface - { - return $this->apiClient; - } - private function getProductOptionRepository(): ?ProductOptionRepositoryInterface { return $this->optionRepository; diff --git a/src/ProductAttributeHelperTrait.php b/src/ProductAttributeHelperTrait.php index d7b1f33..db84735 100644 --- a/src/ProductAttributeHelperTrait.php +++ b/src/ProductAttributeHelperTrait.php @@ -6,18 +6,21 @@ use Akeneo\Pim\ApiClient\AkeneoPimClientInterface; use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface; -use Sylius\Component\Attribute\AttributeType\SelectAttributeType; use Sylius\Component\Product\Model\ProductAttributeInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; /** - * @psalm-type AkeneoAttribute array{code: string, type: string, labels: array} * @psalm-type AkeneoAttributeOption array{_links: array, code: string, attribute: string, sort_order: int, labels: array} */ trait ProductAttributeHelperTrait { abstract private function getAkeneoPimClient(): AkeneoPimClientInterface; + /** + * @return RepositoryInterface + */ + abstract private function getAttributeRepository(): RepositoryInterface; + private function importAttributeConfiguration(string $attributeCode, ProductAttributeInterface $attribute): void { /** @var array{choices: array>, multiple: bool, min: ?int, max: ?int} $configuration */ @@ -27,79 +30,9 @@ private function importAttributeConfiguration(string $attributeCode, ProductAttr ); $attribute->setConfiguration($configuration); - // Do not flush any change here, otherwise we will cause a potential MySQL error irreversible. + $this->getAttributeRepository()->add($attribute); } - /** - * @return RepositoryInterface - */ - abstract private function getProductAttributeRepository(): RepositoryInterface; - - /** - * Return the list of Akeneo attribute codes whose code is used as a code for a Sylius SELECT attribute - * - * @psalm-suppress TooManyTemplateParams - * - * @param ResourceCursorInterface $akeneoAttributes - * - * @return string[] - */ - private function filterBySyliusSelectAttributeCodes(ResourceCursorInterface $akeneoAttributes): array - { - $syliusSelectAttributes = $this->getProductAttributeRepository()->findBy(['type' => SelectAttributeType::TYPE]); - - return $this->filterBySyliusAttributes($syliusSelectAttributes, $akeneoAttributes); - } - - /** - * Return the list of Akeneo attribute codes whose code is used as a code for a Sylius attribute - * - * @psalm-suppress TooManyTemplateParams - * - * @param ResourceCursorInterface $akeneoAttributes - * - * @return string[] - */ - private function filterBySyliusAttributeCodes(ResourceCursorInterface $akeneoAttributes): array - { - $syliusAttributes = $this->getProductAttributeRepository()->findAll(); - - return $this->filterBySyliusAttributes($syliusAttributes, $akeneoAttributes); - } - - /** - * @psalm-suppress TooManyTemplateParams - * - * @param ProductAttributeInterface[] $syliusAttributes - * @param ResourceCursorInterface $akeneoAttributes - * - * @return string[] - */ - private function filterBySyliusAttributes(array $syliusAttributes, ResourceCursorInterface $akeneoAttributes): array - { - $syliusAttributes = array_filter( - array_map( - static fn (ProductAttributeInterface $attribute): ?string => $attribute->getCode(), - $syliusAttributes, - ), - ); - $attributeCodes = []; - /** @var AkeneoAttribute $akeneoAttribute */ - foreach ($akeneoAttributes as $akeneoAttribute) { - if (!in_array($akeneoAttribute['code'], $syliusAttributes, true)) { - continue; - } - $attributeCodes[] = $akeneoAttribute['code']; - } - usort( - $attributeOptionsOrdered, - static fn (array $option1, array $option2): int => $option1['sort_order'] <=> $option2['sort_order'], - ); - - return $attributeCodes; - } - - /** * @param array $attributeOptions * diff --git a/src/SyliusProductAttributeHelperTrait.php b/src/SyliusProductAttributeHelperTrait.php new file mode 100644 index 0000000..375c35d --- /dev/null +++ b/src/SyliusProductAttributeHelperTrait.php @@ -0,0 +1,81 @@ +} + */ +trait SyliusProductAttributeHelperTrait +{ + /** + * @return RepositoryInterface + */ + abstract private function getProductAttributeRepository(): RepositoryInterface; + + /** + * Return the list of Akeneo attribute codes whose code is used as a code for a Sylius SELECT attribute + * + * @psalm-suppress TooManyTemplateParams + * + * @param ResourceCursorInterface $akeneoAttributes + * + * @return string[] + */ + private function filterBySyliusSelectAttributeCodes(ResourceCursorInterface $akeneoAttributes): array + { + $syliusSelectAttributes = $this->getProductAttributeRepository()->findBy(['type' => SelectAttributeType::TYPE]); + + return $this->filterBySyliusAttributes($syliusSelectAttributes, $akeneoAttributes); + } + + /** + * Return the list of Akeneo attribute codes whose code is used as a code for a Sylius attribute + * + * @psalm-suppress TooManyTemplateParams + * + * @param ResourceCursorInterface $akeneoAttributes + * + * @return string[] + */ + private function filterBySyliusAttributeCodes(ResourceCursorInterface $akeneoAttributes): array + { + $syliusAttributes = $this->getProductAttributeRepository()->findAll(); + + return $this->filterBySyliusAttributes($syliusAttributes, $akeneoAttributes); + } + + /** + * @psalm-suppress TooManyTemplateParams + * + * @param ProductAttributeInterface[] $syliusAttributes + * @param ResourceCursorInterface $akeneoAttributes + * + * @return string[] + */ + private function filterBySyliusAttributes(array $syliusAttributes, ResourceCursorInterface $akeneoAttributes): array + { + $syliusAttributes = array_filter( + array_map( + static fn (ProductAttributeInterface $attribute): ?string => $attribute->getCode(), + $syliusAttributes, + ), + ); + $attributeCodes = []; + /** @var AkeneoAttribute $akeneoAttribute */ + foreach ($akeneoAttributes as $akeneoAttribute) { + if (!in_array($akeneoAttribute['code'], $syliusAttributes, true)) { + continue; + } + $attributeCodes[] = $akeneoAttribute['code']; + } + + return $attributeCodes; + } +} diff --git a/src/ValueHandler/AttributeValueHandler.php b/src/ValueHandler/AttributeValueHandler.php index 52b7229..6c8e5d0 100644 --- a/src/ValueHandler/AttributeValueHandler.php +++ b/src/ValueHandler/AttributeValueHandler.php @@ -185,4 +185,12 @@ private function getAkeneoPimClient(): AkeneoPimClientInterface return $akeneoPimClient; } + + /** + * @return RepositoryInterface + */ + private function getAttributeRepository(): RepositoryInterface + { + return $this->attributeRepository; + } }