<?php
namespace Boab\CmsBundle\EventListener;
use Boab\CmsBundle\Entity\PhotoInterface;
use Boab\CmsBundle\Event\PhotoDeleteEvent;
use Boab\CmsBundle\Event\EntityEvent;
use Boab\CmsBundle\Filesystem\FilesystemInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class PhotoManagerListener
{
private $filesystem;
public function __construct(FilesystemInterface $filesystem)
{
$this->filesystem = $filesystem;
}
public function onPhotoCreate(EntityEvent $event):void
{
$entity = $event->getEntity();
$uploadedFile = $entity->getUploadedFile();
if(!$entity instanceof PhotoInterface || !$uploadedFile instanceof UploadedFile){
return;
}
$newFileName = $this->filesystem->upload($uploadedFile, $entity->getUploadRoot());
$entity->setFileName($newFileName);
}
public function onPhotoDelete(PhotoDeleteEvent $event)
{
$entity = $event->getEntity();
//$thumbnailFile = $entity->getUploadRoot().'/'.$entity->getFileName();
$this->filesystem->delete($entity->getThumbnailPath());
}
}