<?php
namespace Pixel\LexikonSW6\Entities\Categories\DataAbstractionLayer;
use Pixel\LexikonSW6\Entities\Categories\Events\LexikonCategoriesIndexerEvent;
use Pixel\LexikonSW6\Entities\Categories\LexikonCategoriesDefinition;
use Shopware\Core\Framework\Adapter\Cache\CacheClearer;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexer;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class LexikonCategoriesIndexer extends EntityIndexer
{
/** @var IteratorFactory */
private $iteratorFactory;
/** @var CacheClearer */
private $cacheClearer;
/** @var EventDispatcherInterface */
private $eventDispatcher;
/**
* @var EntityRepositoryInterface
*/
private $repository;
public function __construct(
IteratorFactory $iteratorFactory,
EntityRepositoryInterface $repository,
CacheClearer $cacheClearer,
EventDispatcherInterface $eventDispatcher
) {
$this->iteratorFactory = $iteratorFactory;
$this->repository = $repository;
$this->cacheClearer = $cacheClearer;
$this->eventDispatcher = $eventDispatcher;
}
public function getName(): string
{
return 'lexikon.listing.indexer';
}
public function iterate($offset): ?EntityIndexingMessage
{
$iterator = $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
$ids = $iterator->fetch();
// loop end? return null
if (empty($ids)) {
return null;
}
return new EntityIndexingMessage(array_values($ids), $iterator->getOffset());
}
public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
{
$updates = $event->getPrimaryKeys(LexikonCategoriesDefinition::ENTITY_NAME);
if (empty($updates)) {
return null;
}
return new EntityIndexingMessage(array_values($updates), null, $event->getContext());
}
public function handle(EntityIndexingMessage $message): void
{
$ids = $message->getData();
// 19.12
$ids = array_unique(array_filter($ids));
if (empty($ids)) {
return;
}
// update all required data
$this->eventDispatcher->dispatch(new LexikonCategoriesIndexerEvent($ids, $message->getContext()));
//$this->cacheClearer->invalidateIds($ids, LexikonCategoriesDefinition::ENTITY_NAME);
}
}