vendor/sulu/article-bundle/Search/ArticleSearchSubscriber.php line 52

Open in your IDE?
  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\ArticleBundle\Search;
  11. use Massive\Bundle\SearchBundle\Search\Event\PreIndexEvent;
  12. use Massive\Bundle\SearchBundle\Search\Field;
  13. use Massive\Bundle\SearchBundle\Search\SearchEvents;
  14. use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
  15. use Sulu\Bundle\ArticleBundle\Document\Resolver\WebspaceResolver;
  16. use Sulu\Bundle\SearchBundle\Search\Factory;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class ArticleSearchSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var Factory
  22.      */
  23.     private $factory;
  24.     /**
  25.      * @var WebspaceResolver
  26.      */
  27.     private $webspaceResolver;
  28.     public function __construct(Factory $factoryWebspaceResolver $webspaceResolver)
  29.     {
  30.         $this->factory $factory;
  31.         $this->webspaceResolver $webspaceResolver;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      *
  36.      * Returns the events this subscriber has subscribed.
  37.      */
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             SearchEvents::PRE_INDEX => 'handlePreIndex',
  42.         ];
  43.     }
  44.     public function handlePreIndex(PreIndexEvent $event): void
  45.     {
  46.         $subject $event->getSubject();
  47.         $document $event->getDocument();
  48.         if (!$subject instanceof ArticleDocument) {
  49.             return;
  50.         }
  51.         $document->setUrl($subject->getRoutePath());
  52.         $document->addField(
  53.             $this->factory->createField(
  54.                 'mainWebspace',
  55.                 $this->webspaceResolver->resolveMainWebspace($subject),
  56.                 Field::TYPE_STRING
  57.             )
  58.         );
  59.         $document->addField(
  60.             $this->factory->createField(
  61.                 'additionalWebspaces',
  62.                 $this->webspaceResolver->resolveAdditionalWebspaces($subject),
  63.                 Field::TYPE_ARRAY
  64.             )
  65.         );
  66.     }
  67. }