vendor/sulu/sulu/src/Sulu/Bundle/MediaBundle/Search/Subscriber/StructureMediaSearchSubscriber.php line 74

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\MediaBundle\Search\Subscriber;
  11. use Massive\Bundle\SearchBundle\Search\Event\PreIndexEvent;
  12. use Massive\Bundle\SearchBundle\Search\SearchEvents;
  13. use Sulu\Bundle\MediaBundle\Content\MediaSelectionContainer;
  14. use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
  15. use Sulu\Component\Content\Document\Behavior\StructureBehavior;
  16. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. /**
  19.  * This subscriber populates the image URL field
  20.  * when a Structure containing an image field is indexed.
  21.  */
  22. class StructureMediaSearchSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var MediaManagerInterface
  26.      */
  27.     protected $mediaManager;
  28.     /**
  29.      * @var RequestAnalyzerInterface
  30.      */
  31.     protected $requestAnalyzer;
  32.     /**
  33.      * The format of the image, which will be returned in the search.
  34.      *
  35.      * @var string
  36.      */
  37.     protected $searchImageFormat;
  38.     /**
  39.      * @param RequestAnalyzerInterface $requestAnalyzer
  40.      * @param string $searchImageFormat
  41.      */
  42.     public function __construct(
  43.         MediaManagerInterface $mediaManager,
  44.         RequestAnalyzerInterface $requestAnalyzer null,
  45.         $searchImageFormat
  46.     ) {
  47.         $this->mediaManager $mediaManager;
  48.         $this->requestAnalyzer $requestAnalyzer;
  49.         $this->searchImageFormat $searchImageFormat;
  50.     }
  51.     /**
  52.      * Returns the events this subscriber has subscribed.
  53.      *
  54.      * @return array
  55.      */
  56.     public static function getSubscribedEvents()
  57.     {
  58.         return [
  59.             SearchEvents::PRE_INDEX => 'handlePreIndex',
  60.         ];
  61.     }
  62.     /**
  63.      * Adds the image to the search document.
  64.      */
  65.     public function handlePreIndex(PreIndexEvent $e)
  66.     {
  67.         $metadata $e->getMetadata();
  68.         $document $e->getDocument();
  69.         $subject $e->getSubject();
  70.         $evaluator $e->getFieldEvaluator();
  71.         if (
  72.             !$e->getSubject() instanceof StructureBehavior
  73.             && StructureBehavior::class !== $metadata->getName()
  74.         ) {
  75.             return;
  76.         }
  77.         if (!$imageUrlField $metadata->getImageUrlField()) {
  78.             return;
  79.         }
  80.         $data $evaluator->getValue($subject$imageUrlField);
  81.         $locale $subject->getLocale();
  82.         if (!$data) {
  83.             $document->setImageUrl(null);
  84.             return;
  85.         }
  86.         $imageUrl $this->getImageUrl($data$locale);
  87.         $document->setImageUrl($imageUrl);
  88.     }
  89.     /**
  90.      * Returns the url for the image.
  91.      *
  92.      * @param array|MediaSelectionContainer $data
  93.      * @param string $locale
  94.      *
  95.      * @return string|null
  96.      *
  97.      * @throws \RuntimeException
  98.      * @throws \InvalidArgumentException
  99.      */
  100.     private function getImageUrl($data$locale)
  101.     {
  102.         // new structures will container an instance of MediaSelectionContainer
  103.         if ($data instanceof MediaSelectionContainer) {
  104.             $medias $data->getData();
  105.         // old ones an array ...
  106.         } else {
  107.             $ids = [];
  108.             if (isset($data['ids'])) {
  109.                 $ids \array_merge($ids$data['ids']);
  110.             } elseif (isset($data['id'])) {
  111.                 $ids \array_merge($ids, [$data['id']]);
  112.             }
  113.             if (=== \count($ids)) {
  114.                 return null;
  115.             }
  116.             $medias $this->mediaManager->get($locale, [
  117.                 'ids' => $ids,
  118.             ]);
  119.         }
  120.         // no media, no thumbnail URL
  121.         if (!$medias) {
  122.             return null;
  123.         }
  124.         $media \current($medias);
  125.         if (!$media) {
  126.             return null;
  127.         }
  128.         $formats $media->getThumbnails();
  129.         if (empty($formats)) {
  130.             return null;
  131.         }
  132.         if (!isset($formats[$this->searchImageFormat])) {
  133.             throw new \InvalidArgumentException(
  134.                 \sprintf('Search image format "%s" is not known'$this->searchImageFormat)
  135.             );
  136.         }
  137.         return $formats[$this->searchImageFormat];
  138.     }
  139. }