<?php declare(strict_types=1);
namespace Wexo\Relewise\Subscriber;
use Shopware\Core\Content\Category\Event\CategoryIndexerEvent;
use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
use Shopware\Core\Framework\Feature;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Wexo\Relewise\Component\RelewiseContentMessage;
use Wexo\Relewise\Component\RelewiseProductMessage;
use Wexo\Relewise\Service\Model\Export\Content\ContentUpdate;
use Wexo\Relewise\Service\Model\Export\Product\ProductUpdate;
class IntegrationSubscriber implements EventSubscriberInterface
{
/**
* @var MessageBusInterface
*/
private MessageBusInterface $bus;
/**
* @param MessageBusInterface $bus
*/
public function __construct(
MessageBusInterface $bus
) {
$this->bus = $bus;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
ProductIndexerEvent::class => 'onProductIndexer',
CategoryIndexerEvent::class => 'onCategoryIndexer'
];
}
/**
* @param ProductIndexerEvent $event
*/
public function onProductIndexer(ProductIndexerEvent $event): void
{
if (Feature::isActive('v6.5.0.0')) {
$ids = $event->getIds();
} else {
$ids = array_merge($event->getIds(), $event->getChildrenIds(), $event->getParentIds());
}
$this->bus->dispatch(
new RelewiseProductMessage(
new ProductUpdate(),
array_unique($ids),
$event->getContext()
)
);
}
/**
* @param CategoryIndexerEvent $event
*/
public function onCategoryIndexer(CategoryIndexerEvent $event): void
{
$this->bus->dispatch(
new RelewiseContentMessage(
new ContentUpdate(),
array_unique($event->getIds()),
$event->getContext()
)
);
}
}