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

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 LoggerInterface
  28. */
  29. protected $logger;
  30. /**
  31. * @param Factory $factory Massive search factory
  32. * @param array $thumbnailMimeTypes
  33. * @param string $searchImageFormat
  34. */
  35. public function __construct(
  36. protected MediaManagerInterface $mediaManager,
  37. protected Factory $factory,
  38. protected $thumbnailMimeTypes,
  39. /**
  40. * The format of the image, which will be returned in the search.
  41. */
  42. protected $searchImageFormat,
  43. ?LoggerInterface $logger = null
  44. ) {
  45. $this->logger = $logger ?: new NullLogger();
  46. }
  47. public static function getSubscribedEvents()
  48. {
  49. return [
  50. SearchEvents::PRE_INDEX => 'handlePreIndex',
  51. ];
  52. }
  53. /**
  54. * Adds the image to the search document.
  55. */
  56. public function handlePreIndex(PreIndexEvent $event)
  57. {
  58. $metadata = $event->getMetadata();
  59. if (
  60. !$event->getSubject() instanceof FileVersionMeta
  61. && FileVersionMeta::class !== $metadata->getName()
  62. ) {
  63. return;
  64. }
  65. $document = $event->getDocument();
  66. $subject = $event->getSubject();
  67. $locale = $subject->getLocale();
  68. $fileVersion = $subject->getFileVersion();
  69. $file = $fileVersion->getFile();
  70. $media = $file->getMedia();
  71. // Do not try and get the image URL if the mime type is not in the
  72. // list of mime types for which thumbnails are generated.
  73. foreach ($this->thumbnailMimeTypes as $type) {
  74. if (\fnmatch($type, $fileVersion->getMimeType())) {
  75. $document->setImageUrl($this->getImageUrl($media, $locale));
  76. break;
  77. }
  78. }
  79. $document->addField($this->factory->createField(
  80. 'media_id',
  81. $media->getId()
  82. ));
  83. $document->addField($this->factory->createField(
  84. 'media_mime',
  85. $fileVersion->getMimeType()
  86. ));
  87. if ($collection = $media->getCollection()) {
  88. $document->addField($this->factory->createField(
  89. 'collection_id',
  90. $collection->getId()
  91. ));
  92. }
  93. }
  94. /**
  95. * Return the image URL for the given media.
  96. *
  97. * TODO: The media API needs to be improved here.
  98. */
  99. private function getImageUrl($media, $locale)
  100. {
  101. $mediaApi = new Media($media, $locale);
  102. $this->mediaManager->addFormatsAndUrl($mediaApi);
  103. $formats = $mediaApi->getThumbnails();
  104. if (!isset($formats[$this->searchImageFormat])) {
  105. $this->logger->warning(\sprintf(
  106. 'Media with ID "%s" does not have thumbnail format "%s". This thumbnail would be used by the search results.',
  107. $media->getId(),
  108. $this->searchImageFormat
  109. ));
  110. return;
  111. }
  112. return $formats[$this->searchImageFormat];
  113. }
  114. }