From 8a91726675d9b11a530f3ffa2336062b4eb07296 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Thu, 22 Jan 2015 10:12:11 +0100
Subject: [PATCH] =?UTF-8?q?UploadedFile:=20utilise=20un=20=C3=A9tat=20plut?=
 =?UTF-8?q?=C3=B4t=20qu'un=20bool=C3=A9en=20"orphelin".?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Entity/UploadedFile.php | 70 ++++++++++++++++++++++++++++++-----------
 1 file changed, 52 insertions(+), 18 deletions(-)

diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php
index b024385c..55038e65 100644
--- a/Entity/UploadedFile.php
+++ b/Entity/UploadedFile.php
@@ -12,6 +12,7 @@ use Doctrine\ORM\Mapping as ORM;
 use Gaufrette\Filesystem;
 use Gaufrette\Stream;
 use Gaufrette\StreamMode;
+use InvalidArgumentException;
 use Rhumsaa\Uuid\Uuid;
 
 /**
@@ -20,6 +21,12 @@ use Rhumsaa\Uuid\Uuid;
  */
 class UploadedFile
 {
+    const ETAT_EN_COURS = 'en-cours';
+    const ETAT_ORPHELIN = 'orphelin';
+    const ETAT_NORMAL   = 'normal';
+    const ETAT_CORROMPU = 'corrompu';
+    const ETAT_MANQUANT = 'manquant';
+
     /**
      * @ORM\Id
      * @ORM\Column(type="guid")
@@ -59,10 +66,10 @@ class UploadedFile
     private $checksum;
 
     /**
-     * @ORM\Column(type="boolean", length=64)
-     * @var boolean
+     * @ORM\Column(type="string", length=10)
+     * @var string
      */
-    private $orphelin;
+    private $etat = self::ETAT_EN_COURS;
 
     /**
      * @ORM\Column(type="datetime")
@@ -87,7 +94,6 @@ class UploadedFile
     public function __construct()
     {
         $this->id = Uuid::uuid4()->toString();
-        $this->orphelin = true;
         $this->path = "orphan/".$this->id;
         $this->createdAt = new DateTime('now');
     }
@@ -218,26 +224,29 @@ class UploadedFile
     }
 
     /**
-     * Set orphelin
+     * Set etat
      *
-     * @param boolean $orphelin
+     * @param string $etat
      * @return UploadedFile
      */
-    public function setOrphelin($orphelin)
+    public function setEtat($etat)
     {
-        $this->orphelin = $orphelin;
+        if(!in_array($etat, [self::ETAT_CORROMPU, self::ETAT_EN_COURS, self::ETAT_MANQUANT, self::ETAT_NORMAL, self::ETAT_ORPHELIN])) {
+            throw new InvalidArgumentException(sprintf("Etat invalide: '%s'", (string)$etat));
+        }
+        $this->etat = $etat;
 
         return $this;
     }
 
     /**
-     * Get orphelin
+     * Get etat
      *
-     * @return boolean
+     * @return string
      */
-    public function isOrphelin()
+    public function getEtat()
     {
-        return $this->orphelin;
+        return $this->etat;
     }
 
     /**
@@ -301,17 +310,42 @@ class UploadedFile
         return $this;
     }
 
+    public function validate()
+    {
+        if (self::ETAT_EN_COURS !== $this->getEtat()) {
+            return;
+        }
+
+        $fs = $this->filesystem;
+        $path = $this->getPath();
+
+        if (!$fs->has($path)) {
+            $this->setEtat(self::ETAT_MANQUANT);
+            return;
+        }
+
+        if ($fs->size($path) !== $this->size || $fs->checksum($path) !== $this->checksum) {
+            $this->setEtat(self::ETAT_CORROMPU);
+            return;
+        }
+    }
+
     /**
+     *
      * @return boolean
      */
     public function isValid()
     {
-        $fs = $this->filesystem;
-        $path = $this->getPath();
-        return
-            $fs->has($path) &&
-            (null === $this->size || $fs->size($path) === $this->size) &&
-            (null === $this->checksum || $fs->checksum($path) === $this->checksum);
+        return $this->getEtat() === self::ETAT_ORPHELIN || $this->getEtat() === self::ETAT_NORMAL;
+    }
+
+    /**
+     *
+     * @return boolean
+     */
+    public function isOrphelin()
+    {
+        return $this->getEtat() === self::ETAT_ORPHELIN;
     }
 
     /**
-- 
GitLab