-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathProcessProductsTask.php
157 lines (127 loc) · 5.9 KB
/
ProcessProductsTask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
namespace Synolia\SyliusAkeneoPlugin\Task\Product;
use Akeneo\Pim\ApiClient\Pagination\Page;
use Akeneo\Pim\ApiClient\Pagination\PageInterface;
use Doctrine\DBAL\ParameterType;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Synolia\SyliusAkeneoPlugin\Event\FilterEvent;
use Synolia\SyliusAkeneoPlugin\Exceptions\Payload\CommandContextIsNullException;
use Synolia\SyliusAkeneoPlugin\Filter\ProductFilterInterface;
use Synolia\SyliusAkeneoPlugin\Logger\Messages;
use Synolia\SyliusAkeneoPlugin\Manager\ProcessManagerInterface;
use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface;
use Synolia\SyliusAkeneoPlugin\Payload\Product\ProductPayload;
use Synolia\SyliusAkeneoPlugin\Provider\Configuration\Api\ApiConnectionProviderInterface;
use Synolia\SyliusAkeneoPlugin\Task\AbstractProcessTask;
final class ProcessProductsTask extends AbstractProcessTask
{
public function __construct(
EntityManagerInterface $entityManager,
LoggerInterface $akeneoLogger,
ProcessManagerInterface $processManager,
BatchProductsTask $task,
private ProductFilterInterface $productFilter,
private ApiConnectionProviderInterface $apiConnectionProvider,
private EventDispatcherInterface $eventDispatcher,
string $projectDir,
) {
parent::__construct($entityManager, $processManager, $task, $akeneoLogger, $projectDir);
}
/**
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
* @TODO Probably need to be refactored
*
* @param ProductPayload $payload
*/
public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInterface
{
$this->logger->debug(self::class);
if ($payload->isContinue()) {
$this->process($payload);
return $payload;
}
$this->logger->notice(Messages::retrieveFromAPI($payload->getType()));
$queryParameters = $this->productFilter->getProductFilters();
$queryParameters['pagination_type'] = 'search_after';
try {
$event = new FilterEvent($payload->getCommandContext());
$this->eventDispatcher->dispatch($event);
$queryParameters['search'] = array_merge($queryParameters['search'] ?? [], $event->getFilters());
} catch (CommandContextIsNullException) {
}
$queryParameters = array_merge_recursive($queryParameters, $payload->getCustomFilters());
$this->logger->notice('Filters', $queryParameters);
/** @var \Akeneo\Pim\ApiClient\Pagination\PageInterface|null $resources */
$resources = $payload->getAkeneoPimClient()->getProductApi()->listPerPage(
$this->apiConnectionProvider->get()->getPaginationSize(),
true,
$queryParameters,
);
if (!$resources instanceof Page) {
return $payload;
}
$count = 0;
$ids = [];
$this->handleProducts($payload, $resources, $count, $ids);
if ($count > 0 && count($ids) > 0 && $payload->isBatchingAllowed() && $payload->getProcessAsSoonAsPossible() && $payload->allowParallel()) {
$this->logger->notice('Batching', ['from_id' => $ids[0], 'to_id' => $ids[(is_countable($ids) ? \count($ids) : 0) - 1]]);
$this->batch($payload, $ids);
}
if ($count > 0 && count($ids) > 0 && $payload->isBatchingAllowed() && $payload->getProcessAsSoonAsPossible() && !$payload->allowParallel()) {
$payload->setIds($ids);
$this->task->__invoke($payload);
}
if ($count > 0 && count($ids) > 0 && !$payload->isBatchingAllowed()) {
$payload->setIds($ids);
$this->task->__invoke($payload);
}
if ($count > 0 && !$payload->getProcessAsSoonAsPossible()) {
$this->process($payload);
}
$this->processManager->startAll();
return $payload;
}
private function handleProducts(
PipelinePayloadInterface $payload,
PageInterface $page,
int &$count = 0,
array &$ids = [],
): void {
$this->processManager->setInstantProcessing($payload->getProcessAsSoonAsPossible());
$this->processManager->setNumberOfParallelProcesses($payload->getMaxRunningProcessQueueSize());
while (
($page instanceof Page && $page->hasNextPage()) ||
($page instanceof Page && !$page->hasPreviousPage()) ||
$page instanceof Page
) {
foreach ($page->getItems() as $item) {
$sql = sprintf(
'INSERT INTO `%s` (`values`, `is_simple`) VALUES (:values, :is_simple);',
ProductPayload::TEMP_AKENEO_TABLE_NAME,
);
$stmt = $this->entityManager->getConnection()->prepare($sql);
$stmt->bindValue('values', json_encode($item, \JSON_THROW_ON_ERROR));
$stmt->bindValue('is_simple', null === $item['parent'], ParameterType::BOOLEAN);
$stmt->execute();
++$count;
$ids[] = $this->entityManager->getConnection()->lastInsertId();
if ($payload->getProcessAsSoonAsPossible() && $payload->allowParallel() && 0 === $count % $payload->getBatchSize()) {
$this->logger->notice('Batching', ['from_id' => $ids[0], 'to_id' => $ids[\count($ids) - 1]]);
$this->batch($payload, $ids);
$ids = [];
}
}
$page = $page->getNextPage();
}
}
protected function createBatchPayload(PipelinePayloadInterface $payload): PipelinePayloadInterface
{
$commandContext = ($payload->hasCommandContext()) ? $payload->getCommandContext() : null;
return new ProductPayload($payload->getAkeneoPimClient(), $commandContext);
}
}