Skip to content

Commit

Permalink
also allow ignoring version save only
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebi committed Mar 27, 2024
1 parent 2345607 commit 36a8e35
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
8 changes: 8 additions & 0 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -38,6 +38,14 @@ public function getConfigTreeBuilder()
->booleanNode('document')->defaultTrue()->end()
->end()
->end()
->arrayNode('version_only')->info('Define whether saveVersionOnly 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()
Expand Down
42 changes: 28 additions & 14 deletions src/EventListener/Pimcore/ChangeListener.php
Expand Up @@ -14,6 +14,8 @@
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\Document;
use Pimcore\Model\Element\AbstractElement;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Valantic\ElasticaBridgeBundle\Exception\EventListener\PimcoreElementNotFoundException;
Expand All @@ -27,6 +29,8 @@
*/
class ChangeListener implements EventSubscriberInterface
{
public const ARGUMENT_IS_AUTO_SAVE = 'isAutoSave';
public const ARGUMENT_SAVE_VERSION_ONLY = 'saveVersionOnly';
private static bool $isEnabled = true;

public function __construct(
Expand Down Expand Up @@ -124,24 +128,34 @@ private function getFreshElement(AbstractElement $element): AbstractElement

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

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

if (!$isAutoSave) {
try {
return self::$isEnabled
&& $this->checkEvent($event, self::ARGUMENT_IS_AUTO_SAVE)
&& $this->checkEvent($event, self::ARGUMENT_SAVE_VERSION_ONLY);
} catch (NotFoundExceptionInterface|ContainerExceptionInterface) {
// this should not happen, as we set a default value
return true;
}
}

if ($event instanceof AssetEvent && $this->configurationRepository->shouldHandleAssetAutoSave()) {
return true;
}
/**
* @param AssetEvent|DataObjectEvent|DocumentEvent $event
* @param self::ARGUMENT_* $argument
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*
* @return bool
*/
private function checkEvent(AssetEvent|DataObjectEvent|DocumentEvent $event, string $argument): bool
{
$isArgument = $event->hasArgument($argument) && $event->getArgument($argument) === true;

if ($event instanceof DataObjectEvent && $this->configurationRepository->shouldHandleDataObjectAutoSave()) {
return true;
}
$argumentName = match ($argument) {
self::ARGUMENT_IS_AUTO_SAVE => ConfigurationRepository::SHOULD_HANDLE_AUTO_SAVE,
self::ARGUMENT_SAVE_VERSION_ONLY => ConfigurationRepository::SHOULD_HANDLE_VERSION_ONLY,
};

return $event instanceof DocumentEvent && $this->configurationRepository->shouldHandleDocumentAutoSave();
return !$isArgument || $this->configurationRepository->shouldHandleVersion($event, $argumentName);
}
}
35 changes: 24 additions & 11 deletions src/Repository/ConfigurationRepository.php
Expand Up @@ -4,13 +4,21 @@

namespace Valantic\ElasticaBridgeBundle\Repository;

use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\DocumentEvent;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

/**
* @internal
*/
class ConfigurationRepository
{
public const SHOULD_HANDLE_AUTO_SAVE = 'auto_save';
public const SHOULD_HANDLE_VERSION_ONLY = 'version_only';

public function __construct(
private readonly ContainerBagInterface $containerBag,
) {}
Expand All @@ -35,18 +43,23 @@ 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
/**
* @param AssetEvent|DataObjectEvent|DocumentEvent $event
* @param self::SHOULD_* $argument
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*
* @return bool
*/
public function shouldHandleVersion(AssetEvent|DataObjectEvent|DocumentEvent $event, string $argument): bool
{
return $this->containerBag->get('valantic_elastica_bridge')['events']['auto_save']['data_object'];
}
$eventName = match (true) {
$event instanceof AssetEvent => 'asset',
$event instanceof DataObjectEvent => 'data_object',
$event instanceof DocumentEvent => 'document',
};

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

0 comments on commit 36a8e35

Please sign in to comment.