Skip to content

Commit

Permalink
allow to supress events
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebi committed Mar 22, 2024
1 parent d834048 commit dcfeba7
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/Messenger/Handler/RefreshElementHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function __invoke(RefreshElement $message): void
{
$element = $this->resolveElement($message);

if (!$message->shouldTriggerEvents()) {
PropagateChanges::disableTriggerEvents();
}

$this->propagateChanges->handle($element);
}
}
4 changes: 4 additions & 0 deletions src/Messenger/Handler/RefreshElementInIndexHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function __invoke(RefreshElementInIndex $message): void
$index = $this->indexRepository->flattenedGet($message->index);
$element = $this->resolveElement($message);

if (!$message->shouldTriggerEvents()) {
PropagateChanges::disableTriggerEvents();
}

if ($index->usesBlueGreenIndices() && !$this->lockService->getIndexingLock($index)->acquire()) {
$this->propagateChanges->handleIndex($element, $index, $index->getBlueGreenInactiveElasticaIndex());
}
Expand Down
7 changes: 6 additions & 1 deletion src/Messenger/Message/RefreshElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@

class RefreshElement extends AbstractRefresh
{
public function __construct(ElementInterface $element)
public function __construct(ElementInterface $element, private readonly bool $shouldTriggerEvents = true)
{
$this->setElement($element);
}

public function shouldTriggerEvents(): bool
{
return $this->shouldTriggerEvents;
}
}
6 changes: 6 additions & 0 deletions src/Messenger/Message/RefreshElementInIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ class RefreshElementInIndex extends AbstractRefresh
public function __construct(
ElementInterface $element,
public readonly string $index,
private readonly bool $shouldTriggerEvents = true,
) {
$this->setElement($element);
}

public function shouldTriggerEvents(): bool
{
return $this->shouldTriggerEvents;
}
}
19 changes: 16 additions & 3 deletions src/Model/Event/RefreshedElementEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@ class RefreshedElementEvent extends Event
{
/**
* @param AbstractElement $element
* @param array<IndexInterface> $index
* @param array<IndexInterface> $indices
*/
public function __construct(
public readonly AbstractElement $element,
public readonly array $index,
private readonly AbstractElement $element,
private readonly array $indices,
) {}

public function getElement(): AbstractElement
{
return $this->element;
}

/**
* @return array<IndexInterface>
*/
public function getIndices(): array
{
return $this->indices;
}
}
20 changes: 20 additions & 0 deletions src/Model/Event/RefreshedElementInIndexEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,24 @@ public function __construct(
public readonly Index $elasticaIndex,
public readonly Operation $operation,
) {}

public function getElement(): AbstractElement
{
return $this->element;
}

public function getIndex(): IndexInterface
{
return $this->index;
}

public function getElasticaIndex(): Index
{
return $this->elasticaIndex;
}

public function getOperation(): Operation
{
return $this->operation;
}
}
25 changes: 21 additions & 4 deletions src/Service/PropagateChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

class PropagateChanges
{
private static bool $triggerEvents = true;

public function __construct(
private readonly IndexRepository $indexRepository,
private readonly DocumentHelper $documentHelper,
Expand All @@ -39,7 +41,10 @@ public function handle(AbstractElement $element): void
$indices = $this->matchingIndicesForElement($this->indexRepository->flattenedAll(), $element);

$event = new RefreshedElementEvent($element, $indices);
$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::PRE_REFRESH_ELEMENT);

if (self::$triggerEvents) {
$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::PRE_REFRESH_ELEMENT);
}

if ($event->isPropagationStopped()) {
return;
Expand All @@ -49,7 +54,9 @@ public function handle(AbstractElement $element): void
$this->messageBus->dispatch(new RefreshElementInIndex($element, $index->getName()));
}

$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::POST_REFRESH_ELEMENT);
if (self::$triggerEvents) {
$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::POST_REFRESH_ELEMENT);
}
}

public function handleIndex(
Expand All @@ -60,6 +67,11 @@ public function handleIndex(
$this->doHandleIndex($element, $index, $elasticaIndex ?? $index->getElasticaIndex());
}

public static function disableTriggerEvents(): void
{
self::$triggerEvents = false;
}

private function doHandleIndex(
AbstractElement $element,
IndexInterface $index,
Expand Down Expand Up @@ -91,7 +103,10 @@ private function doHandleIndex(
default => Operation::NOTHING,
};
$event = new RefreshedElementInIndexEvent($element, $index, $elasticaIndex, $operation);
$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::PRE_REFRESH_ELEMENT_IN_INDEX);

if (self::$triggerEvents) {
$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::PRE_REFRESH_ELEMENT_IN_INDEX);
}

if ($event->isPropagationStopped()) {
return;
Expand All @@ -104,7 +119,9 @@ private function doHandleIndex(
Operation::UPDATE => $this->updateElementInIndex($element, $elasticaIndex, $document),
};

$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::POST_REFRESH_ELEMENT_IN_INDEX);
if (self::$triggerEvents) {
$this->eventDispatcher->dispatch($event, ElasticaBridgeEvents::POST_REFRESH_ELEMENT_IN_INDEX);
}

$this->documentHelper->resetTenantIfNeeded($document, $index);
}
Expand Down

0 comments on commit dcfeba7

Please sign in to comment.