Skip to content

Commit

Permalink
Import and update product option and product option values (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Nov 17, 2023
1 parent e745af0 commit ff07dfd
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 20 deletions.
5 changes: 5 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@
<argument type="service" id="webgriffe_sylius_akeneo.api_client"/>
<argument type="service" id="sylius.repository.product_attribute"/>
<argument type="service" id="event_dispatcher" />
<argument type="service" id="sylius.repository.product_option" />
<argument type="service" id="sylius.translation_locale_provider.admin" />
<argument type="service" id="sylius.factory.product_option_value_translation" />
<argument type="service" id="sylius.factory.product_option_value" />
<argument type="service" id="sylius.factory.product_option_translation" />
<tag name="webgriffe_sylius_akeneo.importer" />
</service>

Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ parameters:
message: '/Cannot call method trans\(\) on Symfony\\Contracts\\Translation\\TranslatorInterface\|null\./'
path: src/Controller/ProductEnqueueController

# Move alias here when global alias are supported also on Psalm: https://github.com/vimeo/psalm/discussions/5376
typeAliases:
200 changes: 185 additions & 15 deletions src/AttributeOptions/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@
use DateTime;
use Sylius\Component\Attribute\AttributeType\SelectAttributeType;
use Sylius\Component\Product\Model\ProductAttributeInterface;
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 Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Webgriffe\SyliusAkeneoPlugin\Event\IdentifiersModifiedSinceSearchBuilderBuiltEvent;
use Webgriffe\SyliusAkeneoPlugin\ImporterInterface;
use Webmozart\Assert\Assert;

/**
* @phpstan-type AkeneoAttribute array{code: string, type: string}
* @phpstan-type AkeneoAttributeOption array{_links: array, code: string, attribute: string, sort_order: int, labels: array<string, string>}
* @psalm-type AkeneoAttribute array{code: string, type: string}
* @psalm-type AkeneoAttributeOption array{_links: array, code: string, attribute: string, sort_order: int, labels: array<string, ?string>}
*/
final class Importer implements ImporterInterface
{
Expand All @@ -27,12 +35,47 @@ final class Importer implements ImporterInterface

/**
* @param RepositoryInterface<ProductAttributeInterface> $attributeRepository
* @param ?FactoryInterface<ProductOptionValueTranslationInterface> $productOptionValueTranslationFactory
* @param ?FactoryInterface<ProductOptionValueInterface> $productOptionValueFactory
* @param ?FactoryInterface<ProductOptionTranslationInterface> $productOptionTranslationFactory
*/
public function __construct(
private AkeneoPimClientInterface $apiClient,
private RepositoryInterface $attributeRepository,
private EventDispatcherInterface $eventDispatcher,
private ?ProductOptionRepositoryInterface $optionRepository = null,
private ?TranslationLocaleProviderInterface $translationLocaleProvider = null,
private ?FactoryInterface $productOptionValueTranslationFactory = null,
private ?FactoryInterface $productOptionValueFactory = null,
private ?FactoryInterface $productOptionTranslationFactory = null,
) {
if ($this->optionRepository === null) {
trigger_deprecation(
'webgriffe/sylius-akeneo-plugin',
'v2.2.0',
'Not passing a "%s" instance to "%s" constructor is deprecated and will not be possible anymore in the next major version.',
ProductOptionRepositoryInterface::class,
self::class,
);
}
if ($this->translationLocaleProvider === null) {
trigger_deprecation(
'webgriffe/sylius-akeneo-plugin',
'v2.2.0',
'Not passing a "%s" instance to "%s" constructor is deprecated and will not be possible anymore in the next major version.',
TranslationLocaleProviderInterface::class,
self::class,
);
}
if ($this->productOptionValueTranslationFactory === null) {
trigger_deprecation(
'webgriffe/sylius-akeneo-plugin',
'v2.2.0',
'Not passing a "%s" instance to "%s" constructor is deprecated and will not be possible anymore in the next major version.',
FactoryInterface::class,
self::class,
);
}
}

public function getAkeneoEntity(): string
Expand All @@ -44,8 +87,18 @@ public function import(string $identifier): void
{
$attribute = $this->attributeRepository->findOneBy(['code' => $identifier]);
if (null !== $attribute && $attribute->getType() === SelectAttributeType::TYPE) {
$this->importAttribute($identifier, $attribute);
$this->importAttributeConfiguration($identifier, $attribute);
}
$optionRepository = $this->optionRepository;
if (!$optionRepository instanceof ProductOptionRepositoryInterface) {
return;
}
$option = $optionRepository->findOneBy(['code' => $identifier]);
if (!$option instanceof ProductOptionInterface) {
return;
}
$this->updateProductOption($option);
$this->importOptionValues($identifier, $option);
}

/**
Expand All @@ -64,7 +117,10 @@ public function getIdentifiersModifiedSince(DateTime $sinceDate): array
/** @var ResourceCursorInterface<array-key, AkeneoAttribute> $akeneoAttributes */
$akeneoAttributes = $this->apiClient->getAttributeApi()->all(50, ['search' => $searchBuilder->getFilters()]);

Check failure on line 118 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:118:9: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 118 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:118:9: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 118 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:118:9: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 118 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:118:9: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 118 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:118:9: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 118 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:118:9: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 118 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:118:9: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 118 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:118:9: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 118 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:118:9: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 118 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:118:9: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

return $this->filterBySyliusAttributeCodes($akeneoAttributes);
return array_merge(
$this->filterBySyliusAttributeCodes($akeneoAttributes),
$this->filterSyliusOptionCodes($akeneoAttributes),
);
}

/**
Expand Down Expand Up @@ -97,21 +153,38 @@ private function filterBySyliusAttributeCodes(ResourceCursorInterface $akeneoAtt
return $attributeCodes;

Check failure on line 153 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

MixedReturnTypeCoercion

src/AttributeOptions/Importer.php:153:16: MixedReturnTypeCoercion: The type 'list<mixed>' is more general than the declared return type 'array<array-key, string>' for Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer::filterBySyliusAttributeCodes (see https://psalm.dev/197)

Check failure on line 153 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

MixedReturnTypeCoercion

src/AttributeOptions/Importer.php:153:16: MixedReturnTypeCoercion: The type 'list<mixed>' is more general than the declared return type 'array<array-key, string>' for Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer::filterBySyliusAttributeCodes (see https://psalm.dev/197)

Check failure on line 153 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

MixedReturnTypeCoercion

src/AttributeOptions/Importer.php:153:16: MixedReturnTypeCoercion: The type 'list<mixed>' is more general than the declared return type 'array<array-key, string>' for Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer::filterBySyliusAttributeCodes (see https://psalm.dev/197)

Check failure on line 153 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

MixedReturnTypeCoercion

src/AttributeOptions/Importer.php:153:16: MixedReturnTypeCoercion: The type 'list<mixed>' is more general than the declared return type 'array<array-key, string>' for Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer::filterBySyliusAttributeCodes (see https://psalm.dev/197)

Check failure on line 153 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

MixedReturnTypeCoercion

src/AttributeOptions/Importer.php:153:16: MixedReturnTypeCoercion: The type 'list<mixed>' is more general than the declared return type 'array<array-key, string>' for Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer::filterBySyliusAttributeCodes (see https://psalm.dev/197)

Check failure on line 153 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

MixedReturnTypeCoercion

src/AttributeOptions/Importer.php:153:16: MixedReturnTypeCoercion: The type 'list<mixed>' is more general than the declared return type 'array<array-key, string>' for Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer::filterBySyliusAttributeCodes (see https://psalm.dev/197)

Check failure on line 153 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

MixedReturnTypeCoercion

src/AttributeOptions/Importer.php:153:16: MixedReturnTypeCoercion: The type 'list<mixed>' is more general than the declared return type 'array<array-key, string>' for Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer::filterBySyliusAttributeCodes (see https://psalm.dev/197)

Check failure on line 153 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

MixedReturnTypeCoercion

src/AttributeOptions/Importer.php:153:16: MixedReturnTypeCoercion: The type 'list<mixed>' is more general than the declared return type 'array<array-key, string>' for Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer::filterBySyliusAttributeCodes (see https://psalm.dev/197)

Check failure on line 153 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

MixedReturnTypeCoercion

src/AttributeOptions/Importer.php:153:16: MixedReturnTypeCoercion: The type 'list<mixed>' is more general than the declared return type 'array<array-key, string>' for Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer::filterBySyliusAttributeCodes (see https://psalm.dev/197)

Check failure on line 153 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

MixedReturnTypeCoercion

src/AttributeOptions/Importer.php:153:16: MixedReturnTypeCoercion: The type 'list<mixed>' is more general than the declared return type 'array<array-key, string>' for Webgriffe\SyliusAkeneoPlugin\AttributeOptions\Importer::filterBySyliusAttributeCodes (see https://psalm.dev/197)
}

private function importAttribute(string $attributeCode, ProductAttributeInterface $attribute): void
/**
* Return the list of Akeneo attribute codes whose code is used as a code for a Sylius attribute
*
* @param ResourceCursorInterface<array-key, AkeneoAttribute> $akeneoAttributes

Check failure on line 159 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:159:15: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 159 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:159:15: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 159 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:159:15: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 159 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:159:15: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 159 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:159:15: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 159 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:159:15: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 159 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:159:15: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 159 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:159:15: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 159 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:159:15: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 159 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

TooManyTemplateParams

src/AttributeOptions/Importer.php:159:15: TooManyTemplateParams: Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface<array-key, array{code: string, type: string}> has too many template params, expecting 0 (see https://psalm.dev/184)
*
* @return string[]
*/
private function filterSyliusOptionCodes(ResourceCursorInterface $akeneoAttributes): array
{
$attributeOptionsOrdered = [];
/** @var ResourceCursorInterface<array-key, AkeneoAttributeOption> $attributeOptions */
$attributeOptions = $this->apiClient->getAttributeOptionApi()->all($attributeCode);
foreach ($attributeOptions as $attributeOption) {
$attributeOptionsOrdered[] = $attributeOption;
$productOptionRepository = $this->optionRepository;
if (!$productOptionRepository instanceof ProductOptionRepositoryInterface) {
return [];
}
usort(
$attributeOptionsOrdered,
static fn (array $option1, array $option2): int => $option1['sort_order'] <=> $option2['sort_order'],
$akeneoAttributeCodes = [];
foreach ($akeneoAttributes as $akeneoAttribute) {

Check failure on line 170 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:170:39: MixedAssignment: Unable to determine the type that $akeneoAttribute is being assigned to (see https://psalm.dev/032)

Check failure on line 170 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:170:39: MixedAssignment: Unable to determine the type that $akeneoAttribute is being assigned to (see https://psalm.dev/032)

Check failure on line 170 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:170:39: MixedAssignment: Unable to determine the type that $akeneoAttribute is being assigned to (see https://psalm.dev/032)

Check failure on line 170 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:170:39: MixedAssignment: Unable to determine the type that $akeneoAttribute is being assigned to (see https://psalm.dev/032)

Check failure on line 170 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:170:39: MixedAssignment: Unable to determine the type that $akeneoAttribute is being assigned to (see https://psalm.dev/032)

Check failure on line 170 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:170:39: MixedAssignment: Unable to determine the type that $akeneoAttribute is being assigned to (see https://psalm.dev/032)

Check failure on line 170 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:170:39: MixedAssignment: Unable to determine the type that $akeneoAttribute is being assigned to (see https://psalm.dev/032)

Check failure on line 170 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:170:39: MixedAssignment: Unable to determine the type that $akeneoAttribute is being assigned to (see https://psalm.dev/032)

Check failure on line 170 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:170:39: MixedAssignment: Unable to determine the type that $akeneoAttribute is being assigned to (see https://psalm.dev/032)

Check failure on line 170 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:170:39: MixedAssignment: Unable to determine the type that $akeneoAttribute is being assigned to (see https://psalm.dev/032)
$akeneoAttributeCodes[] = $akeneoAttribute['code'];

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:171:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

MixedArrayAccess

src/AttributeOptions/Importer.php:171:39: MixedArrayAccess: Cannot access array value on mixed variable $akeneoAttribute (see https://psalm.dev/051)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:171:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

MixedArrayAccess

src/AttributeOptions/Importer.php:171:39: MixedArrayAccess: Cannot access array value on mixed variable $akeneoAttribute (see https://psalm.dev/051)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:171:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

MixedArrayAccess

src/AttributeOptions/Importer.php:171:39: MixedArrayAccess: Cannot access array value on mixed variable $akeneoAttribute (see https://psalm.dev/051)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:171:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

MixedArrayAccess

src/AttributeOptions/Importer.php:171:39: MixedArrayAccess: Cannot access array value on mixed variable $akeneoAttribute (see https://psalm.dev/051)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:171:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

MixedArrayAccess

src/AttributeOptions/Importer.php:171:39: MixedArrayAccess: Cannot access array value on mixed variable $akeneoAttribute (see https://psalm.dev/051)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:171:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

MixedArrayAccess

src/AttributeOptions/Importer.php:171:39: MixedArrayAccess: Cannot access array value on mixed variable $akeneoAttribute (see https://psalm.dev/051)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:171:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

MixedArrayAccess

src/AttributeOptions/Importer.php:171:39: MixedArrayAccess: Cannot access array value on mixed variable $akeneoAttribute (see https://psalm.dev/051)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:171:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

MixedArrayAccess

src/AttributeOptions/Importer.php:171:39: MixedArrayAccess: Cannot access array value on mixed variable $akeneoAttribute (see https://psalm.dev/051)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:171:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

MixedArrayAccess

src/AttributeOptions/Importer.php:171:39: MixedArrayAccess: Cannot access array value on mixed variable $akeneoAttribute (see https://psalm.dev/051)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

MixedAssignment

src/AttributeOptions/Importer.php:171:13: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 171 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

MixedArrayAccess

src/AttributeOptions/Importer.php:171:39: MixedArrayAccess: Cannot access array value on mixed variable $akeneoAttribute (see https://psalm.dev/051)
}
$syliusOptions = $productOptionRepository->findByCodes($akeneoAttributeCodes);

Check failure on line 173 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

MixedArgumentTypeCoercion

src/AttributeOptions/Importer.php:173:64: MixedArgumentTypeCoercion: Argument 1 of Sylius\Component\Product\Repository\ProductOptionRepositoryInterface::findByCodes expects array<array-key, string>, but parent type list{0?: mixed, ...<mixed>} provided (see https://psalm.dev/194)

Check failure on line 173 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

MixedArgumentTypeCoercion

src/AttributeOptions/Importer.php:173:64: MixedArgumentTypeCoercion: Argument 1 of Sylius\Component\Product\Repository\ProductOptionRepositoryInterface::findByCodes expects array<array-key, string>, but parent type list{0?: mixed, ...<mixed>} provided (see https://psalm.dev/194)

Check failure on line 173 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

MixedArgumentTypeCoercion

src/AttributeOptions/Importer.php:173:64: MixedArgumentTypeCoercion: Argument 1 of Sylius\Component\Product\Repository\ProductOptionRepositoryInterface::findByCodes expects array<array-key, string>, but parent type list{0?: mixed, ...<mixed>} provided (see https://psalm.dev/194)

Check failure on line 173 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

MixedArgumentTypeCoercion

src/AttributeOptions/Importer.php:173:64: MixedArgumentTypeCoercion: Argument 1 of Sylius\Component\Product\Repository\ProductOptionRepositoryInterface::findByCodes expects array<array-key, string>, but parent type list{0?: mixed, ...<mixed>} provided (see https://psalm.dev/194)

Check failure on line 173 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^6.3, MySQL 8.0

MixedArgumentTypeCoercion

src/AttributeOptions/Importer.php:173:64: MixedArgumentTypeCoercion: Argument 1 of Sylius\Component\Product\Repository\ProductOptionRepositoryInterface::findByCodes expects array<array-key, string>, but parent type list{0?: mixed, ...<mixed>} provided (see https://psalm.dev/194)

Check failure on line 173 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.0, Symfony ^5.4, MySQL 8.0

MixedArgumentTypeCoercion

src/AttributeOptions/Importer.php:173:64: MixedArgumentTypeCoercion: Argument 1 of Sylius\Component\Product\Repository\ProductOptionRepositoryInterface::findByCodes expects array<array-key, string>, but parent type list{0?: mixed, ...<mixed>} provided (see https://psalm.dev/194)

Check failure on line 173 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

MixedArgumentTypeCoercion

src/AttributeOptions/Importer.php:173:64: MixedArgumentTypeCoercion: Argument 1 of Sylius\Component\Product\Repository\ProductOptionRepositoryInterface::findByCodes expects array<array-key, string>, but parent type list{0?: mixed, ...<mixed>} provided (see https://psalm.dev/194)

Check failure on line 173 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^6.3, MySQL 8.0

MixedArgumentTypeCoercion

src/AttributeOptions/Importer.php:173:64: MixedArgumentTypeCoercion: Argument 1 of Sylius\Component\Product\Repository\ProductOptionRepositoryInterface::findByCodes expects array<array-key, string>, but parent type list{0?: mixed, ...<mixed>} provided (see https://psalm.dev/194)

Check failure on line 173 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.1, Symfony ^5.4, MySQL 8.0

MixedArgumentTypeCoercion

src/AttributeOptions/Importer.php:173:64: MixedArgumentTypeCoercion: Argument 1 of Sylius\Component\Product\Repository\ProductOptionRepositoryInterface::findByCodes expects array<array-key, string>, but parent type list{0?: mixed, ...<mixed>} provided (see https://psalm.dev/194)

Check failure on line 173 in src/AttributeOptions/Importer.php

View workflow job for this annotation

GitHub Actions / Sylius ^1.12, PHP 8.2, Symfony ^5.4, MySQL 8.0

MixedArgumentTypeCoercion

src/AttributeOptions/Importer.php:173:64: MixedArgumentTypeCoercion: Argument 1 of Sylius\Component\Product\Repository\ProductOptionRepositoryInterface::findByCodes expects array<array-key, string>, but parent type list{0?: mixed, ...<mixed>} provided (see https://psalm.dev/194)

return array_map(
static fn (ProductOptionInterface $option): string => (string) $option->getCode(),
$syliusOptions,
);
}

private function importAttributeConfiguration(string $attributeCode, ProductAttributeInterface $attribute): void
{
/** @var array{choices: array<string, array<string, string>>, multiple: bool, min: ?int, max: ?int} $configuration */
$configuration = $attribute->getConfiguration();
$configuration['choices'] = $this->convertAkeneoAttributeOptionsIntoSyliusChoices($attributeOptionsOrdered);
$configuration['choices'] = $this->convertAkeneoAttributeOptionsIntoSyliusChoices(
$this->getSortedAkeneoAttributeOptionsByAttributeCode($attributeCode),
);
$attribute->setConfiguration($configuration);

$this->attributeRepository->add($attribute);
Expand All @@ -126,9 +199,106 @@ private function convertAkeneoAttributeOptionsIntoSyliusChoices(array $attribute
{
$choices = [];
foreach ($attributeOptions as $attributeOption) {
$choices[$attributeOption['code']] = $attributeOption['labels'];
$attributeOptionLabelsNotNull = array_filter(
$attributeOption['labels'],
static fn (?string $label): bool => $label !== null,
);
$choices[$attributeOption['code']] = $attributeOptionLabelsNotNull;
}

return $choices;
}

private function importOptionValues(string $attributeCode, ProductOptionInterface $option): void
{
$attributeOptions = $this->getSortedAkeneoAttributeOptionsByAttributeCode($attributeCode);

foreach ($attributeOptions as $attributeOption) {
$optionValueCode = $attributeCode . '_' . $attributeOption['code'];
$optionValue = null;
foreach ($option->getValues() as $value) {
if ($value->getCode() === $optionValueCode) {
$optionValue = $value;

break;
}
}
if ($optionValue === null) {
// We can assume that if we are here is because the option repository has been injected, so event this factory should be!
$productOptionValueFactory = $this->productOptionValueFactory;
Assert::isInstanceOf($productOptionValueFactory, FactoryInterface::class);
$optionValue = $productOptionValueFactory->createNew();
// TODO handle translations
$optionValue->setCode($optionValueCode);
$option->addValue($optionValue);
}

// 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);
}
}
}
}

/**
* @return array<array-key, AkeneoAttributeOption>
*/
private function getSortedAkeneoAttributeOptionsByAttributeCode(string $attributeCode): array
{
$attributeOptionsOrdered = [];
/** @var ResourceCursorInterface<array-key, AkeneoAttributeOption> $attributeOptions */
$attributeOptions = $this->apiClient->getAttributeOptionApi()->all($attributeCode);
foreach ($attributeOptions as $attributeOption) {
$attributeOptionsOrdered[] = $attributeOption;
}
usort(
$attributeOptionsOrdered,
static fn (array $option1, array $option2): int => $option1['sort_order'] <=> $option2['sort_order'],
);

return $attributeOptionsOrdered;
}

private function updateProductOption(ProductOptionInterface $productOption): void
{
// TODO: Update also the position of the option? The problem is that this position is on family variant entity!
$productOptionCode = $productOption->getCode();
Assert::notNull($productOptionCode);

// We can assume that if we are here is because the option repository has been injected, so event this factory should be!
$productOptionTranslationFactory = $this->productOptionTranslationFactory;
Assert::isInstanceOf($productOptionTranslationFactory, FactoryInterface::class);

$attributeResponse = $this->apiClient->getAttributeApi()->get($productOptionCode);
foreach ($attributeResponse['labels'] as $locale => $label) {
$productOptionTranslation = $productOption->getTranslation($locale);
if ($productOptionTranslation->getLocale() === $locale) {
$productOptionTranslation->setName($label);

continue;
}
$newProductOptionTranslation = $productOptionTranslationFactory->createNew();
$newProductOptionTranslation->setLocale($locale);
$newProductOptionTranslation->setName($label);
$productOption->addTranslation($newProductOptionTranslation);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Sylius\Component\Locale\Model\Locale:
en_US:
code: "en_US"
it_IT:
code: "it_IT"

Sylius\Component\Product\Model\ProductOption:
size:
code: 'size'
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ Sylius\Component\Locale\Model\Locale:
code: "it_IT"

Sylius\Component\Product\Model\ProductAttribute:
finitura:
material:
code: 'material'
type: 'select'
storageType: 'json'

Sylius\Component\Product\Model\ProductOption:
size:
code: 'size'
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Sylius\Component\Locale\Model\Locale:
en_US:
code: 'en_US'
it_IT:
code: 'it_IT'

Sylius\Component\Product\Model\ProductOption:
size:
code: 'size'
translations:
- '@size_en_US'
- '@size_it_IT'

Sylius\Component\Product\Model\ProductOptionTranslation:
size_en_US:
name: 'Format'
locale: 'en_US'
translatable: '@size'
size_it_IT:
name: 'Formato'
locale: 'it_IT'
translatable: '@size'

Sylius\Component\Product\Model\ProductOptionValue:
small:
code: 'size_small'
option: '@size'
translations:
- '@small_it_IT'
- '@small_en_US'
large:
code: 'size_large'
option: '@size'
translations:
- '@large_it_IT'
- '@large_en_US'

Sylius\Component\Product\Model\ProductOptionValueTranslation:
small_it_IT:
value: 'S'
locale: 'it_IT'
translatable: '@small'
small_en_US:
value: 'S'
locale: 'en_US'
translatable: '@small'
large_it_IT:
value: 'L'
locale: 'it_IT'
translatable: '@large'
large_en_US:
value: 'L'
locale: 'en_US'
translatable: '@large'
2 changes: 1 addition & 1 deletion tests/InMemory/Client/Api/InMemoryAttributeOptionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(private ArrayIterator $iterator, private int $pageSi
{
}

public function current()
public function current(): mixed
{
return $this->iterator->current();
}
Expand Down
Loading

0 comments on commit ff07dfd

Please sign in to comment.