diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index c9f7fad..dd0ddf4 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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() diff --git a/src/EventListener/Pimcore/ChangeListener.php b/src/EventListener/Pimcore/ChangeListener.php index 5c2ae31..51480aa 100644 --- a/src/EventListener/Pimcore/ChangeListener.php +++ b/src/EventListener/Pimcore/ChangeListener.php @@ -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; @@ -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( @@ -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); } } diff --git a/src/Repository/ConfigurationRepository.php b/src/Repository/ConfigurationRepository.php index 7875ca9..1701412 100644 --- a/src/Repository/ConfigurationRepository.php +++ b/src/Repository/ConfigurationRepository.php @@ -4,6 +4,11 @@ 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; /** @@ -11,6 +16,9 @@ */ 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, ) {} @@ -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]; } }