src/EventListener/FilePathListener.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\User;
  4. use App\Services\TraitementUrl;
  5. use App\Entity\NeighborhoodEdit;
  6. use Vich\UploaderBundle\Event\Event;
  7. use Symfony\Component\Security\Core\Security;
  8. use App\Entity\FilePathInterface;
  9. class FilePathListener
  10. {
  11.     /**
  12.      * @var Security
  13.      */
  14.     private $security;
  15.     /**
  16.      * FilePathListener constructor.
  17.      * @param Security $security
  18.      */
  19.     public function __construct(Security $security)
  20.     {
  21.         $this->security $security;
  22.     }
  23.     /**
  24.      * @param Event $event
  25.      * @return void
  26.      */
  27.     public function onVichUploaderPreUpload(Event $event)
  28.     {
  29.         $object $event->getObject();
  30.         $mapping $event->getMapping();
  31.         if ((new \ReflectionClass($object))->implementsInterface(FilePathInterface::class)) {
  32.             if (method_exists($object'setAuthor')) {
  33.                 $object->setAuthor($this->security->getUser());
  34.             }
  35.             $object->setPath($mapping->getUriPrefix());
  36.         }
  37.         if ($object instanceof NeighborhoodEdit) {
  38.             $object->setMapping($mapping->getUriPrefix());
  39.         }
  40.     }
  41. }