Skip to content

Commit

Permalink
Restore as previous temporarily
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Jul 10, 2024
1 parent e181cf5 commit 6caaacf
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 82 deletions.
4 changes: 2 additions & 2 deletions src/Attribute/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ?string>}
Expand All @@ -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';

Expand Down
64 changes: 57 additions & 7 deletions src/AttributeOptions/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -36,7 +36,7 @@ final class Importer implements ImporterInterface
{
use ProductOptionHelperTrait,
ProductOptionValueHelperTrait,
ProductAttributeHelperTrait;
SyliusProductAttributeHelperTrait;

/**
* @param RepositoryInterface<ProductAttributeInterface> $attributeRepository
Expand Down Expand Up @@ -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<string, array<string, string>>, 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<array-key, AkeneoAttributeOption> $attributeOptions
*
* @return array<string, array<string, string>>
*/
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
*/
Expand Down Expand Up @@ -181,6 +212,30 @@ private function importOptionValues(array $akeneoAttribute, ProductOptionInterfa
}
}

/**
* @return array<array-key, AkeneoAttributeOption>
*/
private function getSortedAkeneoAttributeOptionsByAttributeCode(string $attributeCode): array
{
$attributeOptionsOrdered = [];
/**
* @psalm-suppress TooManyTemplateParams
*
* @var ResourceCursorInterface<array-key, AkeneoAttributeOption> $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.
Expand Down Expand Up @@ -263,11 +318,6 @@ private function getProductOptionValueFromOption(
return $productOptionValue;
}

private function getAkeneoPimClient(): AkeneoPimClientInterface
{
return $this->apiClient;
}

private function getProductOptionRepository(): ?ProductOptionRepositoryInterface
{
return $this->optionRepository;
Expand Down
79 changes: 6 additions & 73 deletions src/ProductAttributeHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ?string>}
* @psalm-type AkeneoAttributeOption array{_links: array, code: string, attribute: string, sort_order: int, labels: array<string, ?string>}
*/
trait ProductAttributeHelperTrait
{
abstract private function getAkeneoPimClient(): AkeneoPimClientInterface;

/**
* @return RepositoryInterface<ProductAttributeInterface>
*/
abstract private function getAttributeRepository(): RepositoryInterface;

private function importAttributeConfiguration(string $attributeCode, ProductAttributeInterface $attribute): void
{
/** @var array{choices: array<string, array<string, string>>, multiple: bool, min: ?int, max: ?int} $configuration */
Expand All @@ -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<ProductAttributeInterface>
*/
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<array-key, AkeneoAttribute> $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<array-key, AkeneoAttribute> $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<array-key, AkeneoAttribute> $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<array-key, AkeneoAttributeOption> $attributeOptions
*
Expand Down
81 changes: 81 additions & 0 deletions src/SyliusProductAttributeHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusAkeneoPlugin;

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<string, ?string>}
*/
trait SyliusProductAttributeHelperTrait
{
/**
* @return RepositoryInterface<ProductAttributeInterface>
*/
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<array-key, AkeneoAttribute> $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<array-key, AkeneoAttribute> $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<array-key, AkeneoAttribute> $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;
}
}
8 changes: 8 additions & 0 deletions src/ValueHandler/AttributeValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,12 @@ private function getAkeneoPimClient(): AkeneoPimClientInterface

return $akeneoPimClient;
}

/**
* @return RepositoryInterface<ProductAttributeInterface>
*/
private function getAttributeRepository(): RepositoryInterface
{
return $this->attributeRepository;
}
}

0 comments on commit 6caaacf

Please sign in to comment.