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 cc48862
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 91 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
89 changes: 73 additions & 16 deletions src/AttributeOptions/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Webgriffe\SyliusAkeneoPlugin\ProductAttributeHelperTrait;
use Webgriffe\SyliusAkeneoPlugin\ProductOptionHelperTrait;
use Webgriffe\SyliusAkeneoPlugin\ProductOptionValueHelperTrait;
use Webgriffe\SyliusAkeneoPlugin\SyliusProductAttributeHelperTrait;
use Webmozart\Assert\Assert;

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

/**
* @param RepositoryInterface<ProductAttributeInterface> $attributeRepository
Expand Down Expand Up @@ -98,6 +100,26 @@ public function getAkeneoEntity(): string
return 'AttributeOptions';
}

public function import(string $identifier): void
{
$attribute = $this->attributeRepository->findOneBy(['code' => $identifier]);
if (null !== $attribute && $attribute->getType() === SelectAttributeType::TYPE) {
$this->importAttributeConfiguration($identifier, $attribute);
}
$optionRepository = $this->optionRepository;
if (!$optionRepository instanceof ProductOptionRepositoryInterface) {
return;
}
$option = $optionRepository->findOneBy(['code' => $identifier]);
if (!$option instanceof ProductOptionInterface) {
return;
}
/** @var AkeneoAttribute $attributeResponse */
$attributeResponse = $this->apiClient->getAttributeApi()->get($identifier);

$this->importOptionValues($attributeResponse, $option);
}

/**
* As stated at https://api.akeneo.com/documentation/filter.html#by-update-date-3:
*
Expand All @@ -124,24 +146,35 @@ public function getIdentifiersModifiedSince(DateTime $sinceDate): array
);
}

public function import(string $identifier): void
private function importAttributeConfiguration(string $attributeCode, ProductAttributeInterface $attribute): void
{
$attribute = $this->attributeRepository->findOneBy(['code' => $identifier]);
if (null !== $attribute && $attribute->getType() === SelectAttributeType::TYPE) {
$this->importAttributeConfiguration($identifier, $attribute);
}
$optionRepository = $this->optionRepository;
if (!$optionRepository instanceof ProductOptionRepositoryInterface) {
return;
}
$option = $optionRepository->findOneBy(['code' => $identifier]);
if (!$option instanceof ProductOptionInterface) {
return;
/** @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;
}
/** @var AkeneoAttribute $attributeResponse */
$attributeResponse = $this->apiClient->getAttributeApi()->get($identifier);

$this->importOptionValues($attributeResponse, $option);
return $choices;
}

/**
Expand Down Expand Up @@ -181,6 +214,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
73 changes: 0 additions & 73 deletions src/ProductAttributeHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

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
Expand All @@ -30,76 +27,6 @@ private function importAttributeConfiguration(string $attributeCode, ProductAttr
// Do not flush any change here, otherwise we will cause a potential MySQL error irreversible.
}

/**
* @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;
}
}

0 comments on commit cc48862

Please sign in to comment.