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\Contract\FileUploaderInterface;
  4. use Boab\CmsBundle\Entity\PhotoInterface;
  5. use Boab\CmsBundle\Event\PhotoDeleteEvent;
  6. use Boab\CmsBundle\Event\EntityEvent;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile;
  8. class PhotoManagerListener
  9. {
  10.     private $fileUploader;
  11.     public function __construct(FileUploaderInterface $fileUploader)
  12.     {
  13.         $this->fileUploader $fileUploader;
  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->fileUploader->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->fileUploader->delete($thumbnailFile);
  31.     }
  32. }