From 0c8da1b9e4a2def6d36c0c83c3dde6d019398280 Mon Sep 17 00:00:00 2001
From: Cristiano Magro <cristiano.magro@vola.it>
Date: Tue, 16 Jan 2018 19:15:15 +0100
Subject: [PATCH] Refactor decodifico il path dalla mappa e dalla request

---
 tests/Vola/UploadFile/FileUpTest.php |    2 +-
 src/Vola/UploadFile/FileUp.php       |   55 ++++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/src/Vola/UploadFile/FileUp.php b/src/Vola/UploadFile/FileUp.php
index e96add0..909e1ec 100644
--- a/src/Vola/UploadFile/FileUp.php
+++ b/src/Vola/UploadFile/FileUp.php
@@ -34,22 +34,41 @@
     }
 
     /**
-     * Imposto i path di destinazione del file
-     * @param string $cfgClassName nome della classe con i parametri di configurazione da utilizzare
-     * @return \Upload_FileUp
+     * Imposto i path di destinazione del file.
+     *
+     * Ricavo dalla mappa il path su cui memorizzare il file corrente, sulla base del parametro
+     * passato nella Request. Se non trovo corrispondenza viene lanciata eccezione.
+     * @param array $cfgPathMap elenco percorsi validi per la memorizzazione.
+     * @return \Upload_FileUp fluent style
      * @throws Upload_Exc_Error
      */
-    public function setUploadPath(array $cfgPathKey)
+    public function decodeUploadPath(array $cfgPathMap)
     {
+        $this->pathMap = $cfgPathMap;
+        $key = $this->getRequestValue('dirDestinazione', null);
 
-        $key = $this->request->dirDestinazione;
-        if (isset($cfgPathKey[$key])) {
-            $path = $cfgPathKey[$key];
-            $this->request->pathForUpload = filter_input(INPUT_SERVER, 'DOCUMENT_ROOT') . '/../' . $path;
-        } else {
-            throw new Upload_Exc_Error('Decodifica chiave percorso non definita');
-        }
+        $path = $this->getMapValue($key);
+        $pathDocumentRoot = filter_input(INPUT_SERVER, 'DOCUMENT_ROOT');
+
+        //genero il percorso reale dove andrà salvato il file
+        $this->request->pathForUpload =  $pathDocumentRoot. '/../' . $path;
         return $this;
+    }
+
+    /**
+     * Ritorno il valore della Request preso dalla chiamata.
+     *
+     * Se non è definito ritorno il valore di default|null
+     * @param string $nomeParam
+     * @param string $default
+     * @return mixed null|valore chiamata
+     */
+    public function getRequestValue($nomeParam, $default = null)
+    {
+        if (isset($this->request->{$nomeParam})) {
+            return $this->request->{$nomeParam};
+        }
+        return $default;
     }
 
     /**
@@ -116,5 +135,19 @@
         }
     }
 
+    /**
+     * Decodifico mediante la mappa il percorso da utilizzare.
+     * @param $chiave della Request per il percorso
+     * @return mixed  path associato
+     * @throws Upload_Exc_Error
+     * @since 0.3.2
+     */
+    protected function getMapValue($chiave)
+    {
+        if (!isset($cfgPathMap[$chiave])) {
+            throw new Upload_Exc_Error('Decodifica chiave percorso non definita');
+        }
+        return $cfgPathMap[$chiave];
+    }
 
 }
diff --git a/tests/Vola/UploadFile/FileUpTest.php b/tests/Vola/UploadFile/FileUpTest.php
index 538d077..b70e46e 100644
--- a/tests/Vola/UploadFile/FileUpTest.php
+++ b/tests/Vola/UploadFile/FileUpTest.php
@@ -40,7 +40,7 @@
      * @expectedException  Upload_Exc_Error
      */
     public function testExcepitonNessunaMappaPerDecodifica(){
-        $this->object->setUploadPath(array());
+        $this->object->decodeUploadPath(array());
     }
 
 }
\ No newline at end of file

--
Gitblit v1.8.0