Skip to content

Commit

Permalink
refacto filters
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGrimmChester committed Jun 13, 2024
1 parent da0450d commit 1ee96bc
Show file tree
Hide file tree
Showing 44 changed files with 822 additions and 250 deletions.
1 change: 1 addition & 0 deletions src/Command/AbstractImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected function configure(): void
->addOption('parallel', 'p', InputOption::VALUE_NONE, 'Allow parallel task processing')
->addOption('disable-batch', 'd', InputOption::VALUE_NONE, 'Disable batch processing')
->addOption('batch-size', 's', InputOption::VALUE_OPTIONAL, 'Batch Size', 100)
->addOption('from-page', null, InputOption::VALUE_OPTIONAL, 'From page', 1)
->addOption('max-concurrency', 'c', InputOption::VALUE_OPTIONAL, 'Max process concurrency', 5)
->addOption('batch-after-fetch', 'a', InputOption::VALUE_OPTIONAL, 'Fetch all pages then start processing the batches', true)
->addOption('filter', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Add filter')
Expand Down
48 changes: 48 additions & 0 deletions src/Command/BatchImportCategoriesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\Command;

use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Synolia\SyliusAkeneoPlugin\Client\ClientFactoryInterface;
use Synolia\SyliusAkeneoPlugin\Payload\Category\CategoryPayload;
use Synolia\SyliusAkeneoPlugin\Task\Category\BatchCategoriesTask;

final class BatchImportCategoriesCommand extends AbstractBatchCommand
{
protected static $defaultDescription = 'Import batch categories ids from Akeneo PIM.';

/** @var string */
protected static $defaultName = 'akeneo:batch:categories';

public function __construct(
private ClientFactoryInterface $clientFactory,
private LoggerInterface $logger,
private BatchCategoriesTask $task,
) {
parent::__construct(self::$defaultName);
}

/**
* {@inheritdoc}
*/
protected function execute(
InputInterface $input,
OutputInterface $output,
) {
$ids = explode(',', $input->getArgument('ids'));

Check failure on line 36 in src/Command/BatchImportCategoriesCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Parameter #2 $string of function explode expects string, mixed given.

Check failure on line 36 in src/Command/BatchImportCategoriesCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Parameter #2 $string of function explode expects string, mixed given.

Check failure on line 36 in src/Command/BatchImportCategoriesCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Parameter #2 $string of function explode expects string, mixed given.

Check failure on line 36 in src/Command/BatchImportCategoriesCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Parameter #2 $string of function explode expects string, mixed given.

$this->logger->notice('Processing batch', ['from_id' => $ids[0], 'to_id' => $ids[\count($ids) - 1]]);
$this->logger->debug(self::$defaultName, ['batched_ids' => $ids]);

$payload = new CategoryPayload($this->clientFactory->createFromApiCredentials());
$payload->setIds($ids);

$this->task->__invoke($payload);

Check failure on line 44 in src/Command/BatchImportCategoriesCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Parameter #1 $payload of method Synolia\SyliusAkeneoPlugin\Task\Category\BatchCategoriesTask::__invoke() expects Synolia\SyliusAkeneoPlugin\Payload\Attribute\AttributePayload, Synolia\SyliusAkeneoPlugin\Payload\Category\CategoryPayload given.

Check failure on line 44 in src/Command/BatchImportCategoriesCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Parameter #1 $payload of method Synolia\SyliusAkeneoPlugin\Task\Category\BatchCategoriesTask::__invoke() expects Synolia\SyliusAkeneoPlugin\Payload\Attribute\AttributePayload, Synolia\SyliusAkeneoPlugin\Payload\Category\CategoryPayload given.

Check failure on line 44 in src/Command/BatchImportCategoriesCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Parameter #1 $payload of method Synolia\SyliusAkeneoPlugin\Task\Category\BatchCategoriesTask::__invoke() expects Synolia\SyliusAkeneoPlugin\Payload\Attribute\AttributePayload, Synolia\SyliusAkeneoPlugin\Payload\Category\CategoryPayload given.

Check failure on line 44 in src/Command/BatchImportCategoriesCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Parameter #1 $payload of method Synolia\SyliusAkeneoPlugin\Task\Category\BatchCategoriesTask::__invoke() expects Synolia\SyliusAkeneoPlugin\Payload\Attribute\AttributePayload, Synolia\SyliusAkeneoPlugin\Payload\Category\CategoryPayload given.

return 0;
}
}
4 changes: 4 additions & 0 deletions src/Configuration/ConfigurationContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ public function setFilters(array $filters): self;
public function getHandler(): string;

public function setHandler(string $handler): self;

public function getFromPage(): int;

public function setFromPage(int $fromPage): self;
}
14 changes: 14 additions & 0 deletions src/Configuration/ConfigurationContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ trait ConfigurationContextTrait

