<?php
declare(strict_types=1);
/**
* @copyright 2023 Crehler Sp. z o.o.
*
* https://crehler.com/
* support@crehler.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\AdvancedSort\Subscriber;
use Crehler\AdvancedSort\Messages\CatalogInheritanceMessage;
use Crehler\AdvancedSort\Services\Indexer\CategoryItemsUpdater;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class CategorySubscriber implements EventSubscriberInterface
{
private CategoryItemsUpdater $categoryItemsUpdater;
private MessageBusInterface $messageBus;
public function __construct(CategoryItemsUpdater $categoryItemsUpdater, MessageBusInterface $messageBus)
{
$this->categoryItemsUpdater = $categoryItemsUpdater;
$this->messageBus = $messageBus;
}
public static function getSubscribedEvents(): array
{
return [
CategoryEvents::CATEGORY_WRITTEN_EVENT => 'onCategoryWrittenEvent',
];
}
public function onCategoryWrittenEvent(EntityWrittenEvent $event): void
{
$categories = $event->getIds();
foreach ($categories as $categoryId) {
$this->categoryItemsUpdater->handle($categoryId, $event->getContext());
}
$message = new CatalogInheritanceMessage();
$this->messageBus->dispatch($message);
}
}