Skip to content

Commit

Permalink
Product Group processing deported to product models import
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGrimmChester committed Nov 2, 2023
1 parent c4917ac commit d8f2a20
Show file tree
Hide file tree
Showing 22 changed files with 194 additions and 473 deletions.
1 change: 0 additions & 1 deletion docs/LAUNCH.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ and add one of

akeneo:import:categories Import Categories from Akeneo PIM.
akeneo:import:attributes Import Attributes and Options from Akeneo PIM.
akeneo:import:families Import product's families from Akeneo PIM.
akeneo:import:association-type Import Associations type from Akeneo PIM.
akeneo:import:product-models Import Product Models from Akeneo PIM.
akeneo:import:products Import Products from Akeneo PIM.
Expand Down
48 changes: 0 additions & 48 deletions src/Command/BatchImportFamiliesCommand.php

This file was deleted.

54 changes: 0 additions & 54 deletions src/Command/ImportFamiliesCommand.php

This file was deleted.

18 changes: 18 additions & 0 deletions src/Component/Cache/CacheKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\Component\Cache;

class CacheKey
{
public const FAMILIES = 'akeneo:families';

public const FAMILY = 'akeneo:family:%s';

public const FAMILY_BY_VARIANT_CODE = 'akeneo:family_by_variant_code:%s';

public const FAMILY_VARIANTS = 'akeneo:family_variants:%s';

public const ATTRIBUTES = 'akeneo:attributes';
}
26 changes: 0 additions & 26 deletions src/Factory/FamilyPipelineFactory.php

This file was deleted.

70 changes: 0 additions & 70 deletions src/Fixture/FamiliesFixture.php

This file was deleted.

39 changes: 0 additions & 39 deletions src/Payload/Family/FamilyPayload.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,47 @@

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\Task\Family;
namespace Synolia\SyliusAkeneoPlugin\Processor\ProductGroup;

use Doctrine\DBAL\Result;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Synolia\SyliusAkeneoPlugin\Entity\ProductGroup;
use Synolia\SyliusAkeneoPlugin\Entity\ProductGroupInterface;
use Synolia\SyliusAkeneoPlugin\Payload\Family\FamilyPayload;
use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface;
use Synolia\SyliusAkeneoPlugin\Processor\ProductGroup\FamilyVariationAxeProcessor;
use Synolia\SyliusAkeneoPlugin\Task\AbstractBatchTask;

final class BatchFamilyTask extends AbstractBatchTask
class ProductGroupProcessor
{
private int $groupAlreadyExistCount = 0;

private int $groupCreateCount = 0;

private array $productGroupsMapping;

public function __construct(
EntityManagerInterface $entityManager,
private EntityRepository $productGroupRepository,
private EntityManagerInterface $entityManager,
private LoggerInterface $logger,
private FamilyVariationAxeProcessor $familyVariationAxeProcessor,
private EntityRepository $productGroupRepository,
private FactoryInterface $productGroupFactory,
) {
parent::__construct($entityManager);
}

/**
* @param FamilyPayload $payload
*/
public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInterface
public function process(array $resource): void
{
$this->logger->debug(self::class);
$this->productGroupsMapping = [];
$resources = [];

$query = $this->getSelectStatement($payload);
/** @var Result $queryResult */
$queryResult = $query->executeQuery();

while ($results = $queryResult->fetchAll()) {
foreach ($results as $result) {
try {
$resource = json_decode($result['values'], true, 512, \JSON_THROW_ON_ERROR);
$resources[] = $resource;

$this->createProductGroups($resource);
$this->removeEntry($payload, (int) $result['id']);
} catch (\Throwable $throwable) {
$this->logger->warning($throwable->getMessage());
$this->removeEntry($payload, (int) $result['id']);
}
}
}
$this->entityManager->flush();

foreach ($resources as $resource) {
$this->familyVariationAxeProcessor->process($resource);
}
$this->entityManager->flush();

return $payload;
$this->createProductGroups($resource);
$this->familyVariationAxeProcessor->process($resource);
}

private function createGroupForCodeAndFamily(
string $code,
string $family,
string $familyVariant,
?string $parent = null,
): ProductGroupInterface {
): void {
if (isset($this->productGroupsMapping[$code])) {
return $this->productGroupsMapping[$code];
return;
}

$productGroup = $this->productGroupRepository->findOneBy(['model' => $code]);
if ($productGroup instanceof ProductGroup) {

if ($productGroup instanceof ProductGroupInterface) {
$this->productGroupsMapping[$code] = $productGroup;
++$this->groupAlreadyExistCount;

$this->logger->info(sprintf(
'Skipping ProductGroup "%s" for family "%s" as it already exists.',
Expand All @@ -96,8 +54,9 @@ private function createGroupForCodeAndFamily(
$productGroup->setModel($code);
$productGroup->setFamily($family);
$productGroup->setFamilyVariant($familyVariant);
$this->entityManager->persist($productGroup);

return $productGroup;
return;
}

$this->logger->info(sprintf(
Expand All @@ -114,10 +73,6 @@ private function createGroupForCodeAndFamily(
$productGroup->setFamilyVariant($familyVariant);
$this->entityManager->persist($productGroup);
$this->productGroupsMapping[$code] = $productGroup;

++$this->groupCreateCount;

return $productGroup;
}

private function createProductGroups(array $resource): void
Expand Down
Loading

0 comments on commit d8f2a20

Please sign in to comment.