/**
  * @param PostPersistEvent $event
  * @return AbstractResponse
  * @throws \UnexpectedValueException|\InvalidArgumentException
  */
 public function onUpload(PostPersistEvent $event)
 {
     $file = $event->getFile();
     if (!$file instanceof GaufretteFile) {
         $class = get_class($file);
         throw new \UnexpectedValueException("Only gaufrette files are supported, '{$class}' given");
     }
     $originalFilename = $file->getName();
     try {
         // Couldn't find anything better with OneUp uploader...
         $originalFiles = $event->getRequest()->files->all();
         if (isset($originalFiles['files'])) {
             $originalFiles = $originalFiles['files'];
             if (count($originalFiles)) {
                 $originalFile = array_pop($originalFiles);
                 $originalFilename = $originalFile->getClientOriginalName();
             }
         }
     } catch (\Exception $e) {
     }
     $file = $this->resourceManager->addFile($file, $originalFilename, $event->getType());
     /** @var AbstractResponse $response */
     $response = $event->getResponse();
     $response[] = $file;
     return $response;
 }
 /**
  * @param LifecycleEventArgs $args
  * @throws \UnexpectedValueException
  */
 public function preRemove(LifecycleEventArgs $args)
 {
     $entity = $args->getObject();
     if ($entity instanceof ResourceInterface) {
         $this->resourceManager->removeResourceFile($entity);
         return;
     }
 }
 /**
  * @param OptionsResolver $resolver
  * @throws \Symfony\Component\OptionsResolver\Exception\AccessException
  * @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  * @throws \UnexpectedValueException
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $resolver->setRequired(['resource_type']);
     $resolver->setDefaults(['class' => null, 'repository' => null, 'compound' => false, 'error_bubbling' => false]);
     $resolver->setNormalizer('resource_type', function (Options $options, $value) {
         return $this->resourceManager->getResourceTypeConfiguration($value);
     });
     $resolver->setNormalizer('class', function (Options $options, $value) {
         $resourceType = $options['resource_type'];
         if (!$value && $resourceType instanceof ResourceTypeConfiguration) {
             return $resourceType->getEntity();
         }
         return $value;
     });
     $resolver->setNormalizer('repository', function (Options $options, $value) {
         if ($value) {
             if ($value instanceof ObjectRepository) {
                 return $value;
             }
             throw new \UnexpectedValueException("The 'repository' option must be an EntityRepository");
         }
         $class = $options['class'];
         if (!$class) {
             throw new \UnexpectedValueException("Missing option 'class' or 'repository'");
         }
         return $this->doctrine->getRepository($class);
     });
 }
 /**
  * @param ResourceTypeConfiguration $resourceConfiguration
  *
  * @throws \UnexpectedValueException
  *
  * @return array
  */
 protected function getFileNames(ResourceTypeConfiguration $resourceConfiguration)
 {
     $fs = $this->resourceManager->getFilesystemForType($resourceConfiguration->getCode());
     return $fs->keys();
 }
 /**
  * @param ResourceInterface|null $resource
  * @param string                 $action
  * @param bool                   $absolute
  * @return string
  * @throws \Exception
  */
 public function getResourcePath(ResourceInterface $resource = null, $action = 'download', $absolute = false)
 {
     return $this->resourceManager->getFileUrl($resource, $action, $absolute);
 }