<?php declare(strict_types=1);
namespace Swag\EnterpriseSearch\IndexingQuery;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Struct\Struct;
class IndexingQueryExtension extends Struct
{
private const KEY = 'indexingQuery';
/**
* @var array<string, IndexingQueryEntity>
*/
private $indexingQueryEntityMap;
/**
* @var bool
*/
private $cache = true;
private function __construct()
{
}
public static function addIndexingQueryExtension(Context $context): void
{
$extension = new self();
$context->addExtension(self::KEY, $extension);
}
public static function getIndexingQueryExtension(Context $context): self
{
$extension = $context->getExtension(self::KEY);
if (!$extension instanceof IndexingQueryExtension) {
throw new \Exception('No Extension available');
}
return $extension;
}
public static function hasIndexingQueryExtension(Context $context): bool
{
return $context->hasExtension(self::KEY);
}
public function executeWithoutCache(callable $function): void
{
$this->cache = false;
$function();
$this->cache = true;
}
public function isCached(): bool
{
return $this->cache;
}
public function hasIndexingQueryEntityForEntity(string $entityName): bool
{
return isset($this->indexingQueryEntityMap[$entityName]);
}
public function setIndexingQueryForEntity(IndexingQueryEntity $entity): void
{
$this->indexingQueryEntityMap[$entity->getEntityName()] = $entity;
}
public function getIndexingQueryEntityForEntity(string $entityName): IndexingQueryEntity
{
return $this->indexingQueryEntityMap[$entityName];
}
}