-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from synolia/feature/import-categorie-positions
Import category position
- Loading branch information
Showing
26 changed files
with
545 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Synolia\SyliusAkeneoPlugin\Manager\Doctrine; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Gedmo\Sortable\SortableListener; | ||
|
||
class DoctrineSortableManager | ||
{ | ||
private array $originalEventListeners = []; | ||
|
||
public function __construct(private EntityManagerInterface $entityManager) | ||
{ | ||
} | ||
|
||
public function disableSortableEventListener(): void | ||
{ | ||
foreach ($this->entityManager->getEventManager()->getListeners() as $eventName => $listeners) { | ||
foreach ($listeners as $listener) { | ||
if ($listener instanceof SortableListener) { | ||
$this->originalEventListeners[$eventName] = $listener; | ||
$this->entityManager->getEventManager()->removeEventListener($eventName, $listener); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public function enableSortableEventListener(): void | ||
{ | ||
if ($this->originalEventListeners === []) { | ||
return; | ||
} | ||
|
||
foreach ($this->originalEventListeners as $eventName => $listener) { | ||
$this->entityManager->getEventManager()->addEventListener($eventName, $listener); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Synolia\SyliusAkeneoPlugin\Migrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version20231106080715 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Added useAkeneoPositions column'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE akeneo_api_configuration_categories ADD useAkeneoPositions TINYINT(1) NOT NULL'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE akeneo_api_configuration_categories DROP useAkeneoPositions'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Synolia\SyliusAkeneoPlugin\Processor\Category; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Sylius\Component\Core\Model\TaxonInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Synolia\SyliusAkeneoPlugin\Builder\TaxonAttribute\TaxonAttributeValueBuilder; | ||
use Synolia\SyliusAkeneoPlugin\Component\TaxonAttribute\Model\TaxonAttributeSubjectInterface; | ||
use Synolia\SyliusAkeneoPlugin\Entity\TaxonAttribute; | ||
use Synolia\SyliusAkeneoPlugin\Entity\TaxonAttributeInterface; | ||
use Synolia\SyliusAkeneoPlugin\Entity\TaxonAttributeValueInterface; | ||
use Synolia\SyliusAkeneoPlugin\Exceptions\UnsupportedAttributeTypeException; | ||
use Synolia\SyliusAkeneoPlugin\TypeMatcher\TaxonAttribute\TaxonAttributeTypeMatcher; | ||
use Webmozart\Assert\Assert; | ||
|
||
class AttributeProcessor implements CategoryProcessorInterface | ||
{ | ||
private array $taxonAttributes = []; | ||
|
||
private array $taxonAttributeValues = []; | ||
|
||
public static function getDefaultPriority(): int | ||
{ | ||
return 700; | ||
} | ||
|
||
public function __construct( | ||
private LoggerInterface $logger, | ||
private EntityManagerInterface $entityManager, | ||
private RepositoryInterface $taxonAttributeRepository, | ||
private RepositoryInterface $taxonAttributeValueRepository, | ||
private FactoryInterface $taxonAttributeFactory, | ||
private FactoryInterface $taxonAttributeValueFactory, | ||
private TaxonAttributeTypeMatcher $taxonAttributeTypeMatcher, | ||
private TaxonAttributeValueBuilder $taxonAttributeValueBuilder, | ||
) { | ||
} | ||
|
||
public function process(TaxonInterface $taxon, array $resource): void | ||
{ | ||
foreach ($resource['values'] as $attributeValue) { | ||
try { | ||
$taxonAttribute = $this->getTaxonAttributes( | ||
$attributeValue['attribute_code'], | ||
$attributeValue['type'], | ||
); | ||
|
||
$taxonAttributeValue = $this->getTaxonAttributeValues( | ||
$taxon, | ||
$taxonAttribute, | ||
$attributeValue['locale'], | ||
); | ||
|
||
$value = $this->taxonAttributeValueBuilder->build( | ||
$attributeValue['attribute_code'], | ||
$attributeValue['type'], | ||
$attributeValue['locale'], | ||
$attributeValue['channel'], | ||
$attributeValue['data'], | ||
); | ||
|
||
$taxonAttributeValue->setValue($value); | ||
} catch (UnsupportedAttributeTypeException $e) { | ||
$this->logger->warning($e->getMessage(), [ | ||
'trace' => $e->getTrace(), | ||
'exception' => $e, | ||
]); | ||
} | ||
} | ||
} | ||
|
||
public function support(TaxonInterface $taxon, array $resource): bool | ||
{ | ||
return $taxon instanceof TaxonAttributeSubjectInterface && array_key_exists('values', $resource); | ||
} | ||
|
||
private function getTaxonAttributes(string $attributeCode, string $type): TaxonAttributeInterface | ||
{ | ||
if (array_key_exists($attributeCode, $this->taxonAttributes)) { | ||
return $this->taxonAttributes[$attributeCode]; | ||
} | ||
|
||
$taxonAttribute = $this->taxonAttributeRepository->findOneBy(['code' => $attributeCode]); | ||
|
||
if ($taxonAttribute instanceof TaxonAttribute) { | ||
$this->taxonAttributes[$attributeCode] = $taxonAttribute; | ||
|
||
return $taxonAttribute; | ||
} | ||
|
||
$matcher = $this->taxonAttributeTypeMatcher->match($type); | ||
|
||
/** @var TaxonAttributeInterface $taxonAttribute */ | ||
$taxonAttribute = $this->taxonAttributeFactory->createNew(); | ||
$taxonAttribute->setCode($attributeCode); | ||
$taxonAttribute->setType($type); | ||
$taxonAttribute->setStorageType($matcher->getAttributeType()->getStorageType()); | ||
$taxonAttribute->setTranslatable(false); | ||
|
||
$this->entityManager->persist($taxonAttribute); | ||
$this->taxonAttributes[$attributeCode] = $taxonAttribute; | ||
|
||
return $taxonAttribute; | ||
} | ||
|
||
private function getTaxonAttributeValues( | ||
TaxonInterface $taxon, | ||
TaxonAttributeInterface $taxonAttribute, | ||
?string $locale, | ||
): TaxonAttributeValueInterface { | ||
Assert::string($taxon->getCode()); | ||
Assert::string($taxonAttribute->getCode()); | ||
|
||
if ( | ||
array_key_exists($taxon->getCode(), $this->taxonAttributeValues) && | ||
array_key_exists($taxonAttribute->getCode(), $this->taxonAttributeValues[$taxon->getCode()]) && | ||
array_key_exists($locale ?? 'unknown', $this->taxonAttributeValues[$taxon->getCode()][$taxonAttribute->getCode()]) | ||
) { | ||
return $this->taxonAttributeValues[$taxon->getCode()][$taxonAttribute->getCode()][$locale ?? 'unknown']; | ||
} | ||
|
||
$taxonAttributeValue = $this->taxonAttributeValueRepository->findOneBy([ | ||
'subject' => $taxon, | ||
'attribute' => $taxonAttribute, | ||
'localeCode' => $locale, | ||
]); | ||
|
||
if ($taxonAttributeValue instanceof TaxonAttributeValueInterface) { | ||
$this->taxonAttributeValues[$taxon->getCode()][$taxonAttribute->getCode()][$locale ?? 'unknown'] = $taxonAttributeValue; | ||
|
||
return $taxonAttributeValue; | ||
} | ||
|
||
/** @var TaxonAttributeValueInterface $taxonAttributeValue */ | ||
$taxonAttributeValue = $this->taxonAttributeValueFactory->createNew(); | ||
$taxonAttributeValue->setAttribute($taxonAttribute); | ||
$taxonAttributeValue->setTaxon($taxon); | ||
$taxonAttributeValue->setLocaleCode($locale); | ||
$this->entityManager->persist($taxonAttributeValue); | ||
|
||
$this->taxonAttributeValues[$taxon->getCode()][$taxonAttribute->getCode()][$locale ?? 'unknown'] = $taxonAttributeValue; | ||
|
||
return $taxonAttributeValue; | ||
} | ||
} |
Oops, something went wrong.