<?php
namespace App\EventListener;
use App\Entity\Enums\JvnMediaTypeEnum;
use App\Entity\JvnMedia;
use App\Handler\ThumbnailHandler;
use App\Helpers\Utils;
use Imagine\Image\Box;
use League\Flysystem\FileExistsException;
use League\Flysystem\FilesystemInterface;
use Symfony\Component\Security\Core\Security;
use Vich\UploaderBundle\Event\Event;
use Vich\UploaderBundle\Storage\StorageInterface;
class ImageUploadListener
{
private ThumbnailHandler $thumbnailHandler;
private StorageInterface $vichStorage;
private FilesystemInterface $mediaStorage;
private Security $security;
public function __construct(
ThumbnailHandler $thumbnailHandler,
StorageInterface $vichStorage,
FilesystemInterface $mediaStorage,
Security $security
)
{
$this
->setThumbnailHandler($thumbnailHandler)
->setVichStorage($vichStorage)
->setMediaStorage($mediaStorage)
->setSecurity($security)
;
}
/**
* @param Event $event
* @throws FileExistsException
*/
public function onPostUpload(Event $event)
{
/** @var JvnMedia $image */
$image = $event->getObject();
if( $image->getType() === JvnMediaTypeEnum::get(JvnMediaTypeEnum::PHOTO) ) {
$thumbnail = clone $image;
$thumbnail->setFilename(Utils::thumbnailName($image->getFilename()));
$image = $this
->getThumbnailHandler()
->getImagine()
->read($this->getVichStorage()->resolveStream($image, 'file'))
->resize(
new Box($this->getThumbnailHandler()->getWidth(), $this->getThumbnailHandler()->getHeight())
)
->strip()
;
$thumbnailFile = $this->getVichStorage()->resolvePath($thumbnail, 'file');
// Vich Uploader use prefix and to avoid double prefix we need to reset
$this->getMediaStorage()->getAdapter()->setPathPrefix(null);
if( ! $this->getMediaStorage()->has($thumbnailFile) ) {
$this->getMediaStorage()->write($thumbnailFile, $image->get('jpeg'));
}
// We unset thumbnail object to avoid problems with ORM persistence
unset($thumbnail);
}
}
/**
* @return ThumbnailHandler
*/
public function getThumbnailHandler(): ThumbnailHandler {
return $this->thumbnailHandler;
}
/**
* @param ThumbnailHandler $thumbnailHandler
* @return ImageUploadListener
*/
public function setThumbnailHandler(ThumbnailHandler $thumbnailHandler): ImageUploadListener {
$this->thumbnailHandler = $thumbnailHandler;
return $this;
}
/**
* @return StorageInterface
*/
public function getVichStorage(): StorageInterface {
return $this->vichStorage;
}
/**
* @param StorageInterface $vichStorage
* @return ImageUploadListener
*/
public function setVichStorage(StorageInterface $vichStorage): ImageUploadListener {
$this->vichStorage = $vichStorage;
return $this;
}
/**
* @return FilesystemInterface
*/
public function getMediaStorage(): FilesystemInterface {
return $this->mediaStorage;
}
/**
* @param FilesystemInterface $mediaStorage
* @return ImageUploadListener
*/
public function setMediaStorage(FilesystemInterface $mediaStorage): ImageUploadListener {
$this->mediaStorage = $mediaStorage;
return $this;
}
/**
* @return Security
*/
public function getSecurity(): Security {
return $this->security;
}
/**
* @param Security $security
* @return ImageUploadListener
*/
public function setSecurity(Security $security): ImageUploadListener {
$this->security = $security;
return $this;
}
}