src/EventSubscriber/CarImageUploadSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\CarImages;
  4. use App\Service\CarImageThumbnailGenerator;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Vich\UploaderBundle\Event\Event;
  8. use Vich\UploaderBundle\Event\Events as VichEvents;
  9. use Vich\UploaderBundle\Storage\StorageInterface;
  10. final class CarImageUploadSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         private readonly StorageInterface $storage,
  14.         private readonly CarImageThumbnailGenerator $thumbnailGenerator,
  15.         private readonly ?LoggerInterface $logger null,
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             VichEvents::POST_UPLOAD => 'onPostUpload',
  22.         ];
  23.     }
  24.     public function onPostUpload(Event $event): void
  25.     {
  26.         $object $event->getObject();
  27.         if (!$object instanceof CarImages) {
  28.             return;
  29.         }
  30.         $path $this->storage->resolvePath($object'imageFile');
  31.         if (!$path) {
  32.             return;
  33.         }
  34.         try {
  35.             $this->thumbnailGenerator->ensureThumbnails($path);
  36.         } catch (\Throwable $e) {
  37.             if ($this->logger) {
  38.                 $this->logger->warning('No se pudo generar la miniatura del vehĂ­culo', [
  39.                     'path' => $path,
  40.                     'message' => $e->getMessage(),
  41.                 ]);
  42.             }
  43.         }
  44.     }
  45. }