Changes to entities. File framework in place.
authorCameron Ball <cameron@getapproved.com.au>
Fri, 7 Nov 2014 06:22:30 +0000 (14:22 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 7 Nov 2014 06:22:30 +0000 (14:22 +0800)
14 files changed:
Controllers/FileController.php [new file with mode: 0644]
Controllers/SimfileController.php
DataAccess/DataMapper/Helpers/EntityMapsHelper.php
DataAccess/FileRepository.php [new file with mode: 0644]
DataAccess/IFileRepository.php [new file with mode: 0644]
DataAccess/IUserRepository.php
Domain/Entities/File.php [new file with mode: 0644]
Domain/Entities/IFile.php [new file with mode: 0644]
Domain/Entities/StepMania/ISimfile.php
Domain/Entities/StepMania/Simfile.php
config/DI.php
config/DataMaps.php
config/Routes.php
public_html/index.php

diff --git a/Controllers/FileController.php b/Controllers/FileController.php
new file mode 100644 (file)
index 0000000..444ee85
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+namespace Controllers;
+
+use Controllers\IDivineController;
+use Services\Http\IHttpRequest;
+use Services\Http\IHttpResponse;
+use DataAccess\IFileRepository;
+
+class FileController implements IDivineController
+{
+    private $_fileRepository;
+    private $_response;
+    private $_request;
+    
+    public function __construct(
+        IHttpRequest $request,
+        IHttpResponse $response,
+        IFileRepository $repository
+    ) {
+        $this->_request = $request;
+        $this->_response = $response;
+        $this->_fileRepository = $repository;
+    }
+    
+    public function indexAction() {
+        ;
+    }
+    
+    // list simfiles
+    public function serveBannerAction($hash)
+    {
+        $file = $this->_fileRepository->findByHash($hash);
+        
+        if(!$file)
+        {
+            $this->_response->setHeader('HTTP/1.0 404 Not Found', 'Nothing to see here')
+                            ->setBody('Move along.')
+                            ->sendResponse();
+            
+            return;
+        }
+        
+        $match = reset(glob('../files/' . $file->getPath() . '/' . $file->getHash() . '.*'));
+        $this->_response->setHeader('Content-Type', $file->getMimetype())
+                        ->setHeader('Content-Length', $file->getSize())
+                        ->setBody(file_get_contents($match))
+                        ->sendResponse();
+    }
+}
index b56cb4a..3a042d7 100644 (file)
@@ -40,9 +40,36 @@ class SimfileController implements IDivineController
         \r
         foreach($simfiles as $simfile)\r
         {\r
+            $singleSteps = array();\r
+            $doubleSteps = array();\r
+            \r
+            foreach($simfile->getSteps() as $steps)\r
+            {   \r
+                $stepDetails = array(\r
+                    'artist' => $steps->getArtist()->getTag(),\r
+                    'difficulty' => $steps->getDifficulty()->getITGName(),\r
+                    'rating' => $steps->getRating()\r
+                );\r
+                \r
+                if($steps->getMode()->getPrettyName() == 'Single')\r
+                {\r
+                    $singleSteps[] = $stepDetails;\r
+                } else {\r
+                    $doubleSteps[] = $stepDetails;\r
+                }\r
+            }\r
+\r
             $returnArray[] = array(\r
+                'title' => $simfile->getTitle(),\r
                 'artist' => $simfile->getArtist()->getName(),\r
-                'title' => $simfile->getTitle()\r
+                'steps' => array(\r
+                    'single' => $singleSteps,\r
+                    'double' => $doubleSteps\r
+                ),\r
+                'bgChanges' => $simfile->hasBgChanges() ? 'Yes' : 'No',\r
+                'fgChanges' => $simfile->hasFgChanges() ? 'Yes' : 'No',\r
+                'bpmChanges' => $simfile->hasBPMChanges() ? 'Yes' : 'No',\r
+                'banner' => $simfile->getBanner() ? 'files/banner/' . $simfile->getBanner()->getHash() : 'files/banner/default'\r
             );\r
         }\r
         \r
@@ -59,6 +86,8 @@ class SimfileController implements IDivineController
         $filenames = $this->_uploadManager->setDestination('../files/StepMania/')\r
                                           ->process();\r
         \r
+        //parse .sm files and save to DB. should use SimfileParser service\r
+        \r
         echo '<pre>';\r
         print_r($filenames);\r
         echo '</pre>';\r
index fe45000..f06975f 100644 (file)
@@ -48,7 +48,7 @@ class EntityMapsHelper
     {\r
         $className = $maps[$this->_entityName]['class'];\r
         $table = $maps[$this->_entityName]['table'];\r
-\r
+        \r
         // If the table we already have contains the id of a row we need in\r
         // another table\r
         if(isset($row[$this->_tableName . '_id'])) {\r
@@ -58,8 +58,10 @@ class EntityMapsHelper
                 $join_id));\r
             $statement->execute();\r
             $row = $statement->fetch();\r
+        } else {\r
+            return null;\r
         }\r
