src/Aqarmap/Bundle/ListingBundle/EventListener/PhotoThumbSerializerListener.php line 57

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\ListingBundle\Entity\ListingPhoto;
  4. use Aqarmap\Bundle\ListingBundle\Entity\Photo;
  5. use Aqarmap\Bundle\MainBundle\Service\ThumbURL;
  6. use JMS\Serializer\EventDispatcher\Events;
  7. use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
  8. use JMS\Serializer\EventDispatcher\PreSerializeEvent;
  9. use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
  10. /**
  11.  * Add thumbs url pre serialization.
  12.  */
  13. class PhotoThumbSerializerListener implements EventSubscriberInterface
  14. {
  15.     private $thumbURL;
  16.     private $uploaderHelper;
  17.     public function __construct(ThumbURL $thumbURLUploaderHelper $uploaderHelper)
  18.     {
  19.         $this->thumbURL $thumbURL;
  20.         $this->uploaderHelper $uploaderHelper;
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             [
  26.                 'event' => Events::PRE_SERIALIZE,
  27.                 'method' => 'onPreSerialize',
  28.                 'class' => Photo::class,
  29.             ],
  30.             [
  31.                 'event' => Events::PRE_SERIALIZE,
  32.                 'method' => 'onPreSerialize',
  33.                 'class' => ListingPhoto::class,
  34.             ],
  35.         ];
  36.     }
  37.     public function onPreSerialize(PreSerializeEvent $event): void
  38.     {
  39.         $object $event->getObject();
  40.         if ($object instanceof ListingPhoto) {
  41.             $hasValidLogo $object->getListing()->getUser()->getIsValidLogo();
  42.             $logo $object->getListing()->getUser()->getLogo();
  43.             $this->generateThumbnailsUrls($object->getFile(), $hasValidLogo $logo null);
  44.         } elseif ($object instanceof Photo) {
  45.             if ($object->getThumbnails()) {
  46.                 return;
  47.             }
  48.             $this->generateThumbnailsUrls($object);
  49.         }
  50.     }
  51.     private function generateThumbnailsUrls(Photo $photo, ?Photo $logoPhoto null)
  52.     {
  53.         $thumbsUrls = [
  54.             'large' => $this->thumbURL->generateURL($this->uploaderHelper->asset($photo'file'), 'large'),
  55.             'small' => $this->thumbURL->generateURL($this->uploaderHelper->asset($photo'file'), 'small'),
  56.             'thumb' => $this->thumbURL->generateURL($this->uploaderHelper->asset($photo'file'), 'search-thumb'),
  57.             'logo' => $this->thumbURL->generateURL($this->uploaderHelper->asset($photo'file'), 'logo'),
  58.         ];
  59.         if ($logoPhoto) {
  60.             $largeWithLogo $this->thumbURL->generateURL($this->uploaderHelper->asset($logoPhoto'file'), 'slider-photo-watermarked-logo-large');
  61.             $largeWithLogo .= '/'.ltrim($this->uploaderHelper->asset($photo'file'), '/');
  62.             $thumbsUrls['large'] = $largeWithLogo;
  63.         }
  64.         $photo->setThumbnails($thumbsUrls);
  65.     }
  66. }