<?php
namespace App\EventListener;
use App\Entity\User;
use App\Services\TraitementUrl;
use App\Entity\NeighborhoodEdit;
use Vich\UploaderBundle\Event\Event;
use Symfony\Component\Security\Core\Security;
use App\Entity\FilePathInterface;
class FilePathListener
{
/**
* @var Security
*/
private $security;
/**
* FilePathListener constructor.
* @param Security $security
*/
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* @param Event $event
* @return void
*/
public function onVichUploaderPreUpload(Event $event)
{
$object = $event->getObject();
$mapping = $event->getMapping();
if ((new \ReflectionClass($object))->implementsInterface(FilePathInterface::class)) {
if (method_exists($object, 'setAuthor')) {
$object->setAuthor($this->security->getUser());
}
$object->setPath($mapping->getUriPrefix());
}
if ($object instanceof NeighborhoodEdit) {
$object->setMapping($mapping->getUriPrefix());
}
}
}