vendor/crehler/advanced-sort/src/Subscriber/CategorySubscriber.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @copyright 2023 Crehler Sp. z o.o.
  5.  *
  6.  * https://crehler.com/
  7.  * support@crehler.com
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\AdvancedSort\Subscriber;
  13. use Crehler\AdvancedSort\Messages\CatalogInheritanceMessage;
  14. use Crehler\AdvancedSort\Services\Indexer\CategoryItemsUpdater;
  15. use Shopware\Core\Content\Category\CategoryEvents;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Messenger\MessageBusInterface;
  19. class CategorySubscriber implements EventSubscriberInterface
  20. {
  21.     private CategoryItemsUpdater $categoryItemsUpdater;
  22.     private MessageBusInterface $messageBus;
  23.     public function __construct(CategoryItemsUpdater $categoryItemsUpdaterMessageBusInterface $messageBus)
  24.     {
  25.         $this->categoryItemsUpdater $categoryItemsUpdater;
  26.         $this->messageBus $messageBus;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             CategoryEvents::CATEGORY_WRITTEN_EVENT => 'onCategoryWrittenEvent',
  32.         ];
  33.     }
  34.     public function onCategoryWrittenEvent(EntityWrittenEvent $event): void
  35.     {
  36.         $categories $event->getIds();
  37.         foreach ($categories as $categoryId) {
  38.             $this->categoryItemsUpdater->handle($categoryId,  $event->getContext());
  39.         }
  40.         $message = new CatalogInheritanceMessage();
  41.         $this->messageBus->dispatch($message);
  42.     }
  43. }