51 lines
1.6 KiB
PHP
51 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\AdminBundle;
|
|
|
|
use KupShop\CatalogBundle\Search\FulltextElastic;
|
|
use KupShop\KupShopBundle\Context\ContextManager;
|
|
use KupShop\KupShopBundle\Context\LanguageContext;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
|
|
trait FulltextUpdatesTrait
|
|
{
|
|
private FulltextElastic $_fulltextElastic;
|
|
private ContextManager $_contextManager;
|
|
|
|
protected function updateProductsFulltext(array $productIds): void
|
|
{
|
|
$this->forEachFulltextLanguage(function () use ($productIds) {
|
|
$this->getFulltextElastic()->updateProductFulltext($productIds);
|
|
$this->getFulltextElastic()->updateProductsAttributes($productIds);
|
|
});
|
|
}
|
|
|
|
protected function updateIndices(string $type = 'all'): void
|
|
{
|
|
$this->forEachFulltextLanguage(fn () => $this->getFulltextElastic()->updateIndex($type));
|
|
}
|
|
|
|
private function forEachFulltextLanguage(callable $callback)
|
|
{
|
|
$this->getContextManager()->deactivateUser(function () use ($callback) {
|
|
foreach ($this->getFulltextElastic()->getFulltextLanguages() as $language) {
|
|
$this->getContextManager()->activateContexts([LanguageContext::class => $language->getId()],
|
|
fn () => $callback(),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
private function getFulltextElastic(): FulltextElastic
|
|
{
|
|
return $this->_fulltextElastic ??= ServiceContainer::getService(FulltextElastic::class);
|
|
}
|
|
|
|
private function getContextManager(): ContextManager
|
|
{
|
|
return $this->_contextManager ??= ServiceContainer::getService(ContextManager::class);
|
|
}
|
|
}
|