Improvements based on import test.
authorCameron Ball <cameron@getapproved.com.au>
Thu, 11 Dec 2014 07:28:39 +0000 (15:28 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Thu, 11 Dec 2014 07:28:39 +0000 (15:28 +0800)
.gitignore
Controllers/FileController.php
Controllers/SimfileController.php
Services/IStatusReporter.php [new file with mode: 0644]
Services/StatusReporter.php [new file with mode: 0644]
Services/Uploads/UploadManager.php
Services/UserSession.php
Services/ZipParser.php
config/DI.php
config/app.php.example [new file with mode: 0644]
public_html/index.php

index 008e5e6..575a306 100644 (file)
@@ -5,3 +5,4 @@ vendor
 db.php
 files
 FacebookApp.php
+app.php
index 6fe626e..c50a623 100644 (file)
@@ -6,6 +6,7 @@ use Controllers\IDivineController;
 use Services\Http\IHttpRequest;
 use Services\Http\IHttpResponse;
 use Services\IUserSession;
+use Services\IStatusReporter;
 use Services\IUserQuota;
 use Domain\Entities\IDownloadFactory;
 use DataAccess\IFileRepository;
@@ -20,6 +21,7 @@ class FileController implements IDivineController
     private $_downloadFactory;
     private $_userSession;
     private $_userQuota;
+    private $_statusReporter;
     
     public function __construct(
         IHttpRequest $request,
@@ -28,7 +30,8 @@ class FileController implements IDivineController
         IDownloadFactory $downloadFactory,
         IDownloadRepository $downloadRepository,
         IUserSession $userSession,
-        IUserQuota $userQuota
+        IUserQuota $userQuota,
+        IStatusReporter $statusReporter
     ) {
         $this->_request = $request;
         $this->_response = $response;
@@ -37,6 +40,7 @@ class FileController implements IDivineController
         $this->_downloadFactory = $downloadFactory;
         $this->_userSession = $userSession;
         $this->_userQuota = $userQuota;
+        $this->_statusReporter = $statusReporter;
     }
     
     public function indexAction() {
@@ -106,16 +110,16 @@ class FileController implements IDivineController
     private function notAuthorised()
     {
         $this->_response->setHeader('Content-Type', 'application/json')
-            ->setBody(json_encode(array('error' => 'You must be authenticated to download files')))
-            ->sendResponse();
+                        ->setBody($this->_statusReporter->error('You must be authenticated to download files')->json())
+                        ->sendResponse();
         exit();
     }
     
     private function notEnoughQuota()
     {
         $this->_response->setHeader('Content-Type', 'application/json')
-            ->setBody(json_encode(array('error' => 'You don\'t have enough quota remaining for this file.')))
-            ->sendResponse();
+                        ->setBody($this->_statusReporter->error('You don\'t have enough quota remaining for this file.')->json())
+                        ->sendResponse();
         exit();
     }
 }
index 06fe869..b9ca15e 100644 (file)
@@ -2,12 +2,14 @@
 \r
 namespace Controllers;\r
 \r
+use Exception;\r
 use Controllers\IDivineController;\r
 use Services\Http\IHttpResponse;\r
 use Services\Uploads\IUploadManager;\r
 use Services\IUserSession;\r
 use Services\IZipParser;\r
 use Services\ISMOMatcher;\r
+use Services\IStatusReporter;\r
 use DataAccess\StepMania\ISimfileRepository;\r
 use DataAccess\StepMania\IPackRepository;\r
 use DataAccess\IFileRepository;\r
@@ -27,6 +29,8 @@ class SimfileController implements IDivineController
     private $_zipParser;\r
     private $_smoMatcher;\r
     private $_downloadRepository;\r
+    private $_statusReporter;\r
+    private $_userSession;\r
     \r
     public function __construct(\r
         IHttpResponse $response,\r
@@ -37,7 +41,8 @@ class SimfileController implements IDivineController
         IUserSession $userSession,\r
         IZipParser $zipParser,\r
         ISMOMatcher $smoMatcher,\r
-        IDownloadRepository $downloadRepository\r
+        IDownloadRepository $downloadRepository,\r
+        IStatusReporter $statusReporter\r
     ) {\r
         $this->_response = $response;\r
         $this->_uploadManager = $uploadManager;\r
@@ -47,6 +52,8 @@ class SimfileController implements IDivineController
         $this->_zipParser = $zipParser;\r
         $this->_smoMatcher = $smoMatcher;\r
         $this->_downloadRepository = $downloadRepository;\r
+        $this->_statusReporter = $statusReporter;\r
+        $this->_userSession = $userSession;\r
     }\r
     \r
     public function indexAction() {\r
@@ -147,7 +154,9 @@ class SimfileController implements IDivineController
     }\r
     \r
     public function uploadAction()\r
-    {                       \r
+    {\r
+        if(!$this->_userSession->getCurrentUser()) $this->_statusReporter->error('You must be authenticated to upload files');\r
+        \r
         //TODO: Put directory in config ?\r
         $files = $this->_uploadManager->setFilesDirectory('../files')\r
                                       ->setDestination('StepMania/')\r
@@ -157,7 +166,9 @@ class SimfileController implements IDivineController
         {\r
             $zipParser = $this->_zipParser;\r
             $zipParser->parse($file);\r
-                        \r
+                   \r
+            if(!$zipParser->simfiles()) $this->_statusReporter->error('That zip doesn\'t seem to have any simfiles in it.');\r
+            \r
             //save the actual zip in the db\r
             $this->findAndAddSmoMirror($file);\r
             $this->_fileRepository->save($file);  \r
@@ -181,6 +192,8 @@ class SimfileController implements IDivineController
                 $this->_simfileRepository->save($simfile);\r
             }\r
         }\r
+        \r
+        $this->_statusReporter->success('Uploaded succesfully');\r
     }\r
     \r
     private function getPackMirrorsArray(IPack $pack)\r
diff --git a/Services/IStatusReporter.php b/Services/IStatusReporter.php
new file mode 100644 (file)
index 0000000..97d5da8
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+namespace Services;
+
+use Exception;
+
+interface IStatusReporter
+{
+    public function success($message);
+    public function error($message);
+    public function exception(Exception $exception);
+    public function json();
+}
diff --git a/Services/StatusReporter.php b/Services/StatusReporter.php
new file mode 100644 (file)
index 0000000..b36a53d
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+namespace Services;
+
+use Exception;
+use Services\Http\IHttpResponse;
+use Services\IStatusReporter;
+
+class StatusReporter implements IStatusReporter
+{
+    private $_message;
+    private $_type;
+    private $_response;
+    
+    const ERROR = 'error';
+    const SUCCESS = 'success';
+    const EXCEPTION = 'exception';
+    
+    public function __construct(IHttpResponse $response) {
+        $this->_response = $response;
+    }
+    
+    public function error($message)
+    {
+        $this->_message = $message;
+        $this->_type = self::ERROR;
+        $this->_response->setHeader('Content-Type', 'application/json')
+                        ->setBody($this->json())
+                        ->sendResponse();
+        exit();
+    }
+    
+    public function success($message)
+    {
+        $this->_message = $message;
+        $this->_type = self::SUCCESS;
+        $this->_response->setHeader('Content-Type', 'application/json')
+                        ->setBody($this->json())
+                        ->sendResponse();
+        exit();
+    }
+    
+    //no need to exit here, exceptions stop the program.
+    public function exception(Exception $exception)
+    {       
+        //we'll be instatic context here so I have to do it this way.
+        header('Content-Type: application/json');
+        echo json_encode(array(self::EXCEPTION => $exception->getMessage()));
+    }
+    
+    public function json()
+    {
+        return json_encode(
+            array($this->_type => $this->_message)
+        );
+    }
+}
index 8530c2b..81c12c7 100644 (file)
@@ -92,12 +92,12 @@ class UploadManager implements IUploadManager{
             
             /* @var $file \Services\Uploads\IFile */
             $results[] = $this->_fileBuilder->With_Hash($hash)
-                                       ->With_Path(rtrim($this->_destination, '/'))
-                                       ->With_Filename($file->getName())
-                                       ->With_Mimetype($file->getType())
-                                       ->With_Size($file->getSize())
-                                       ->With_UploadDate(time())
-                                       ->build();
+                                            ->With_Path(rtrim($this->_destination, '/'))
+                                            ->With_Filename($file->getName())
+                                            ->With_Mimetype($file->getType())
+                                            ->With_Size($file->getSize())
+                                            ->With_UploadDate(time())
+                                            ->build();
         }
         
         return $results;
index 04505a0..ead323f 100644 (file)
@@ -27,17 +27,19 @@ class UserSession implements IUserSession
 
     private function findToken()
     {        
-        if($this->_request->isPost())
-        {
+        //if($this->_request->isPost())
+        //{
+        //Try post
             $request = $this->_request->post();
             if(!empty($request['token'])) return $request['token'];
-        }
+        //}
         
-        if($this->_request->isGet())
-        {
+        //if($this->_request->isGet())
+        //{
+        //Try get
             $request = $this->_request->get();
             if(!empty($request['token'])) return $request['token'];
-        }
+        //}
         
         //no good, try the body
         $body = json_decode($this->_request->getBody(), true);
index d0418a7..f3a55f4 100644 (file)
@@ -98,7 +98,8 @@ class ZipParser implements IZipParser
         //or single, but to do that we simply check the number of found sm files. To overcome this
         //first populate the smFiles array with the raw sm data, then apply SmDataToSmClass on each
         //array element. This way the check is accurate and the array gets populated as expected.
-        $this->_smFiles = array_map(array($this, 'SmDataToSmClass'), $this->_smFiles);
+        //@ing it because when exceptions are thrown this still produces a warning and I dont want that.
+        $this->_smFiles = @array_map(array($this, 'SmDataToSmClass'), $this->_smFiles);
     }
     
     private function packNameFromFiles()
index 5d0838b..bbfd1d7 100644 (file)
@@ -41,6 +41,7 @@ return [
     'Services\IZipParser'                                 => DI\object('Services\ZipParser'),\r
     'Services\IBannerExtracter'                           => DI\object('Services\BannerExtracter'),\r
     'Services\ISMOMatcher'                                => DI\object('Services\SMOMatcher'),\r
+    'Services\IStatusReporter'                            => DI\object('Services\StatusReporter'),\r
     \r
     //DA\r
     'DataAccess\StepMania\ISimfileRepository'             => DI\object('DataAccess\StepMania\SimfileRepository'),\r
diff --git a/config/app.php.example b/config/app.php.example
new file mode 100644 (file)
index 0000000..3b505ae
--- /dev/null
@@ -0,0 +1,6 @@
+<?php
+
+return [
+    'allow-origin' => 'http://roll.divinelegy.meeples:8000',
+    'mode'         => 'production'
+];
\ No newline at end of file
index d4843d3..2aa5c39 100644 (file)
@@ -1,12 +1,15 @@
 <?php\r
 \r
-//TODO: Config this\r
-header("Access-Control-Allow-Origin: http://172.17.12.110:8000");\r
-header("Access-Control-Allow-Origin: http://roll.divinelegy.meeples:8000");\r
-//header("Access-Control-Allow-Origin: http://roll.divinelegy.dev:8000");\r
-\r
 require_once('../vendor/autoload.php');\r
 \r
+$config = require('../config/app.php');\r
+\r
+// Allow these origins to do cross domain JS.\r
+header("Access-Control-Allow-Origin: " . $config['allow-origin']);\r
+\r
+// Nice exceptions\r
+if($config['mode'] == 'production') set_exception_handler(array('\Services\StatusReporter', 'exception'));\r
+\r
 // Everything time related should be UTC+0 based\r
 date_default_timezone_set('UTC');\r
 \r