/**
  * @param \FSi\DoctrineExtensions\Uploadable\File $file
  * @param null $prefix
  * @return string
  */
 protected function generatePath(File $file, $prefix = null)
 {
     if (!isset($prefix) && isset($this->filePrefix)) {
         $prefix = $this->filePrefix;
     }
     if (isset($prefix)) {
         $prefix = trim($prefix, '/');
         return $prefix . '/' . ltrim($file->getKey(), '/');
     }
     return $file->getKey();
 }
 function it_generate_url_for_fsi_file_with_base_url_set(File $file, UploadableFilesystem $filesystem, Local $adapter)
 {
     $file->getKey()->willReturn('TestFolder/File/file name.jpg');
     $file->getFilesystem()->willReturn($filesystem);
     $filesystem->getBaseUrl()->willReturn("http://domain.com/basepath/");
     $this->fileUrl($file)->shouldReturn('http://domain.com/basepath/TestFolder/File/file name.jpg');
 }
 function it_compute_file_asset_path(File $file, AssetsExtension $assets, FSiFilePathResolver $filePathResolver)
 {
     $file->getKey()->willReturn('file-name.txt');
     $filePathResolver->filePath($file, 'uploaded')->willReturn('/uploaded/file-name.txt');
     $assets->getAssetUrl('/uploaded/file-name.txt')->willReturn('/uploaded/file-name.txt');
     $this->fileAsset($file, 'uploaded')->shouldReturn('/uploaded/file-name.txt');
 }
 function it_generate_url_with_passed_prefix_even_if_there_is_a_prefix_in_globals(Twig_Environment $environment, File $file, Filesystem $filesystem, Local $adapter)
 {
     $environment->getGlobals()->willReturn(array('fsi_file_prefix' => 'uploaded'));
     $this->initRuntime($environment);
     $file->getKey()->shouldBeCalled()->willReturn('TestFolder/File/file.jpg');
     $file->getFilesystem()->shouldBeCalled()->willReturn($filesystem);
     $filesystem->getAdapter()->willReturn($adapter);
     $this->fileAsset($file, 'my_prefix')->shouldReturn('/my_prefix/TestFolder/File/file.jpg');
     $this->filePath($file, 'my_prefix')->shouldReturn('/my_prefix/TestFolder/File/file.jpg');
 }
 /**
  * Updating files keys.
  *
  * @param \Doctrine\Common\Persistence\ObjectManager $objectManager
  * @param \FSi\DoctrineExtensions\Uploadable\Mapping\ClassMetadata $uploadableMeta
  * @param object $object
  * @param \FSi\DoctrineExtensions\Mapping\Event\AdapterInterface $eventAdapter
  * @throws \FSi\DoctrineExtensions\Uploadable\Exception\RuntimeException
  */
 protected function updateFiles(ObjectManager $objectManager, $uploadableMeta, $object, AdapterInterface $eventAdapter)
 {
     $propertyObserver = $this->getPropertyObserver($objectManager);
     $id = $eventAdapter->extractIdentifier($objectManager, $object, false);
     $id = implode('-', $id);
     foreach ($uploadableMeta->getUploadableProperties() as $property => $config) {
         if (!$propertyObserver->hasSavedValue($object, $config['targetField']) || $propertyObserver->hasValueChanged($object, $config['targetField'])) {
             $accessor = PropertyAccess::getPropertyAccessor();
             $file = $accessor->getValue($object, $config['targetField']);
             $filesystem = $this->computeFilesystem($config);
             // Since file has changed, the old one should be removed.
             if ($oldKey = $accessor->getValue($object, $property)) {
                 if ($oldFile = $propertyObserver->getSavedValue($object, $config['targetField'])) {
                     $this->addToDelete($oldFile);
                 }
             }
             if (empty($file)) {
                 $accessor->setValue($object, $property, null);
                 $propertyObserver->saveValue($object, $config['targetField']);
                 continue;
             }
             if (!$this->getFileHandler()->supports($file)) {
                 throw new RuntimeException(sprintf('Can\'t handle resource of type "%s".', is_object($file) ? get_class($file) : gettype($file)));
             }
             $keymaker = $this->computeKeymaker($config);
             $keyLength = $this->computeKeyLength($config);
             $keyPattern = $config['keyPattern'] ? $config['keyPattern'] : null;
             $fileName = $this->getFileHandler()->getName($file);
             $newKey = $this->generateNewKey($keymaker, $object, $property, $id, $fileName, $keyLength, $keyPattern, $filesystem);
             $newFile = new File($newKey, $filesystem);
             $newFile->setContent($this->getFileHandler()->getContent($file));
             $accessor->setValue($object, $property, $newFile->getKey());
             // Save its current value, so if another update will be called, there won't be another saving.
             $propertyObserver->setValue($object, $config['targetField'], $newFile);
         }
     }
 }