vendor/sulu/sulu/src/Sulu/Bundle/MediaBundle/Search/EventListener/PermissionListener.php line 52

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\MediaBundle\Search\EventListener;
  11. use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
  12. use Sulu\Bundle\MediaBundle\Entity\Collection;
  13. use Sulu\Bundle\MediaBundle\Entity\FileVersionMetaRepository;
  14. use Sulu\Component\Security\Event\PermissionUpdateEvent;
  15. use Sulu\Component\Security\Event\SecurityEvents;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. /**
  18.  * Removes a media from the index, as soon as it gets secured.
  19.  */
  20. class PermissionListener implements EventSubscriberInterface
  21. {
  22.     public function __construct(
  23.         private FileVersionMetaRepository $fileVersionMetaRepository,
  24.         private SearchManagerInterface $searchManager,
  25.     ) {
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [SecurityEvents::PERMISSION_UPDATE => 'onPermissionUpdate'];
  30.     }
  31.     /**
  32.      * Removes all FileVersionMetas belonging to the collection, which just got secured.
  33.      */
  34.     public function onPermissionUpdate(PermissionUpdateEvent $event)
  35.     {
  36.         if (Collection::class !== $event->getType()) {
  37.             return;
  38.         }
  39.         foreach ($this->fileVersionMetaRepository->findByCollectionId($event->getIdentifier()) as $fileVersionMeta) {
  40.             $this->searchManager->deindex($fileVersionMeta);
  41.         }
  42.     }
  43. }