-
Notifications
You must be signed in to change notification settings - Fork 967
/
CategoryIndexer.php
211 lines (170 loc) · 6.39 KB
/
CategoryIndexer.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php declare(strict_types=1);
namespace Shopware\Core\Content\Category\DataAbstractionLayer;
use Doctrine\DBAL\Connection;
use Shopware\Core\Content\Category\CategoryDefinition;
use Shopware\Core\Content\Category\Event\CategoryIndexerEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IterableQuery;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\ChildCountUpdater;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexer;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\TreeUpdater;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class CategoryIndexer extends EntityIndexer
{
public const CHILD_COUNT_UPDATER = 'category.child-count';
public const TREE_UPDATER = 'category.tree';
public const BREADCRUMB_UPDATER = 'category.breadcrumb';
/**
* @var IteratorFactory
*/
private $iteratorFactory;
/**
* @var Connection
*/
private $connection;
/**
* @var EntityRepositoryInterface
*/
private $repository;
/**
* @var ChildCountUpdater
*/
private $childCountUpdater;
/**
* @var TreeUpdater
*/
private $treeUpdater;
/**
* @var CategoryBreadcrumbUpdater
*/
private $breadcrumbUpdater;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
public function __construct(
Connection $connection,
IteratorFactory $iteratorFactory,
EntityRepositoryInterface $repository,
ChildCountUpdater $childCountUpdater,
TreeUpdater $treeUpdater,
CategoryBreadcrumbUpdater $breadcrumbUpdater,
EventDispatcherInterface $eventDispatcher
) {
$this->iteratorFactory = $iteratorFactory;
$this->repository = $repository;
$this->childCountUpdater = $childCountUpdater;
$this->treeUpdater = $treeUpdater;
$this->breadcrumbUpdater = $breadcrumbUpdater;
$this->connection = $connection;
$this->eventDispatcher = $eventDispatcher;
}
public function getName(): string
{
return 'category.indexer';
}
public function getTotal(): int
{
return $this->getIterator(null)->fetchCount();
}
/**
* @param array|null $offset
*
* @deprecated tag:v6.5.0 The parameter $offset will be native typed
*/
public function iterate(/*?array */$offset): ?EntityIndexingMessage
{
$iterator = $this->getIterator($offset);
$ids = $iterator->fetch();
if (empty($ids)) {
return null;
}
return new CategoryIndexingMessage(array_values($ids), $iterator->getOffset());
}
public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
{
$categoryEvent = $event->getEventByEntityName(CategoryDefinition::ENTITY_NAME);
if (!$categoryEvent) {
return null;
}
$ids = $categoryEvent->getIds();
$idsWithChangedParentIds = [];
foreach ($categoryEvent->getWriteResults() as $result) {
if (!$result->getExistence()) {
continue;
}
$state = $result->getExistence()->getState();
if (isset($state['parent_id'])) {
$ids[] = Uuid::fromBytesToHex($state['parent_id']);
}
$payload = $result->getPayload();
if (\array_key_exists('parentId', $payload)) {
if ($payload['parentId'] !== null) {
$ids[] = $payload['parentId'];
}
$idsWithChangedParentIds[] = $payload['id'];
}
}
if (empty($ids)) {
return null;
}
if ($idsWithChangedParentIds !== []) {
$this->treeUpdater->batchUpdate(
$idsWithChangedParentIds,
CategoryDefinition::ENTITY_NAME,
$event->getContext()
);
}
$children = $this->fetchChildren($ids, $event->getContext()->getVersionId());
$ids = array_unique(array_merge($ids, $children));
return new CategoryIndexingMessage(array_values($ids), null, $event->getContext(), \count($ids) > 20);
}
public function handle(EntityIndexingMessage $message): void
{
$ids = $message->getData();
$ids = array_unique(array_filter($ids));
if (empty($ids)) {
return;
}
$context = Context::createDefaultContext();
$this->connection->beginTransaction();
if ($message->allow(self::CHILD_COUNT_UPDATER)) {
// listen to parent id changes
$this->childCountUpdater->update(CategoryDefinition::ENTITY_NAME, $ids, $context);
}
if ($message->allow(self::TREE_UPDATER)) {
$this->treeUpdater->batchUpdate($ids, CategoryDefinition::ENTITY_NAME, $context);
}
if ($message->allow(self::BREADCRUMB_UPDATER)) {
// listen to name changes
$this->breadcrumbUpdater->update($ids, $context);
}
$this->connection->commit();
$this->eventDispatcher->dispatch(new CategoryIndexerEvent($ids, $context, $message->getSkip()));
}
private function fetchChildren(array $categoryIds, string $versionId): array
{
$query = $this->connection->createQueryBuilder();
$query->select('DISTINCT LOWER(HEX(category.id))');
$query->from('category');
$wheres = [];
foreach ($categoryIds as $id) {
$key = 'path' . $id;
$wheres[] = 'category.path LIKE :' . $key;
$query->setParameter($key, '%|' . $id . '|%');
}
$query->andWhere('(' . implode(' OR ', $wheres) . ')');
$query->andWhere('category.version_id = :version');
$query->setParameter('version', Uuid::fromHexToBytes($versionId));
return $query->execute()->fetchAll(\PDO::FETCH_COLUMN);
}
private function getIterator(?array $offset): IterableQuery
{
return $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
}
}