-
Notifications
You must be signed in to change notification settings - Fork 3
/
PropagateChanges.php
203 lines (171 loc) · 6.86 KB
/
PropagateChanges.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
<?php
declare(strict_types=1);
namespace Valantic\ElasticaBridgeBundle\Service;
use Elastica\Exception\NotFoundException;
use Elastica\Index;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\Element\AbstractElement;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Valantic\ElasticaBridgeBundle\Document\DocumentInterface;
use Valantic\ElasticaBridgeBundle\Enum\ElementInIndexOperation;
use Valantic\ElasticaBridgeBundle\Index\IndexInterface;
use Valantic\ElasticaBridgeBundle\Messenger\Message\RefreshElementInIndex;
use Valantic\ElasticaBridgeBundle\Model\Event\ElasticaBridgeEvents;
use Valantic\ElasticaBridgeBundle\Model\Event\RefreshedElementEvent;
use Valantic\ElasticaBridgeBundle\Model\Event\RefreshedElementInIndexEvent;
use Valantic\ElasticaBridgeBundle\Repository\IndexRepository;
class PropagateChanges
{
private static bool $isPropagationStopped = false;
public function __construct(
private readonly IndexRepository $indexRepository,
private readonly DocumentHelper $documentHelper,
private readonly MessageBusInterface $messageBus,
private readonly EventDispatcherInterface $eventDispatcher,
) {}
/**
* Whenever an event occurs, a decision needs to be made:
* 1. Which indices might need to be updated?
* 2. Does the element need to be in Elasticsearch or not?
* 3. Are there Elasticsearch documents to be created/updated or deleted?
*/
public function handle(AbstractElement $element): void
{
$indices = $this->matchingIndicesForElement($this->indexRepository->flattenedAll(), $element);
$event = new RefreshedElementEvent($element, $indices);
if (!self::$isPropagationStopped) {
$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::PRE_REFRESH_ELEMENT);
}
foreach ($indices as $index) {
$this->messageBus->dispatch(
new RefreshElementInIndex(
$element,
$index->getName(),
self::$isPropagationStopped || $event->isPropagationStopped()
)
);
}
if (!self::$isPropagationStopped && !$event->isPropagationStopped()) {
$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::POST_REFRESH_ELEMENT);
}
}
public function handleIndex(
AbstractElement $element,
IndexInterface $index,
?Index $elasticaIndex = null,
): void {
$this->doHandleIndex($element, $index, $elasticaIndex ?? $index->getElasticaIndex());
}
public static function stopPropagation(): void
{
self::$isPropagationStopped = true;
}
private function doHandleIndex(
AbstractElement $element,
IndexInterface $index,
Index $elasticaIndex,
): void {
// TODO: actually use $elasticaIndex
$document = $index->findDocumentInstanceByPimcore($element);
if (!$document instanceof DocumentInterface) {
return;
}
$this->documentHelper->setTenantIfNeeded($document, $index);
if (
!in_array($document::class, $index->subscribedDocuments(), true)
|| ($element->getType() === AbstractObject::OBJECT_TYPE_VARIANT && !$document->treatObjectVariantsAsDocuments())
) {
$this->documentHelper->resetTenantIfNeeded($document, $index);
return;
}
$isPresent = $this->isIdInIndex($document::getElasticsearchId($element), $elasticaIndex);
$shouldIndex = $document->shouldIndex($element);
$operation = match (true) {
!$isPresent && $shouldIndex => ElementInIndexOperation::INSERT,
$isPresent && $shouldIndex => ElementInIndexOperation::UPDATE,
$isPresent && !$shouldIndex => ElementInIndexOperation::DELETE,
default => ElementInIndexOperation::NOTHING,
};
$event = new RefreshedElementInIndexEvent($element, $index, $elasticaIndex, $operation);
if (!self::$isPropagationStopped) {
$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::PRE_REFRESH_ELEMENT_IN_INDEX);
}
match ($operation) {
ElementInIndexOperation::INSERT => $this->addElementToIndex($element, $elasticaIndex, $document),
ElementInIndexOperation::UPDATE => $this->updateElementInIndex($element, $elasticaIndex, $document),
ElementInIndexOperation::DELETE => $this->deleteElementFromIndex($element, $elasticaIndex, $document),
ElementInIndexOperation::NOTHING => null,
};
if (!self::$isPropagationStopped && !$event->isPropagationStopped()) {
$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::POST_REFRESH_ELEMENT_IN_INDEX);
}
$this->documentHelper->resetTenantIfNeeded($document, $index);
}
/**
* @param DocumentInterface<AbstractElement> $document
*/
private function addElementToIndex(
AbstractElement $element,
Index $index,
DocumentInterface $document,
): void {
$elasticaDocument = $this->documentHelper->elementToDocument($document, $element);
$index->addDocument($elasticaDocument);
}
/**
* @param DocumentInterface<AbstractElement> $document
*/
private function updateElementInIndex(
AbstractElement $element,
Index $index,
DocumentInterface $document,
): void {
$elasticaDocument = $this->documentHelper->elementToDocument($document, $element);
// updateDocument() allows partial updates, hence the full replace here
$index->addDocument($elasticaDocument);
}
/**
* @param DocumentInterface<AbstractElement> $document
*/
private function deleteElementFromIndex(
AbstractElement $element,
Index $index,
DocumentInterface $document,
): void {
$elasticsearchId = $document::getElasticsearchId($element);
$index->deleteById($elasticsearchId);
}
/**
* Returns an array of indices that could contain $element.
*
* @param \Generator<string,IndexInterface,void,void> $indices
*
* @return IndexInterface[]
*/
private function matchingIndicesForElement(
\Generator $indices,
AbstractElement $element,
): array {
$matching = [];
foreach ($indices as $index) {
/** @var IndexInterface $index */
if ($index->isElementAllowedInIndex($element)) {
$matching[] = $index;
}
}
return $matching;
}
/**
* Checks whether a given ID is in an index.
*/
private function isIdInIndex(string $id, Index $index): bool
{
try {
$index->getDocument($id);
} catch (NotFoundException) {
return false;
}
return true;
}
}