<?php
namespace App\EventSubscriber;
use App\Entity\CarImages;
use App\Service\CarImageThumbnailGenerator;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Vich\UploaderBundle\Event\Event;
use Vich\UploaderBundle\Event\Events as VichEvents;
use Vich\UploaderBundle\Storage\StorageInterface;
final class CarImageUploadSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly StorageInterface $storage,
private readonly CarImageThumbnailGenerator $thumbnailGenerator,
private readonly ?LoggerInterface $logger = null,
) {
}
public static function getSubscribedEvents(): array
{
return [
VichEvents::POST_UPLOAD => 'onPostUpload',
];
}
public function onPostUpload(Event $event): void
{
$object = $event->getObject();
if (!$object instanceof CarImages) {
return;
}
$path = $this->storage->resolvePath($object, 'imageFile');
if (!$path) {
return;
}
try {
$this->thumbnailGenerator->ensureThumbnails($path);
} catch (\Throwable $e) {
if ($this->logger) {
$this->logger->warning('No se pudo generar la miniatura del vehĂculo', [
'path' => $path,
'message' => $e->getMessage(),
]);
}
}
}
}