Skip to content

Commit

Permalink
Update related Products and allow to clear caches after population
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebi committed Mar 14, 2024
1 parent e7844d3 commit 2b8fa29
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
Expand Up @@ -9,6 +9,7 @@
use Pimcore\Model\Element\AbstractElement;
use Valantic\ElasticaBridgeBundle\Document\AbstractTenantAwareDocument;
use Valantic\ElasticaBridgeBundle\Document\DataObjectNormalizerTrait;
use Valantic\ElasticaBridgeBundle\Document\DocumentInterface;
use Valantic\ElasticaBridgeBundle\Enum\DocumentType;

/**
Expand All @@ -29,6 +30,22 @@ 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: 10 additions & 0 deletions src/Document/AbstractDocument.php
Expand Up @@ -74,6 +74,16 @@ 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: 10 additions & 0 deletions src/Document/DocumentInterface.php
Expand Up @@ -109,4 +109,14 @@ public function treatObjectVariantsAsDocuments(): bool;
* @internal
*/
public static function getElasticsearchId(AbstractElement $element): string;

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

/**
* @return array<AbstractElement>
*/
public function relatedObjects(AbstractElement $element): array;
}
13 changes: 13 additions & 0 deletions src/Service/DocumentHelper.php
Expand Up @@ -5,6 +5,7 @@
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 @@ -67,4 +68,16 @@ public function resetTenantIfNeeded(
$document->resetTenant();
}
}

/**
* @param array<string> $tags
* @return void
*/
public function clearCaches(array $tags): void
{
if ($tags === []) {
return;
}
Cache::clearTags($tags);
}
}
26 changes: 26 additions & 0 deletions src/Service/PropagateChanges.php
Expand Up @@ -84,6 +84,8 @@ private function doHandleIndex(
$this->deleteElementFromIndex($element, $elasticaIndex, $document);
}

$this->handleRelatedObjects($element, $document);
$this->cachesToClear($document);
$this->documentHelper->resetTenantIfNeeded($document, $index);
}

Expand Down Expand Up @@ -160,4 +162,28 @@ private function isIdInIndex(string $id, Index $index): bool

return true;
}

/**
* @param AbstractElement $element
* @param DocumentInterface<AbstractElement> $document
* @return void
*/
private function handleRelatedObjects(AbstractElement $element, DocumentInterface $document): void
{
$relatedObjects = $document->relatedObjects($element);

foreach ($relatedObjects as $relatedObject) {
$this->handle($relatedObject);
}
}

/**
* @param DocumentInterface<AbstractElement> $document
* @return void
*/
private function cachesToClear(DocumentInterface $document): void
{
$tags = $document->getCacheTags();
$this->documentHelper->clearCaches($tags);
}
}

0 comments on commit 2b8fa29

Please sign in to comment.