vendor/shopware/core/Content/Media/Subscriber/MediaLoadedSubscriber.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Media\Subscriber;
  3. use Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailCollection;
  4. use Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailEntity;
  5. use Shopware\Core\Content\Media\MediaEntity;
  6. use Shopware\Core\Content\Media\MediaEvents;
  7. use Shopware\Core\Content\Media\Pathname\UrlGeneratorInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Shopware\Core\Framework\Log\Package;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  13.  */
  14. #[Package('content')]
  15. class MediaLoadedSubscriber implements EventSubscriberInterface
  16. {
  17.     private UrlGeneratorInterface $urlGenerator;
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(UrlGeneratorInterface $urlGenerator)
  22.     {
  23.         $this->urlGenerator $urlGenerator;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             MediaEvents::MEDIA_LOADED_EVENT => [
  29.                 ['unserialize'10],
  30.                 ['addUrls'],
  31.             ],
  32.         ];
  33.     }
  34.     public function addUrls(EntityLoadedEvent $event): void
  35.     {
  36.         /** @var MediaEntity $media */
  37.         foreach ($event->getEntities() as $media) {
  38.             if (!$media->hasFile() || $media->isPrivate()) {
  39.                 continue;
  40.             }
  41.             $media->setUrl($this->urlGenerator->getAbsoluteMediaUrl($media));
  42.             foreach ($media->getThumbnails() ?? [] as $thumbnail) {
  43.                 $this->addThumbnailUrl($thumbnail$media);
  44.             }
  45.         }
  46.     }
  47.     public function unserialize(EntityLoadedEvent $event): void
  48.     {
  49.         /** @var MediaEntity $media */
  50.         foreach ($event->getEntities() as $media) {
  51.             if ($media->getMediaTypeRaw()) {
  52.                 $media->setMediaType(unserialize($media->getMediaTypeRaw()));
  53.             }
  54.             if ($media->getThumbnails() === null) {
  55.                 if ($media->getThumbnailsRo()) {
  56.                     $media->setThumbnails(unserialize($media->getThumbnailsRo()));
  57.                 } else {
  58.                     $media->setThumbnails(new MediaThumbnailCollection());
  59.                 }
  60.             }
  61.         }
  62.     }
  63.     private function addThumbnailUrl(MediaThumbnailEntity $thumbnailMediaEntity $media): void
  64.     {
  65.         $thumbnail->setUrl(
  66.             $this->urlGenerator->getAbsoluteThumbnailUrl(
  67.                 $media,
  68.                 $thumbnail
  69.             )
  70.         );
  71.     }
  72. }