-        \r
+\r
         $constructors = AbstractPopulationHelper::getConstrutorArray($maps, $this->_entityName, $row, $db);\r
 \r
         if(count($constructors) == 0)\r
diff --git a/DataAccess/FileRepository.php b/DataAccess/FileRepository.php
new file mode 100644 (file)
index 0000000..2e8f9a4
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+namespace DataAccess;
+
+use DataAccess\IFileRepository;
+use DataAccess\DataMapper\IDataMapper;
+use DataAccess\Queries\IQueryBuilderFactory;
+
+class FileRepository implements IFileRepository
+{
+    private $_dataMapper;
+    private $_queryBuilderFactory;
+    
+    public function __construct(IDataMapper $dataMapper, IQueryBuilderFactory $queryBuilderFactory) {
+        $this->_dataMapper = $dataMapper;
+        $this->_queryBuilderFactory = $queryBuilderFactory;
+    }
+    
+    public function findById($id) {
+        $queryBuilder = $this->_queryBuilderFactory->createInstance();
+        $queryBuilder->where('id', '=', "$id");
+        
+        return $this->_dataMapper->map(
+            'File',
+            $queryBuilder
+        );
+    }
+    
+    public function findRange($id, $limit)
+    {
+        return $this->_dataMapper->findRange(
+            'User',
+            'SELECT * FROM %s WHERE id>=' . $id . ' LIMIT ' . $limit
+        );
+    }
+    
+    public function findByHash($hash)
+    {
+        $queryBuilder = $this->_queryBuilderFactory->createInstance();
+        $queryBuilder->where('hash', '=', "$hash");
+        
+        $results = $this->_dataMapper->map(
+                    'File',
+                    $queryBuilder
+                   );
+                
+        return reset($results);
+    }
+}
diff --git a/DataAccess/IFileRepository.php b/DataAccess/IFileRepository.php
new file mode 100644 (file)
index 0000000..fb82c76
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+namespace DataAccess;
+
+use DataAccess\IRepository;
+
+interface IFileRepository extends IRepository
+{
+    public function findByHash($hash);
+}
+
+    
index f34efc5..c48d905 100644 (file)
@@ -7,7 +7,6 @@ use Domain\Entities\IUser;
 
 interface IUserRepository extends IRepository
 {
-    public function findById($id);
     public function findByAuthToken($token);
     public function findByFacebookId($id);
     public function save(IUser $entity);
diff --git a/Domain/Entities/File.php b/Domain/Entities/File.php
new file mode 100644 (file)
index 0000000..3b70b35
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+
+namespace Domain\Entities;
+
+use Domain\Entities\AbstractEntity;
+
+class File extends AbstractEntity implements IFile
+{    
+    private $_hash;
+    private $_path;
+    private $_filename;
+    private $_mimetype;
+    private $_size;
+    private $_uploadDate;
+    
+    public function __construct(
+        $hash,
+        $path,
+        $filename,
+        $mimetype,
+        $size,
+        $uploadDate
+    ) {
+        $this->_hash = $hash;
+        $this->_path = $path;
+        $this->_filename = $filename;
+        $this->_mimetype = $mimetype;
+        $this->_size = $size;
+        $this->_uploadDate = $uploadDate;
+    }
+    
+    public function getFilename() 
+    {
+        return $this->_filename;
+    }
+    
+    public function getHash() 
+    {
+        return $this->_hash;
+    }
+    
+    public function getPath()
+    {
+        return $this->_path;
+    }
+    
+    public function getMimetype()
+    {
+        return $this->_mimetype;
+    }
+    
+    public function getSize() 
+    {
+        return $this->_size;
+    }
+    
+    public function getUploadDate()
+    {
+        return $this->_uploadDate;
+    }
+}
\ No newline at end of file
diff --git a/Domain/Entities/IFile.php b/Domain/Entities/IFile.php
new file mode 100644 (file)
index 0000000..4ce49c3
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+namespace Domain\Entities;
+
+interface IFile extends IDivineEntity
+{
+    public function getPath();
+    public function getHash();
+    public function getFilename();
+    public function getMimetype();
+    public function getSize();
+    public function getUploadDate();
+}
\ No newline at end of file
index b135f89..b8e4608 100644 (file)
@@ -15,6 +15,7 @@ interface ISimfile extends IDivineEntity
     public function hasStops();\r
     public function hasFgChanges();\r
     public function hasBgChanges();\r
+    public function getBanner();\r
         \r
     public function addStepChart(IStepChart $stepChart);\r
     public function getSteps();\r
index e1941a5..d0ddc66 100644 (file)
@@ -9,6 +9,7 @@ use Domain\ConstantsAndTypes\SIMFILE_CONSTANT;
 use Domain\Exception\InvalidStepChartException;\r
 use Domain\Entities\StepMania\ISimfile;\r
 use Domain\Entities\IUser;\r
+use Domain\Entities\IFile;\r
 use Domain\Entities\AbstractEntity;\r
 \r
 class Simfile extends AbstractEntity implements ISimfile\r
@@ -21,6 +22,7 @@ class Simfile extends AbstractEntity implements ISimfile
     private $_stops = false;\r
     private $_fgChanges = false;\r
     private $_bgChanges = false;\r
+    private $_banner;\r
     private $_steps;\r
     \r
     public function __construct(\r
@@ -32,6 +34,7 @@ class Simfile extends AbstractEntity implements ISimfile
         $stops,\r
         $fgChanges,\r
         $bgChanges,\r
+        IFile $banner = null,\r
         array $steps\r
     ) {\r
         $this->_title = $title;\r
@@ -42,6 +45,7 @@ class Simfile extends AbstractEntity implements ISimfile
         $this->_stops = $stops;\r
         $this->_fgChanges = $fgChanges;\r
         $this->_bgChanges = $bgChanges;\r
+        $this->_banner = $banner;\r
 \r
         foreach($steps as $stepChart) {\r
             if(!$stepChart instanceof IStepChart) {\r
@@ -96,6 +100,11 @@ class Simfile extends AbstractEntity implements ISimfile
         $this->_steps[] = $stepChart;\r
     }\r
     \r
+    public function getBanner()\r
+    {\r
+        return $this->_banner;\r
+    }\r
+    \r
     public function getSteps()\r
     {\r
         return $this->_steps;\r
index 49627d5..86dbb1f 100644 (file)
@@ -26,6 +26,7 @@ return [
     //DA\r
     'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'),\r
     'DataAccess\IUserRepository' => DI\object('DataAccess\UserRepository'),\r
+    'DataAccess\IFileRepository' => DI\object('DataAccess\FileRepository'),\r
     'DataAccess\IDatabaseFactory' => DI\object('DataAccess\DatabaseFactory')\r
         ->constructor(DI\link('db.credentials')),\r
     'DataAccess\DataMapper\IDataMapper' => DI\object('DataAccess\DataMapper\DataMapper')\r
index 559e7ef..d18492f 100644 (file)
@@ -20,6 +20,7 @@ return [
             'stops' => DataAccess\Int('stops', 'hasStops'),\r
             'fgChanges' => DataAccess\Int('fg_changes', 'hasFgChanges'),\r
             'bgChanges' => DataAccess\Int('bg_changes', 'hasBgChanges'),\r
+            'banner' => DataAccess\Entity('File', 'getBanner', 'banner_file'),\r
             'steps' => DataAccess\VOArray('StepChart', 'getSteps')\r
         ]\r
     ],\r
@@ -111,5 +112,19 @@ return [
         'maps' => [\r
             'stepManiaName' => DataAccess\Varchar('difficulty', 'getStepManiaName')\r
         ]\r
+    ],\r
+    \r
+    'File' => [\r
+        'class' => 'Domain\Entities\File',\r
+        'table' => 'files',\r
+        'maps' => [\r
+            'hash' => DataAccess\Varchar('hash'),\r
+            'path' => DataAccess\Varchar('path'),\r
+            'filename' => DataAccess\Varchar('filename'),\r
+            'mimetype' => DataAccess\Varchar('mimetype'),\r
+            'size' => DataAccess\Int('size'),\r
+            //TODO: actually this is stored as a datetime, might need datetime helper for stuff\r
+            'uploadDate' => DataAccess\Varchar('uploaded')\r
+        ]\r
     ]\r
 ];\r
index 73c3cae..1d0eda5 100644 (file)
@@ -28,5 +28,11 @@ return [
         'method' => ['GET'],\r
         'controller' => 'User',\r
         'action' => 'getUser'\r
+    ],\r
+    \r
+    '/files/banner/:hash' => [\r
+        'method' => ['GET'],\r
+        'controller' => 'File',\r
+        'action' => 'serveBanner'\r
     ]\r
 ];\r
index 56627e8..17e529f 100644 (file)
@@ -3,7 +3,7 @@
 //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
+//header("Access-Control-Allow-Origin: http://roll.divinelegy.dev:8000");\r
 \r
 require_once('../vendor/autoload.php');\r
 \r