Commit 8a917266 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

UploadedFile: utilise un état plutôt qu'un booléen "orphelin".

Showing with 52 additions and 18 deletions
+52 -18
......@@ -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;
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment