lib/boab/cms-bundle/src/EventListener/PhotoManagerListener.php line 32

Open in your IDE?
  1. <?php
  2. namespace Boab\CmsBundle\EventListener;
  3. use Boab\CmsBundle\Entity\PhotoInterface;
  4. use Boab\CmsBundle\Event\PhotoDeleteEvent;
  5. use Boab\CmsBundle\Event\EntityEvent;
  6. use Boab\CmsBundle\Filesystem\FilesystemInterface;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile;
  8. class PhotoManagerListener
  9. {
  10.     private $filesystem;
  11.     public function __construct(FilesystemInterface $filesystem)
  12.     {
  13.         $this->filesystem $filesystem;
  14.     }
  15.     public function onPhotoCreate(EntityEvent $event):void
  16.     {    
  17.         $entity $event->getEntity();
  18.         $uploadedFile $entity->getUploadedFile();
  19.         if(!$entity instanceof PhotoInterface || !$uploadedFile instanceof UploadedFile){
  20.             return;
  21.         }
  22.         $newFileName $this->filesystem->upload($uploadedFile$entity->getUploadRoot());
  23.         $entity->setFileName($newFileName);   
  24.     }
  25.     
  26.     public function onPhotoDelete(PhotoDeleteEvent $event)
  27.     {
  28.         $entity $event->getEntity();
  29.         //$thumbnailFile = $entity->getUploadRoot().'/'.$entity->getFileName();
  30.         $this->filesystem->delete($entity->getThumbnailPath());
  31.     }
  32. }