-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusAkeneoPlugin\Attribute; | ||
|
||
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface; | ||
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface; | ||
use Akeneo\Pim\ApiClient\Search\SearchBuilder; | ||
use DateTime; | ||
use Sylius\Component\Product\Model\ProductAttributeInterface; | ||
use Sylius\Component\Product\Model\ProductAttributeTranslationInterface; | ||
use Sylius\Component\Product\Model\ProductOptionInterface; | ||
use Sylius\Component\Product\Model\ProductOptionTranslationInterface; | ||
use Sylius\Component\Product\Repository\ProductOptionRepositoryInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Webgriffe\SyliusAkeneoPlugin\Event\IdentifiersModifiedSinceSearchBuilderBuiltEvent; | ||
use Webgriffe\SyliusAkeneoPlugin\ImporterInterface; | ||
use Webgriffe\SyliusAkeneoPlugin\ProductOptionHelperTrait; | ||
use Webgriffe\SyliusAkeneoPlugin\SyliusProductAttributeHelperTrait; | ||
|
||
/** | ||
* @psalm-type AkeneoAttribute array{code: string, type: string, labels: array<string, ?string>} | ||
*/ | ||
final class Importer implements ImporterInterface | ||
{ | ||
public const SIMPLESELECT_TYPE = 'pim_catalog_simpleselect'; | ||
|
||
public const MULTISELECT_TYPE = 'pim_catalog_multiselect'; | ||
|
||
public const BOOLEAN_TYPE = 'pim_catalog_boolean'; | ||
|
||
public const METRIC_TYPE = 'pim_catalog_metric'; | ||
|
||
public const AKENEO_ENTITY = 'Attribute'; | ||
|
||
use ProductOptionHelperTrait, SyliusProductAttributeHelperTrait; | ||
|
||
/** | ||
* @param FactoryInterface<ProductOptionTranslationInterface> $productOptionTranslationFactory | ||
* @param RepositoryInterface<ProductAttributeInterface> $productAttributeRepository | ||
* @param FactoryInterface<ProductAttributeTranslationInterface> $productAttributeTranslationFactory | ||
*/ | ||
public function __construct( | ||
private EventDispatcherInterface $eventDispatcher, | ||
private AkeneoPimClientInterface $apiClient, | ||
private ProductOptionRepositoryInterface $productOptionRepository, | ||
private FactoryInterface $productOptionTranslationFactory, | ||
private RepositoryInterface $productAttributeRepository, | ||
private FactoryInterface $productAttributeTranslationFactory, | ||
) { | ||
} | ||
|
||
public function getAkeneoEntity(): string | ||
{ | ||
return self::AKENEO_ENTITY; | ||
} | ||
|
||
public function getIdentifiersModifiedSince(DateTime $sinceDate): array | ||
{ | ||
$searchBuilder = new SearchBuilder(); | ||
$this->eventDispatcher->dispatch( | ||
new IdentifiersModifiedSinceSearchBuilderBuiltEvent($this, $searchBuilder, $sinceDate), | ||
); | ||
/** | ||
* @psalm-suppress TooManyTemplateParams | ||
* | ||
* @var ResourceCursorInterface<array-key, AkeneoAttribute> $akeneoAttributes | ||
*/ | ||
$akeneoAttributes = $this->apiClient->getAttributeApi()->all(50, ['search' => $searchBuilder->getFilters()]); | ||
|
||
return array_merge( | ||
$this->filterBySyliusAttributeCodes($akeneoAttributes), | ||
$this->filterSyliusOptionCodes($akeneoAttributes), | ||
); | ||
} | ||
|
||
public function import(string $identifier): void | ||
{ | ||
/** @var AkeneoAttribute $akeneoAttribute */ | ||
$akeneoAttribute = $this->apiClient->getAttributeApi()->get($identifier); | ||
|
||
$syliusProductAttribute = $this->productAttributeRepository->findOneBy(['code' => $identifier]); | ||
if ($syliusProductAttribute instanceof ProductAttributeInterface) { | ||
$this->importAttributeData($akeneoAttribute, $syliusProductAttribute); | ||
} | ||
|
||
$syliusProductOption = $this->productOptionRepository->findOneBy(['code' => $identifier]); | ||
if ($syliusProductOption instanceof ProductOptionInterface) { | ||
$this->importOptionData($akeneoAttribute, $syliusProductOption); | ||
} | ||
} | ||
|
||
/** | ||
* @return FactoryInterface<ProductOptionTranslationInterface> | ||
*/ | ||
private function getProductOptionTranslationFactory(): FactoryInterface | ||
{ | ||
return $this->productOptionTranslationFactory; | ||
} | ||
|
||
private function getProductOptionRepository(): ProductOptionRepositoryInterface | ||
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.2, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.3, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.3, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.2, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.3, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.1, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.1, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.3, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.3, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.2, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.1, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.3, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.1, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.3, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.2, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.1, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.2, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.1, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.2, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.2, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.12.16, PHP 8.1, Symfony ^5.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.1, Symfony ^6.4, MySQL 8.0
Check failure on line 104 in src/Attribute/Importer.php GitHub Actions / Sylius 1.13.1, PHP 8.3, Symfony ^5.4, MySQL 8.0
|
||
{ | ||
return $this->productOptionRepository; | ||
} | ||
|
||
/** | ||
* @return RepositoryInterface<ProductAttributeInterface> | ||
*/ | ||
private function getProductAttributeRepository(): RepositoryInterface | ||
{ | ||
return $this->productAttributeRepository; | ||
} | ||
|
||
/** | ||
* @param AkeneoAttribute $akeneoAttribute | ||
*/ | ||
private function importAttributeData(array $akeneoAttribute, ProductAttributeInterface $syliusProductAttribute): void | ||
{ | ||
$this->importProductAttributeTranslations($akeneoAttribute, $syliusProductAttribute); | ||
$this->productAttributeRepository->add($syliusProductAttribute); | ||
} | ||
|
||
/** | ||
* @param AkeneoAttribute $akeneoAttribute | ||
*/ | ||
private function importOptionData(array $akeneoAttribute, ProductOptionInterface $syliusProductOption): void | ||
{ | ||
$this->importProductOptionTranslations($akeneoAttribute, $syliusProductOption); | ||
$this->productOptionRepository->add($syliusProductOption); | ||
// TODO: Update also the position of the option? The problem is that this position is on family variant entity! | ||
} | ||
|
||
/** | ||
* @param AkeneoAttribute $akeneoAttribute | ||
*/ | ||
private function importProductAttributeTranslations(array $akeneoAttribute, ProductAttributeInterface $syliusProductAttribute): void | ||
{ | ||
foreach ($akeneoAttribute['labels'] as $locale => $label) { | ||
$productAttributeTranslation = $syliusProductAttribute->getTranslation($locale); | ||
if ($productAttributeTranslation->getLocale() === $locale) { | ||
$productAttributeTranslation->setName($label); | ||
|
||
continue; | ||
} | ||
$newProductAttributeTranslation = $this->productAttributeTranslationFactory->createNew(); | ||
$newProductAttributeTranslation->setLocale($locale); | ||
$newProductAttributeTranslation->setName($label); | ||
$syliusProductAttribute->addTranslation($newProductAttributeTranslation); | ||
} | ||
} | ||
} |