vendor/friendsofsymfony/elastica-bundle/src/Persister/Listener/FilterObjectsListener.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSElasticaBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\ElasticaBundle\Persister\Listener;
  11. use FOS\ElasticaBundle\Persister\Event\PreInsertObjectsEvent;
  12. use FOS\ElasticaBundle\Provider\IndexableInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class FilterObjectsListener implements EventSubscriberInterface
  15. {
  16.     private IndexableInterface $indexable;
  17.     public function __construct(IndexableInterface $indexable)
  18.     {
  19.         $this->indexable $indexable;
  20.     }
  21.     public function filterObjects(PreInsertObjectsEvent $event): void
  22.     {
  23.         $options $event->getOptions();
  24.         if (false == empty($options['skip_indexable_check'])) {
  25.             return;
  26.         }
  27.         $objects $event->getObjects();
  28.         $index $options['indexName'];
  29.         $filtered = [];
  30.         foreach ($objects as $object) {
  31.             if (!$this->indexable->isObjectIndexable($index$object)) {
  32.                 continue;
  33.             }
  34.             $filtered[] = $object;
  35.         }
  36.         $event->setFilteredObjectCount(\count($objects) - \count($filtered));
  37.         $event->setObjects($filtered);
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             PreInsertObjectsEvent::class => 'filterObjects',
  43.         ];
  44.     }
  45. }