src/EventListener/UploadListener.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Oneup\UploaderBundle\UploadEvents;
  7. use Oneup\UploaderBundle\Event\PostUploadEvent;
  8. use App\Entity\File;
  9. use App\Entity\Order;
  10. use Symfony\Component\Filesystem\Filesystem;
  11. use App\Override\Xthiago\PDFVersionConverter\src\Converter\GhostscriptConverterCommand;
  12. use App\Override\Xthiago\PDFVersionConverter\src\Converter\GhostscriptConverter;
  13. class UploadListener implements EventSubscriberInterface
  14. {
  15.     private $em;
  16.     private $projectDir;
  17.     public function __construct(EntityManagerInterface $em)
  18.     {
  19.         $this->em $em;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return array(
  24.             UploadEvents::POST_UPLOAD => 'onPostUpload',
  25.         );
  26.     }
  27.     public function onPostUpload(PostUploadEvent $event)
  28.     {
  29.         // get event config
  30.         $mappingConfig $event->getConfig();
  31.         $file_sent $event->getFile();
  32.         $request $event->getRequest();
  33.         $name $file_sent->getFileName();
  34.         $file $this->em->getRepository("App:File")->findOneBy(array('name' => $name));
  35.         if (!$file) {
  36.             $file = new File();
  37.             $file->setName($name);
  38.         }
  39.         switch ($event->getType()) {
  40.             case 'userPic':
  41.                 $token $request->query->get('token');
  42.                 $file->setEntityName('userPic');
  43.                 $file->setEntityId(null);
  44.                 $file->setToken($token);
  45.                 $file->setMappings('userPic');
  46.                 break;
  47.             default:
  48.                 break;
  49.         }
  50.         $this->em->persist($file);
  51.         $this->em->flush();
  52.     }
  53. }