Pārlūkot izejas kodu

[Uploadable] Minor tweaks

comfortablynumb 13 gadi atpakaļ
vecāks
revīzija
23312c647c

+ 37 - 59
lib/Gedmo/Uploadable/UploadableListener.php

@@ -115,9 +115,27 @@ class UploadableListener extends MappedEventSubscriber
         $fileInfoProp = $refl->getProperty($config['fileInfoProperty']);
         $fileInfoProp->setAccessible(true);
         $fileInfo = $fileInfoProp->getValue($object);
+
+        if (!$fileInfo) {
+            // Nothing to do
+
+            return;
+        }
+
+        $fileInfo = is_array($fileInfo) ? new FileInfoArray($fileInfo) : $fileInfo;
+
         $filePathField = $refl->getProperty($config['filePathField']);
         $filePathField->setAccessible(true);
-        $fileInfoCollection = $this->prepareFileInfoCollection($meta, $fileInfo);
+
+        if (!($fileInfo instanceof FileInfoInterface)) {
+            $msg = 'Property "%s" for class "%s" must contain either an array with information ';
+            $msg .= 'about an uploaded or a FileInfoInterface instance. ';
+
+            throw new \RuntimeException(sprintf($msg,
+                $fileInfoProp->getName(),
+                $meta->name
+            ));
+        }
 
         $path = $config['path'];
 
@@ -159,73 +177,33 @@ class UploadableListener extends MappedEventSubscriber
             $fileSizeField->setAccessible(true);
         }
 
-        foreach ($fileInfoCollection as $f) {
-            // First we remove the original file
-            $this->removefile($meta, $config, $object);
+        // First we remove the original file
+        $this->removefile($meta, $config, $object);
 
-            $info = $this->moveFile($f, $path, $config['allowOverwrite'], $config['appendNumber']);
-            $filePathField->setValue($object, $info['filePath']);
+        $info = $this->moveFile($fileInfo, $path, $config['allowOverwrite'], $config['appendNumber']);
+        $filePathField->setValue($object, $info['filePath']);
 
-            if ($config['callback'] !== '') {
-                $callbackMethod = $refl->getMethod($config['callback']);
-                $callbackMethod->setAccessible(true);
+        if ($config['callback'] !== '') {
+            $callbackMethod = $refl->getMethod($config['callback']);
+            $callbackMethod->setAccessible(true);
 
-                $callbackMethod->invokeArgs($object, array($config));
-            }
-
-            $changes = array(
-                $config['filePathField'] => array($filePathField->getValue($object), $info['filePath'])
-            );
-
-            if ($config['fileMimeTypeField']) {
-                $changes[$config['fileMimeTypeField']] = array($fileMimeTypeField->getValue($object), $info['fileMimeType']);
-            }
-
-            if ($config['fileSizeField']) {
-                $changes[$config['fileSizeField']] = array($fileSizeField->getValue($object), $info['fileSize']);
-            }
-
-            $uow->scheduleExtraUpdate($object, $changes);
-            $ea->setOriginalObjectProperty($uow, spl_object_hash($object), $config['filePathField'], $info['filePath']);
+            $callbackMethod->invokeArgs($object, array($config));
         }
-    }
 
