vendor/sulu/sulu/src/Sulu/Bundle/MediaBundle/Search/Subscriber/MediaSearchSubscriber.php line 87

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\Factory;
  13. use Massive\Bundle\SearchBundle\Search\SearchEvents;
  14. use Psr\Log\LoggerInterface;
  15. use Psr\Log\NullLogger;
  16. use Sulu\Bundle\MediaBundle\Api\Media;
  17. use Sulu\Bundle\MediaBundle\Entity\FileVersionMeta;
  18. use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. /**
  21.  * This subscriber populates the image URL field
  22.  * when a Structure containing an image field is indexed.
  23.  */
  24. class MediaSearchSubscriber implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var MediaManagerInterface
  28.      */
  29.     protected $mediaManager;
  30.     /**
  31.      * The format of the image, which will be returned in the search.
  32.      *
  33.      * @var string
  34.      */
  35.     protected $searchImageFormat;
  36.     /**
  37.      * @var Factory
  38.      */
  39.     protected $factory;
  40.     /**
  41.      * @var array
  42.      */
  43.     protected $thumbnailMimeTypes;
  44.     /**
  45.      * @var LoggerInterface
  46.      */
  47.     protected $logger;
  48.     /**
  49.      * @param Factory $factory Massive search factory
  50.      * @param LoggerInterface $logger
  51.      * @param array $thumbnailMimeTypes
  52.      * @param string $searchImageFormat
  53.      */
  54.     public function __construct(
  55.         MediaManagerInterface $mediaManager,
  56.         Factory $factory,
  57.         $thumbnailMimeTypes,
  58.         $searchImageFormat,
  59.         LoggerInterface $logger null
  60.     ) {
  61.         $this->mediaManager $mediaManager;
  62.         $this->factory $factory;
  63.         $this->searchImageFormat $searchImageFormat;
  64.         $this->thumbnailMimeTypes $thumbnailMimeTypes;
  65.         $this->logger $logger ?: new NullLogger();
  66.     }
  67.     public static function getSubscribedEvents()
  68.     {
  69.         return [
  70.             SearchEvents::PRE_INDEX => 'handlePreIndex',
  71.         ];
  72.     }
  73.     /**
  74.      * Adds the image to the search document.
  75.      */
  76.     public function handlePreIndex(PreIndexEvent $event)
  77.     {
  78.         $metadata $event->getMetadata();
  79.         if (
  80.             !$event->getSubject() instanceof FileVersionMeta
  81.             && FileVersionMeta::class !== $metadata->getName()
  82.         ) {
  83.             return;
  84.         }
  85.         $document $event->getDocument();
  86.         $subject $event->getSubject();
  87.         $locale $subject->getLocale();
  88.         $fileVersion $subject->getFileVersion();
  89.         $file $fileVersion->getFile();
  90.         $media $file->getMedia();
  91.         // Do not try and get the image URL if the mime type is not in the
  92.         // list of mime types for which thumbnails are generated.
  93.         foreach ($this->thumbnailMimeTypes as $type) {
  94.             if (\fnmatch($type$fileVersion->getMimeType())) {
  95.                 $document->setImageUrl($this->getImageUrl($media$locale));
  96.                 break;
  97.             }
  98.         }
  99.         $document->addField($this->factory->createField(
  100.             'media_id',
  101.             $media->getId()
  102.         ));
  103.         $document->addField($this->factory->createField(
  104.             'media_mime',
  105.             $fileVersion->getMimeType()
  106.         ));
  107.         if ($collection $media->getCollection()) {
  108.             $document->addField($this->factory->createField(
  109.                 'collection_id',
  110.                 $collection->getId()
  111.             ));
  112.         }
  113.     }
  114.     /**
  115.      * Return the image URL for the given media.
  116.      *
  117.      * TODO: The media API needs to be improved here.
  118.      */
  119.     private function getImageUrl($media$locale)
  120.     {
  121.         $mediaApi = new Media($media$locale);
  122.         $this->mediaManager->addFormatsAndUrl($mediaApi);
  123.         $formats $mediaApi->getThumbnails();
  124.         if (!isset($formats[$this->searchImageFormat])) {
  125.             $this->logger->warning(\sprintf(
  126.                 'Media with ID "%s" does not have thumbnail format "%s". This thumbnail would be used by the search results.',
  127.                 $media->getId(),
  128.                 $this->searchImageFormat
  129.             ));
  130.             return;
  131.         }
  132.         return $formats[$this->searchImageFormat];
  133.     }
  134. }