Skip to content

Commit

Permalink
Merge pull request #62 from valantic/feature/autosave
Browse files Browse the repository at this point in the history
Handle auto-save
  • Loading branch information
limenet committed Mar 14, 2024
2 parents 716783e + 3b57f5b commit 2d696c0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ public function getConfigTreeBuilder()
->booleanNode('should_skip_failing_documents')->defaultFalse()->info('If true, when a document fails to be indexed, it will be skipped and indexing continue with the next document. If false, indexing that index will be aborted.')->end()
->end()
->end()
->arrayNode('events')
->addDefaultsIfNotSet()
->children()
->arrayNode('auto_save')->info('Define whether auto-save events should trigger a refresh of the element in the corresponding indices.')
->addDefaultsIfNotSet()
->children()
->booleanNode('asset')->defaultTrue()->end()
->booleanNode('data_object')->defaultTrue()->end()
->booleanNode('document')->defaultTrue()->end()
->end()
->end()
->end()
->end()
->end()
->end();

Expand Down
31 changes: 30 additions & 1 deletion src/EventListener/Pimcore/ChangeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Messenger\MessageBusInterface;
use Valantic\ElasticaBridgeBundle\Exception\EventListener\PimcoreElementNotFoundException;
use Valantic\ElasticaBridgeBundle\Messenger\Message\RefreshElement;
use Valantic\ElasticaBridgeBundle\Repository\ConfigurationRepository;

/**
* An abstract listener for DataObject and Document listeners.
Expand All @@ -30,11 +31,12 @@ class ChangeListener implements EventSubscriberInterface

public function __construct(
private readonly MessageBusInterface $messageBus,
private readonly ConfigurationRepository $configurationRepository,
) {}

public function handle(AssetEvent|DataObjectEvent|DocumentEvent $event): void
{
if (!self::$isEnabled) {
if (!$this->shouldHandle($event)) {
return;
}

Expand Down Expand Up @@ -95,4 +97,31 @@ private function getFreshElement(AbstractElement $element): AbstractElement

return $elementClass::getById($element->getId(), ['force' => true]) ?? throw $e;
}

private function shouldHandle(AssetEvent|DataObjectEvent|DocumentEvent $event): bool
{
if (!self::$isEnabled) {
return false;
}

$isAutoSave = $event->hasArgument('isAutoSave') && $event->getArgument('isAutoSave') === true;

if ($isAutoSave) {
return true;
}

if ($event instanceof AssetEvent && $this->configurationRepository->shouldHandleAssetAutoSave()) {
return true;
}

if ($event instanceof DataObjectEvent && $this->configurationRepository->shouldHandleDataObjectAutoSave()) {
return true;
}

if ($event instanceof DocumentEvent && $this->configurationRepository->shouldHandleDocumentAutoSave()) {
return true;
}

return false;
}
}
15 changes: 15 additions & 0 deletions src/Repository/ConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ public function shouldSkipFailingDocuments(): bool
{
return $this->containerBag->get('valantic_elastica_bridge')['indexing']['should_skip_failing_documents'];
}

public function shouldHandleAssetAutoSave(): bool
{
return $this->containerBag->get('valantic_elastica_bridge')['events']['auto_save']['asset'];
}

public function shouldHandleDataObjectAutoSave(): bool
{
return $this->containerBag->get('valantic_elastica_bridge')['events']['auto_save']['data_object'];
}

public function shouldHandleDocumentAutoSave(): bool
{
return $this->containerBag->get('valantic_elastica_bridge')['events']['auto_save']['document'];
}
}

0 comments on commit 2d696c0

Please sign in to comment.