Skip to content

Commit

Permalink
Merge branch 'update-related-products' into v4.2.0-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebi committed Mar 22, 2024
2 parents 6142db7 + dcfeba7 commit e6c0e08
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 92 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ valantic_elastica_bridge:

[Set up a worker](https://symfony.com/doc/current/messenger.html#consuming-messages-running-the-worker) to process `elastica_bridge_index`. Alternatively you can route the transport to use the `sync` handler: `framework.messenger.transports.elastica_bridge_index: 'sync'`.

## Events

This project uses Symfony's event dispatcher. Here are the events that you can listen to:

| Description | Example Usage | Event Constant (`ElasticaBridgeEvents::`) | Event Object (`Model\Event\`) |
|-----------------------------------------------------|----------------------------------------------------------------------|-------------------------------------------|--------------------------------|
| After an element has been refreshed in an index. | Log Event, send notification | `POST_REFRESH_ELEMENT_IN_INDEX` | `RefreshedElementInIndexEvent` |
| Before an element is refreshed in an index. | Stop propagation of element in specific index | `PRE_REFRESH_ELEMENT_IN_INDEX` | `RefreshedElementInIndexEvent` |
| After an element has been refreshed in all indices. | Clear caches, refresh related Objects, Log Event, send notification | `POST_REFRESH_ELEMENT` | `RefreshedElementEvent` |
| Before an element is refreshed in all indices. | Stop propagation of element in all indices | `PRE_REFRESH_ELEMENT` | `RefreshedElementEvent` |

You can create an event subscriber or an event listener to listen to these events. Please refer to the [Symfony documentation](https://symfony.com/doc/current/event_dispatcher.html) for more information on how to use the event dispatcher.

### Possible Use Cases for Events
- clear cache after an element has been refreshed
- send a notification after an element has been refreshed
- log the event
- update related elements in the index
- etc

## Indexing

### Bulk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,6 @@ public function getSubType(): ?string
return Product::class;
}

/**
* @return array<AbstractElement>
*/
public function relatedObjects(AbstractElement $element): array
{
if ($element instanceof Product) {
return $element->getRelatedProducts();
}

return [];
}

public function getCacheTags(): array
{
return ['navigation'];
}

public function getNormalized(AbstractElement $element): array
{
return [
Expand Down
10 changes: 0 additions & 10 deletions src/Document/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ public function getListingInstance(IndexInterface $index): AbstractListing
return $listingInstance;
}

public function relatedObjects(AbstractElement $element): array
{
return [];
}

public function getCacheTags(): array
{
return [];
}

final public static function getElasticsearchId(AbstractElement $element): string
{
$documentType = DocumentType::tryFrom($element->getType());
Expand Down
10 changes: 0 additions & 10 deletions src/Document/DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,6 @@ public function getListingInstance(IndexInterface $index): AbstractListing;
*/
public function treatObjectVariantsAsDocuments(): bool;

/**
* @return array<string>
*/
public function getCacheTags(): array;

/**
* @return array<AbstractElement>
*/
public function relatedObjects(AbstractElement $element): array;

/**
* Returns the Elasticsearch ID for a Pimcore element.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Enum/Operation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Valantic\ElasticaBridgeBundle\Enum;

enum Operation: int
{
case NOTHING = 0;
case DELETE = 1;
case INSERT = 2;
case UPDATE = 3;
}
2 changes: 1 addition & 1 deletion src/EventListener/Pimcore/ChangeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function shouldHandle(AssetEvent|DataObjectEvent|DocumentEvent $event):

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

if (!$isAutoSave) {
if ($isAutoSave) {
return true;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Messenger/Handler/RefreshElementHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public function __invoke(RefreshElement $message): void
{
$element = $this->resolveElement($message);

$this->propagateChanges->handle($element);
if (!$message->shouldTriggerEvents()) {
PropagateChanges::disableTriggerEvents();
}

$this->propagateChanges->handleRelatedObjects($element);
$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;
}
}
13 changes: 13 additions & 0 deletions src/Model/Event/ElasticaBridgeEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Valantic\ElasticaBridgeBundle\Model\Event;

interface ElasticaBridgeEvents
{
public const POST_REFRESH_ELEMENT_IN_INDEX = 'valantic.elastica_bridge.post_refreshed_element_in_index';
public const PRE_REFRESH_ELEMENT_IN_INDEX = 'valantic.elastica_bridge.pre_refreshed_element_in_index';
public const POST_REFRESH_ELEMENT = 'valantic.elastica_bridge.post_refreshed_element';
public const PRE_REFRESH_ELEMENT = 'valantic.elastica_bridge.pre_refreshed_element';
}
34 changes: 34 additions & 0 deletions src/Model/Event/RefreshedElementEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Valantic\ElasticaBridgeBundle\Model\Event;

use Pimcore\Model\Element\AbstractElement;
use Symfony\Contracts\EventDispatcher\Event;
use Valantic\ElasticaBridgeBundle\Index\IndexInterface;

class RefreshedElementEvent extends Event
{
/**
* @param AbstractElement $element
* @param array<IndexInterface> $indices
*/
public function __construct(
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;
}
}
41 changes: 41 additions & 0 deletions src/Model/Event/RefreshedElementInIndexEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Valantic\ElasticaBridgeBundle\Model\Event;

use Elastica\Index;
use Pimcore\Model\Element\AbstractElement;
use Symfony\Contracts\EventDispatcher\Event;
use Valantic\ElasticaBridgeBundle\Enum\Operation;
use Valantic\ElasticaBridgeBundle\Index\IndexInterface;

class RefreshedElementInIndexEvent extends Event
{
public function __construct(
public readonly AbstractElement $element,
public readonly IndexInterface $index,
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;
}
}
14 changes: 0 additions & 14 deletions src/Service/DocumentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Valantic\ElasticaBridgeBundle\Service;

use Elastica\Document;
use Pimcore\Cache;
use Pimcore\Model\Element\AbstractElement;
use Valantic\ElasticaBridgeBundle\Document\DocumentInterface;
use Valantic\ElasticaBridgeBundle\Document\TenantAwareInterface as DocumentTenantAwareInterface;
Expand Down Expand Up @@ -68,17 +67,4 @@ public function resetTenantIfNeeded(
$document->resetTenant();
}
}

/**
* @param array<string> $tags
*
* @return void
*/
public function clearCaches(array $tags): void
{
if ($tags === []) {
return;
}
Cache::clearTags($tags);
}
}

0 comments on commit e6c0e08

Please sign in to comment.