<?php
declare(strict_types=1);
namespace Bodymed\Webshop\Core\Subscriber;
use Bodymed\Webshop\Messenger\Messages\PharmacyCategoryConfig\PharmacyCategoryConfigWritten;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class PharmacyCategorySubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $pharmacyCategoryConfigRepository;
private MessageBusInterface $messengerAsyncBus;
public function __construct(
EntityRepositoryInterface $pharmacyCategoryConfigRepository,
MessageBusInterface $messengerAsyncBus
) {
$this->pharmacyCategoryConfigRepository = $pharmacyCategoryConfigRepository;
$this->messengerAsyncBus = $messengerAsyncBus;
}
public static function getSubscribedEvents(): array
{
return [
'pharmacy_category_config.written' => 'onEntityWritten',
];
}
public function onEntityWritten(EntityWrittenEvent $event): void
{
$context = $event->getContext();
$ids = $event->getIds();
$result = $this->pharmacyCategoryConfigRepository->search(new Criteria($ids), $context);
$this->messengerAsyncBus->dispatch(PharmacyCategoryConfigWritten::create($result->getEntities(), $context));
}
}