vendor/ibexa/taxonomy/src/lib/Event/Subscriber/RemoveTaxonomyEntrySubscriber.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\Taxonomy\Event\Subscriber;
  8. use Ibexa\Contracts\Core\Persistence\Handler as ContentPersistenceHandler;
  9. use Ibexa\Contracts\Core\Repository\SearchService;
  10. use Ibexa\Contracts\Core\Repository\Values\Content\Query;
  11. use Ibexa\Contracts\Core\Search\Handler as ContentSearchHandler;
  12. use Ibexa\Contracts\Taxonomy\Event\BeforeRemoveTaxonomyEntryEvent;
  13. use Ibexa\Contracts\Taxonomy\Event\RemoveTaxonomyEntryEvent;
  14. use Ibexa\Contracts\Taxonomy\Search\Query\Criterion;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. final class RemoveTaxonomyEntrySubscriber implements EventSubscriberInterface
  17. {
  18.     private SearchService $searchService;
  19.     private ContentSearchHandler $searchHandler;
  20.     private ContentPersistenceHandler $persistenceHandler;
  21.     public function __construct(
  22.         SearchService $searchService,
  23.         ContentSearchHandler $searchHandler,
  24.         ContentPersistenceHandler $persistenceHandler
  25.     ) {
  26.         $this->searchService $searchService;
  27.         $this->searchHandler $searchHandler;
  28.         $this->persistenceHandler $persistenceHandler;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             BeforeRemoveTaxonomyEntryEvent::class => 'onBeforeRemoveTaxonomyEntry',
  34.             RemoveTaxonomyEntryEvent::class => 'onRemoveTaxonomyEntry',
  35.         ];
  36.     }
  37.     /**
  38.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
  39.      */
  40.     public function onBeforeRemoveTaxonomyEntry(BeforeRemoveTaxonomyEntryEvent $event): void
  41.     {
  42.         $query = new Query();
  43.         $query->query = new Criterion\TaxonomyEntryId($event->getTaxonomyEntry()->getId());
  44.         $searchHits $this->searchService->findContent($query)->getIterator();
  45.         $contentIds = [];
  46.         foreach ($searchHits as $searchHit) {
  47.             $contentId $searchHit->valueObject->getVersionInfo()->getContentInfo()->getId();
  48.             $contentIds[] = $contentId;
  49.         }
  50.         $event->setContentIds($contentIds);
  51.     }
  52.     public function onRemoveTaxonomyEntry(RemoveTaxonomyEntryEvent $event): void
  53.     {
  54.         $persistenceHandler $this->persistenceHandler;
  55.         foreach ($event->getContentIds() as $contentId) {
  56.             $locations $persistenceHandler->locationHandler()->loadLocationsByContent($contentId);
  57.             foreach ($locations as $location) {
  58.                 $this->searchHandler->indexLocation($location);
  59.             }
  60.             $this->searchHandler->indexContent(
  61.                 $persistenceHandler->contentHandler()->load($contentId)
  62.             );
  63.         }
  64.     }
  65. }