-    /**
-     * Prepare the array of FileInfoInterface objects
-     *
-     * @param classMetadata
-     * @param mixed - Single array, array of arrays, FileInfoInterface or array of FileInfoInterface objects
-     *
-     * @return array - Array of FileInfoInterface objects
-     */
-    protected function prepareFileInfoCollection(ClassMetadata $meta, $fileInfo)
-    {
-        if (!$fileInfo || (is_array($fileInfo) && empty($fileInfo))) {
-            return array();
-        }
-
-        $fileInfoCollection = array();
-        $fileInfo = array($fileInfo);
-
-        foreach ($fileInfo as $value) {
-            if (is_array($value)) {
-                $fileInfoCollection[] = new FileInfoArray($value);
+        $changes = array(
+            $config['filePathField'] => array($filePathField->getValue($object), $info['filePath'])
+        );
 
-                break;
-            } else if (is_object($value) && $value instanceof FileInfoInterface) {
-                $fileInfoCollection[] = $fileInfo;
-            } else {
-                $msg = 'FileInfo for class "%s" must return either an array with information ';
-                $msg .= 'about an uploaded file (provided by $_FILES) or a FileInfoInterface object. ';
-                $msg .= 'You can even return an array of elements representing any of these two options.';
+        if ($config['fileMimeTypeField']) {
+            $changes[$config['fileMimeTypeField']] = array($fileMimeTypeField->getValue($object), $info['fileMimeType']);
+        }
 
-                throw new \RuntimeException(sprintf($msg,
-                    $meta->name
-                ));
-            }
+        if ($config['fileSizeField']) {
+            $changes[$config['fileSizeField']] = array($fileSizeField->getValue($object), $info['fileSize']);
         }
 
-        return $fileInfoCollection;
+        $uow->scheduleExtraUpdate($object, $changes);
+        $ea->setOriginalObjectProperty($uow, spl_object_hash($object), $config['filePathField'], $info['filePath']);
     }
 
     /**

+ 1 - 1
tests/Gedmo/Uploadable/Fixture/Entity/File.php

@@ -76,7 +76,7 @@ class File
         return $this->article;
     }
 
-    public function setFileInfo(array $fileInfo)
+    public function setFileInfo($fileInfo)
     {
         $this->fileInfo = $fileInfo;
     }

+ 1 - 1
tests/Gedmo/Uploadable/Fixture/Entity/Image.php

@@ -58,7 +58,7 @@ class Image
         return $this->filePath;
     }
 
-    public function setFileInfo(array $fileInfo)
+    public function setFileInfo($fileInfo)
     {
         $this->fileInfo = $fileInfo;
     }

+ 8 - 31
tests/Gedmo/Uploadable/UploadableEntityTest.php

@@ -9,7 +9,8 @@ use Tool\BaseTestCaseORM,
     Uploadable\Fixture\Entity\Article,
     Uploadable\Fixture\Entity\File,
     Uploadable\Fixture\Entity\FileWithoutPath,
-    Gedmo\Uploadable\Stub\UploadableListenerStub;
+    Gedmo\Uploadable\Stub\UploadableListenerStub,
+    Gedmo\Uploadable\FileInfo\FileInfoArray;
 
 /**
  * These are tests for Uploadable behavior
@@ -104,7 +105,8 @@ class UploadableEntityTest extends BaseTestCaseORM
         $fileInfo['tmp_name'] = $this->testFile2;
         $fileInfo['name'] = $this->testFilename2;
 
-        $image2->setFileInfo($fileInfo);
+        // We use a FileInfoInterface instance here
+        $image2->setFileInfo(new FileInfoArray($fileInfo));
 
         // For now, we need to force the update changing one of the managed fields. If we don't do this,
         // entity won't be marked for update
@@ -175,6 +177,10 @@ class UploadableEntityTest extends BaseTestCaseORM
     {
         $file = new FileWithoutPath();
 
+        $fileInfo = $this->generateUploadedFile();
+
+        $file->setFileInfo($fileInfo);
+
         $this->em->persist($file);
         $this->em->flush();
     }
@@ -210,27 +216,6 @@ class UploadableEntityTest extends BaseTestCaseORM
         $this->assertTrue($file->callbackWasCalled);
     }
 
-    /**
-      * @dataProvider prepareFileInfoCollectionMethodExceptionsProvider
-      */
-    public function testPrepareFileInfoCollectionMethodExceptions($fileInfo)
-    {
-        $this->setExpectedException('RuntimeException');
-
-        $meta = $this->em->getClassMetadata('Uploadable\Fixture\Entity\File');
-
-        $refl = new \ReflectionClass(get_class($this->listener));
-        $method = $refl->getMethod('prepareFileInfoCollection');
-        $method->setAccessible(true);
-
-        $method->invokeArgs($this->listener, array($meta, $fileInfo));
-    }
-
-    public function testPrepareFileInfoCollectionMethod()
-    {
-
-    }
-
     /**
      * @dataProvider uploadExceptionsProvider
      */
@@ -250,14 +235,6 @@ class UploadableEntityTest extends BaseTestCaseORM
 
     // Data Providers
 
-    public function prepareFileInfoCollectionMethodExceptionsProvider()
-    {
-        return array(
-            array(array('size' => 123)),
-            array(new \DateTime())
-        );
-    }
-
     public function uploadExceptionsProvider()
     {
         return array(