lib/boab/cms-bundle/src/EventListener/FileUploadListener.php line 34

Open in your IDE?
  1. <?php
  2. namespace Boab\CmsBundle\EventListener;
  3. use Boab\CmsBundle\Event\EntityEvent;
  4. use Boab\CmsBundle\Util\FileUploader;
  5. use Boab\CmsBundle\Event\TermPrePersistEvent;
  6. use Boab\CmsBundle\Entity\FileUploadInterface;
  7. use Boab\CmsBundle\Event\ContentPreUpdateEvent;
  8. use Symfony\Component\HttpFoundation\File\UploadedFile;
  9. use Boab\CmsBundle\Event\ContentPrePersistEvent;
  10. use Boab\CmsBundle\Entity\PhotoInterface;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. class FileUploadListener
  13. {
  14.     
  15.     private $fileUploader;
  16.     public function __construct(FileUploader $fileUploader)
  17.     {
  18.         $this->fileUploader $fileUploader;
  19.     }
  20.     public function onPrePersist(ContentPrePersistEvent $event)
  21.     {
  22.         $entity $event->getEntity();
  23.         if(!$entity instanceof FileUploadInterface){
  24.             return;
  25.         }
  26.         $this->saveFile($entity);
  27.     }
  28.     public function onPreUpdate(ContentPreUpdateEvent $event)
  29.     {
  30.         $entity $event->getEntity();
  31.         if(!$entity instanceof FileUploadInterface){
  32.             return;
  33.         }
  34.         $this->saveFile($entity);
  35.     }
  36.     public function onPrePersistTerm(TermPrePersistEvent $event)
  37.     {
  38.         $entity $event->getTerm();
  39.         if(!$entity instanceOf FileUploadInterface){
  40.             return;
  41.         }
  42.         $this->saveFile($entity);     
  43.     }
  44.     public function onPhotoCreate(EntityEvent $event) : void
  45.     {    
  46.         $entity $event->getEntity();
  47.         $photo $entity->getUploadedFile();
  48.         if(!$entity instanceof PhotoInterface || !$photo instanceof UploadedFile){
  49.             return ;
  50.         }
  51.         $oldFileName $entity->getFileName();
  52.         $file $entity->getUploadedFile();
  53.         $newFileName $this->fileUploader->upload($file$entity->getUploadRoot());
  54.         $entity->setFileName($newFileName);
  55.         $oldThumbnail $entity->getUploadRoot().'/'.$oldFileName;
  56.         $this->fileUploader->deleteFile($oldThumbnail);
  57.     }    
  58.     private function saveFile(FileUploadInterface $entity)
  59.     {
  60.         $uploadedFile $entity->getUploadedFile();
  61.         if($uploadedFile instanceof UploadedFile){
  62.             $oldThumbnail $entity->getThumbnailPath();
  63.             /*
  64.             if(!$this->isDefaultThumbnail($oldThumbnail)){
  65.                 $oldFile =  $oldThumbnail->getPathname();
  66.                 $this->fileUploader->deleteFile($oldFile);
  67.             }
  68.             */
  69.             $destination $entity->getUploadRoot();
  70.             $fileName $this->fileUploader->upload($uploadedFile$destination);
  71.             $entity->setThumbnail($fileName);
  72.             return;
  73.         }  
  74.         /**  
  75.          * Reset thumbnail to normal filename and ignore default thumbnail
  76.         */
  77.         $thumbnail $entity->getThumbnail();
  78.         $fileName $thumbnail instanceof File $thumbnail->getBasename() : $thumbnail;
  79.         if($fileName == FileUploader::DEFAULT_THUMBNAIL){
  80.             $entity->setThumbnail(null);
  81.             return;
  82.         }
  83.         $entity->setThumbnail($fileName);
  84.     }
  85. }