private string $handler = SymfonyProcessTaskHandler::HANDLER_CODE;

private int $fromPage = 1;

public function getBatchSize(): int
{
return $this->batchSize;
Expand Down Expand Up @@ -147,4 +149,16 @@ public function setHandler(string $handler): ConfigurationContextInterface

return $this;
}

public function getFromPage(): int
{
return $this->fromPage;
}

public function setFromPage(int $fromPage): ConfigurationContextInterface
{
$this->fromPage = $fromPage;

return $this;
}
}
4 changes: 2 additions & 2 deletions src/Factory/CategoryPipelineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use League\Pipeline\PipelineInterface;
use Synolia\SyliusAkeneoPlugin\Pipeline\Processor;
use Synolia\SyliusAkeneoPlugin\Task\Category\CreateUpdateEntityTask;
use Synolia\SyliusAkeneoPlugin\Task\Category\RetrieveCategoriesTask;
use Synolia\SyliusAkeneoPlugin\Task\Category\ProcessCategoriesTask;

final class CategoryPipelineFactory extends AbstractPipelineFactory
{
Expand All @@ -17,7 +17,7 @@ public function create(): PipelineInterface
$pipeline = new Pipeline(new Processor($this->dispatcher));

return $pipeline
->pipe($this->taskProvider->get(RetrieveCategoriesTask::class))
->pipe($this->taskProvider->get(ProcessCategoriesTask::class))
->pipe($this->taskProvider->get(CreateUpdateEntityTask::class))

Check failure on line 21 in src/Factory/CategoryPipelineFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Class Synolia\SyliusAkeneoPlugin\Task\Category\CreateUpdateEntityTask not found.

Check failure on line 21 in src/Factory/CategoryPipelineFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Class Synolia\SyliusAkeneoPlugin\Task\Category\CreateUpdateEntityTask not found.

Check failure on line 21 in src/Factory/CategoryPipelineFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Class Synolia\SyliusAkeneoPlugin\Task\Category\CreateUpdateEntityTask not found.

Check failure on line 21 in src/Factory/CategoryPipelineFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Class Synolia\SyliusAkeneoPlugin\Task\Category\CreateUpdateEntityTask not found.
;
}
Expand Down
1 change: 1 addition & 0 deletions src/Factory/PayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private function createContext(
->setBatchingAllowed($isBatchingAllowed)
->setProcessAsSoonAsPossible(filter_var($batchAfterFetch, FILTER_VALIDATE_BOOLEAN))
->setBatchSize((int) $input->getOption('batch-size'))

Check failure on line 53 in src/Factory/PayloadFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Ignored error pattern #^Cannot cast mixed to int\.$# in path /home/runner/work/SyliusAkeneoPlugin/SyliusAkeneoPlugin/src/Factory/PayloadFactory.php is expected to occur 2 times, but occurred 3 times.

Check failure on line 53 in src/Factory/PayloadFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Ignored error pattern #^Cannot cast mixed to int\.$# in path /home/runner/work/SyliusAkeneoPlugin/SyliusAkeneoPlugin/src/Factory/PayloadFactory.php is expected to occur 2 times, but occurred 3 times.

Check failure on line 53 in src/Factory/PayloadFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Ignored error pattern #^Cannot cast mixed to int\.$# in path /home/runner/work/SyliusAkeneoPlugin/SyliusAkeneoPlugin/src/Factory/PayloadFactory.php is expected to occur 2 times, but occurred 3 times.

Check failure on line 53 in src/Factory/PayloadFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Ignored error pattern #^Cannot cast mixed to int\.$# in path /home/runner/work/SyliusAkeneoPlugin/SyliusAkeneoPlugin/src/Factory/PayloadFactory.php is expected to occur 2 times, but occurred 3 times.
->setFromPage((int) $input->getOption('from-page'))
->setMaxRunningProcessQueueSize((int) $input->getOption('max-concurrency'))

Check failure on line 55 in src/Factory/PayloadFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Cannot cast mixed to int.

Check failure on line 55 in src/Factory/PayloadFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Cannot cast mixed to int.

Check failure on line 55 in src/Factory/PayloadFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Cannot cast mixed to int.

Check failure on line 55 in src/Factory/PayloadFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Cannot cast mixed to int.
->setFilters((array) ($input->getOption('filter') ?: []))
->setHandler($input->getOption('handler') ?? $context->getHandler())
Expand Down
5 changes: 2 additions & 3 deletions src/Handler/Task/SymfonyMessengerTaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Akeneo\Pim\ApiClient\Pagination\Page;
use Akeneo\Pim\ApiClient\Pagination\PageInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\MessageBusInterface;
Expand Down Expand Up @@ -37,7 +36,7 @@ public function batch(PipelinePayloadInterface $pipelinePayload, array $items):

public function handle(
PipelinePayloadInterface $pipelinePayload,
ResourceCursorInterface|PageInterface $handleType,
iterable|PageInterface $handleType,
): void {
$count = 0;
$items = [];
Expand Down Expand Up @@ -83,7 +82,7 @@ private function handleByPage(

private function handleByCursor(
PipelinePayloadInterface $payload,
ResourceCursorInterface $resourceCursor,
iterable $resourceCursor,
int &$count = 0,
array &$items = [],
): void {
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/Task/SymfonyProcessTaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function continue(PipelinePayloadInterface $pipelinePayload): void
*/
public function handle(
PipelinePayloadInterface $pipelinePayload,
ResourceCursorInterface|PageInterface $handleType,
iterable|PageInterface $handleType,
): void {
$this->processManager->setInstantProcessing($pipelinePayload->getProcessAsSoonAsPossible());
$this->processManager->setNumberOfParallelProcesses($pipelinePayload->getMaxRunningProcessQueueSize());
Expand Down
3 changes: 1 addition & 2 deletions src/Handler/Task/TaskHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Synolia\SyliusAkeneoPlugin\Handler\Task;

use Akeneo\Pim\ApiClient\Pagination\PageInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface;

Expand All @@ -25,7 +24,7 @@ public function batch(

public function handle(
PipelinePayloadInterface $pipelinePayload,
ResourceCursorInterface|PageInterface $handleType,
iterable|PageInterface $handleType,
): void;

public function continue(PipelinePayloadInterface $pipelinePayload): void;
Expand Down
12 changes: 12 additions & 0 deletions src/Message/Batch/CategoryBatchMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\Message\Batch;

class CategoryBatchMessage implements BatchMessageInterface
{
public function __construct(public array $items)
{
}
}
35 changes: 35 additions & 0 deletions src/MessageHandler/Batch/CategoryBatchMessageHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\MessageHandler\Batch;

use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Synolia\SyliusAkeneoPlugin\Message\Batch\CategoryBatchMessage;
use Synolia\SyliusAkeneoPlugin\Processor\Resource\Category\CategoryResourceProcessor;
use Synolia\SyliusAkeneoPlugin\Processor\Resource\Exception\MaxResourceProcessorRetryException;

#[AsMessageHandler]
class CategoryBatchMessageHandler
{
public function __construct(
private EventDispatcherInterface $dispatcher,
private CategoryResourceProcessor $resourceProcessor,
) {
}

public function __invoke(CategoryBatchMessage $attributeBatchMessage): void
{
foreach ($attributeBatchMessage->items as $resource) {
try {
$this->resourceProcessor->process($resource);
} catch (MaxResourceProcessorRetryException) {
// Skip the failing line
$this->dispatcher->dispatch(new CategoryBatchMessage([$resource]));

continue;
}
}
}
}
1 change: 1 addition & 0 deletions src/Payload/AbstractPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(
$this->isContinue = $commandContext->isContinue();
$this->processAsSoonAsPossible = $commandContext->getProcessAsSoonAsPossible();
$this->handler = $commandContext->getHandler();
$this->fromPage = $commandContext->getFromPage();
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/Payload/Category/CategoryPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@

namespace Synolia\SyliusAkeneoPlugin\Payload\Category;

use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Synolia\SyliusAkeneoPlugin\Command\Context\CommandContextInterface;
use Synolia\SyliusAkeneoPlugin\Exceptions\NoCategoryResourcesException;
use Synolia\SyliusAkeneoPlugin\Message\Batch\BatchMessageInterface;
use Synolia\SyliusAkeneoPlugin\Message\Batch\CategoryBatchMessage;
use Synolia\SyliusAkeneoPlugin\Payload\AbstractPayload;

final class CategoryPayload extends AbstractPayload
{
public const TEMP_AKENEO_TABLE_NAME = 'tmp_akeneo_categories';

public const BATCH_COMMAND_NAME = 'akeneo:batch:categories';

public function __construct(
AkeneoPimClientInterface $akeneoPimClient,
?CommandContextInterface $commandContext = null,
) {
parent::__construct($akeneoPimClient, $commandContext);

$this->setTmpTableName(self::TEMP_AKENEO_TABLE_NAME);
$this->setCommandName(self::BATCH_COMMAND_NAME);
}

private array $resources;

public function getResources(): array
Expand All @@ -28,6 +45,6 @@ public function setResources(array $resources): void

public function createBatchMessage(array $items): BatchMessageInterface
{
throw new \InvalidArgumentException();
return new CategoryBatchMessage($items);
}
}
Loading

0 comments on commit 1ee96bc

Please sign in to comment.