Implement download entity and related stuff. Will build things like user quota on...
authorCameron Ball <cameron@getapproved.com.au>
Fri, 28 Nov 2014 08:00:22 +0000 (16:00 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 28 Nov 2014 08:00:22 +0000 (16:00 +0800)
16 files changed:
Controllers/DownloadTestController.php [new file with mode: 0644]
Controllers/PackTestController.php [deleted file]
DataAccess/DownloadRepository.php [new file with mode: 0644]
DataAccess/IDownloadRepository.php [new file with mode: 0644]
DataAccess/Queries/DownloadQueryConstraints.php [new file with mode: 0644]
DataAccess/Queries/IDownloadQueryConstraints.php [new file with mode: 0644]
DataAccess/Queries/QueryConstraints.php [deleted file]
Domain/Entities/Download.php [new file with mode: 0644]
Domain/Entities/DownloadFactory.php [new file with mode: 0644]
Domain/Entities/IDownload.php [new file with mode: 0644]
Services/IUserSession.php
Services/UserSession.php
config/DI.php
config/DataMaps.php
config/Routes.php
public_html/index.php

diff --git a/Controllers/DownloadTestController.php b/Controllers/DownloadTestController.php
new file mode 100644 (file)
index 0000000..c12df99
--- /dev/null
@@ -0,0 +1,32 @@
+<?php\r
+\r
+namespace Controllers;\r
+\r
+use Controllers\IDivineController;\r
+use DataAccess\IDownloadRepository;\r
+use DataAccess\Queries\DownloadQueryConstraints;\r
+use DateTime;\r
+\r
+class DownloadTestController implements IDivineController\r
+{\r
+    private $_downloadRepository;\r
+    \r
+    public function __construct(\r
+        IDownloadRepository $repository\r
+    ) {\r
+        $this->_downloadRepository = $repository;\r
+    }\r
+    \r
+    public function indexAction() {\r
+        $start = new DateTime('0:00 today');\r
+        $end = new DateTime();\r
+        \r
+        $constraints = new DownloadQueryConstraints();\r
+        $constraints->inDateRange($start, $end);\r
+        $downloads = $this->_downloadRepository->findByUserId(4, $constraints);\r
+\r
+        echo '<pre>';\r
+        print_r($downloads);\r
+        echo '</pre>';\r
+    }\r
+}\r
diff --git a/Controllers/PackTestController.php b/Controllers/PackTestController.php
deleted file mode 100644 (file)
index 060514a..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-<?php\r
-\r
-namespace Controllers;\r
-\r
-use Controllers\IDivineController;\r
-use DataAccess\StepMania\IPackRepository;\r
-use DataAccess\StepMania\ISimfileRepository;\r
-\r
-class PackTestController implements IDivineController\r
-{\r
-    private $_packRepository;\r
-    \r
-    public function __construct(\r
-        IPackRepository $repository\r
-    ) {\r
-        $this->_packRepository = $repository;\r
-    }\r
-    \r
-    public function indexAction() {\r
-        $pack = $this->_packRepository->findById(10);\r
-\r
-        echo '<pre>';\r
-        print_r($pack);\r
-        echo '</pre>';\r
-    }\r
-}\r
diff --git a/DataAccess/DownloadRepository.php b/DataAccess/DownloadRepository.php
new file mode 100644 (file)
index 0000000..544611e
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+
+namespace DataAccess;
+
+use DataAccess\IDownloadRepository;
+use DataAccess\DataMapper\IDataMapper;
+use DataAccess\Queries\IQueryBuilderFactory;
+use DataAccess\Queries\IQueryBuilder;
+use DataAccess\Queries\IDownloadQueryConstraints;
+use Domain\Entities\IDownload;
+
+//TODO: Implement some sort of caching. Probably OK for now not to worry.
+class DownloadRepository implements IDownloadRepository
+{
+    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);
+             
+        $result = $this->_dataMapper->map('Download', $queryBuilder);
+        return reset($result);
+    }
+    
+    public function findAll()
+    {
+        $queryBuilder = $this->_queryBuilderFactory->createInstance();
+        $queryBuilder->where('id', '>', 0);
+        $result = $this->_dataMapper->map('Download', $queryBuilder);
+        return $result;
+    }
+    
+    public function findRange($id, $limit)
+    {
+        $queryBuilder = $this->_queryBuilderFactory->createInstance();
+        $queryBuilder->where('id', '>=', $id)->limit($limit);
+                
+        return $this->_dataMapper->map('Download', $queryBuilder);
+    }
+    
+    public function save(IDownload $entity) {
+        return $this->_dataMapper->save($entity);
+    }
+    
+    public function findByUserId($id, IDownloadQueryConstraints $constraints = null)
+    {
+        $queryBuilder = $this->_queryBuilderFactory->createInstance();
+        $queryBuilder->where('user_id', '=', $id);
+        
+        return $this->applyConstraintsAndReturn($constraints, $queryBuilder);
+    }
+    
+    public function findByFileId($id, IDownloadQueryConstraints $constraints = null)
+    {
+        $queryBuilder = $this->_queryBuilderFactory->createInstance();
+        $queryBuilder->where('file_id', '=', $id);
+        
+        return $this->applyConstraintsAndReturn($constraints, $queryBuilder);
+    }
+    
+    private function applyConstraintsAndReturn(IDownloadQueryConstraints $constraints = NULL, IQueryBuilder $queryBuilder)
+    {
+        if($constraints)
+        {
+            $constraints->applyTo($queryBuilder);
+        }
+
+        return $this->_dataMapper->map('Download', $queryBuilder);
+    }
+}
\ No newline at end of file
diff --git a/DataAccess/IDownloadRepository.php b/DataAccess/IDownloadRepository.php
new file mode 100644 (file)
index 0000000..e1052dd
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+namespace DataAccess;
+
+use DataAccess\IRepository;
+use DataAccess\Queries\IDownloadQueryConstraints;
+use Domain\Entities\IDownload;
+
+interface IDownloadRepository extends IRepository
+{
+    public function findByUserId($id, IDownloadQueryConstraints $constraints = null);
+    public function findByFileId($id, IDownloadQueryConstraints $constraints = null);
+    public function save(IDownload $file);
+}
+
+    
diff --git a/DataAccess/Queries/DownloadQueryConstraints.php b/DataAccess/Queries/DownloadQueryConstraints.php
new file mode 100644 (file)
index 0000000..a91b40e
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+namespace DataAccess\Queries;
+
+use DataAccess\Queries\IQueryBuilder;
+use DataAccess\Queries\IDownloadQueryConstraints;
+use DateTimeInterface;
+
+class DownloadQueryConstraints implements IDownloadQueryConstraints
+{        
+    private $_queryBuilder;
+    private $_dateRangeStart;
+    private $_dateRangeEnd;
+    
+    public function applyTo(IQueryBuilder $queryBuilder)
+    {
+        $this->_queryBuilder = $queryBuilder;
+        $this->applyDateRange();
+    }
+    
+    public function inDateRange(DateTimeInterface $start, DateTimeInterface $end)
+    {
+        $this->_dateRangeStart = $start;
+        $this->_dateRangeEnd = $end;
+        return $this;
+    }
+    
+    private function applyDateRange()
+    {
+        if($this->_dateRangeStart && $this->_dateRangeEnd) {
+            $this->_queryBuilder->where('timestamp', '>=', $this->_dateRangeStart->getTimestamp())
+                                ->where('timestamp', '<=', $this->_dateRangeEnd->getTimestamp());
+        }
+        
+        return $this;
+    }
+}
+    
\ No newline at end of file
diff --git a/DataAccess/Queries/IDownloadQueryConstraints.php b/DataAccess/Queries/IDownloadQueryConstraints.php
new file mode 100644 (file)
index 0000000..f51eef8
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+namespace DataAccess\Queries;
+
+use DataAccess\Queries\IQueryConstraints;
+use DateTimeInterface;
+
+interface IDownloadQueryConstraints extends IQueryConstraints
+{
+    public function inDateRange(DateTimeInterface $start, DateTimeInterface $end);
+}
\ No newline at end of file
diff --git a/DataAccess/Queries/QueryConstraints.php b/DataAccess/Queries/QueryConstraints.php
deleted file mode 100644 (file)
index 415baf8..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-<?php
-
-namespace DataAccess\Queries;
-
-use DataAccess\Queries\IQueryConstraints;
-
-class QueryConstraints implements IQueryConstraints
-{
-    private $_queryString;
-    
-    protected $_whereClauses = array();
-    protected $_limitClause;
-    protected $_joinClause;
-    
-    public function applyTo($queryString)
-    {
-        $this->_queryString = $queryString;
-        
-        $this->applyJoinClause()
-             ->applyWhereClauses()
-             ->applyLimitClause();
-        
-        return $this->_queryString;
-    }
-    
-    public function where($columnName, $operator, $value)
-    {
-        $this->_whereClauses[$columnName] = array('operator' => $operator, 'value' => $value);
-        return $this;
-    }
-    
-    public function limit($start, $end = null)
-    {
-        if($end) 
-        {
-            $this->_limitClause = sprintf(' LIMIT %u,%u', $start, $end);
-            return $this;
-        }
-        
-        $this->_limitClause = sprintf(' LIMIT %u', $start);
-        
-        return $this;
-    }
-    
-    public function join($type, $tableA, $columnA, $tableB, $columnB)
-    {
-        $this->_joinClause = sprintf(' %s JOIN %s ON %s.%s = %s.%s', $type, $tableB, $tableA, $columnA, $tableB, $columnB);
-        return $this;
-    }
-    
-    private function applyJoinClause()
-    {
-        $this->_queryString .= $this->_joinClause;
-        return $this;
-    }
-    
-    private function applyWhereClauses()
-    {
-        $this->_queryString .= ' WHERE ';
-        
-        foreach($this->_whereClauses as $columnName => $columnValue)
-        {
-            switch(gettype($columnValue['value']))
-            {
-                case 'integer':
-                    $this->_queryString .= sprintf("%s%s%u", $columnName, $columnValue['operator'], $columnValue['value']) . ' AND ';
-                    break;
-                case 'string':
-                    $this->_queryString .= sprintf("%s %s '%s'", $columnName, $columnValue['operator'], $columnValue['value']) . ' AND ';
-                    break;
-            }
-            
-        }
-        
-        $this->_queryString = rtrim($this->_queryString, ' AND ');
-        return $this;
-    }
-    
-    private function applyLimitClause()
-    {
-        $this->_queryString .= $this->_limitClause;
-        return $this;
-    }
-    
-}
\ No newline at end of file
diff --git a/Domain/Entities/Download.php b/Domain/Entities/Download.php
new file mode 100644 (file)
index 0000000..d00d776
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+
+namespace Domain\Entities;
+
+use Domain\Entities\IDownload;
+use Domain\Entities\IUser;
+use Domain\Entities\IFile;
+
+class Download extends AbstractEntity implements IDownload
+{
+    private $_user;
+    private $_file;
+    private $_timestamp;
+    private $_ip;
+    
+    public function __construct(
+        IUser $user,
+        IFile $file,
+        $timestamp,
+        $ip
+    ) {
+        $this->_user = $user;
+        $this->_file = $file;
+        $this->_timestamp = $timestamp;
+        $this->_ip = $ip;
+    }
+    
+    public function getFile()
+    {
+        return $this->_file;
+    }
+    
+    public function getIp()
+    {
+        return $this->_ip;
+    }
+    
+    public function getTimestamp()
+    {
+        return $this->_timestamp;
+    }
+    
+    public function getUser()
+    {
+        return $this->_user;
+    }
+}
+
diff --git a/Domain/Entities/DownloadFactory.php b/Domain/Entities/DownloadFactory.php
new file mode 100644 (file)
index 0000000..3b54f41
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+namespace Domain\Entities;
+
+use Domain\Entities\Download;
+
+interface IDownloadFactory
+{
+    public function createInstance(
+        IUser $user,
+        IFile $file,
+        $timestamp,
+        $ip
+    );
+}
+
+class DownloadFactory implements IDownloadFactory
+{
+    public function createInstance(
+        IUser $user,
+        IFile $file,
+        $timestamp,
+        $ip
+    ) {
+        return new Download(
+            $user,
+            $file,
+            $timestamp,
+            $ip
+        );
+    }
+}
diff --git a/Domain/Entities/IDownload.php b/Domain/Entities/IDownload.php
new file mode 100644 (file)
index 0000000..3bf72c2
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+namespace Domain\Entities;
+
+interface IDownload
+{
+    public function getUser();
+    public function getFile();
+    public function getTimestamp();
+    public function getIp();
+}
+
index 96b13dd..084aa0b 100644 (file)
@@ -5,5 +5,6 @@ namespace Services;
 interface IUserSession
 {
     public function getCurrentUser();
+    public function getCurrentUserQuota();
 }
 
index f56d56f..53052d6 100644 (file)
@@ -25,6 +25,10 @@ class UserSession implements IUserSession
         return $this->_currentUser;
     }
     
+    public function getCurrentUserQuota() {
+        ;
+    }
+    
     private function findToken()
     {        
         if($this->_request->isPost())
index 3d117d0..34e6be0 100644 (file)
@@ -42,6 +42,7 @@ return [
     //DA\r
     'DataAccess\StepMania\ISimfileRepository'             => DI\object('DataAccess\StepMania\SimfileRepository'),\r
     'DataAccess\StepMania\IPackRepository'                => DI\object('DataAccess\StepMania\PackRepository'),\r
+    'DataAccess\IDownloadRepository'                      => DI\object('DataAccess\DownloadRepository'),\r
     'DataAccess\IUserRepository'                          => DI\object('DataAccess\UserRepository'),\r
     'DataAccess\IFileRepository'                          => DI\object('DataAccess\FileRepository'),\r
     'DataAccess\IDatabaseFactory'                         => DI\object('DataAccess\DatabaseFactory')\r
index e9a4a7d..532f940 100644 (file)
@@ -149,5 +149,16 @@ return [
             'uri' => DataAccess\Varchar('uri'),\r
             'source' => DataAccess\Varchar('source')\r
         ]\r
+    ],\r
+    \r
+    'Download' => [\r
+        'class' => 'Domain\Entities\Download',\r
+        'table' => 'downloads',\r
+        'maps' => [\r
+            'user' => DataAccess\Entity('User'),\r
+            'file' => DataAccess\Entity('File'),\r
+            'timestamp' => DataAccess\Int('timestamp'),\r
+            'ip' => DataAccess\Varchar('ip')\r
+        ]\r
     ]\r
 ];\r
index 6c9f5e7..c3b28ca 100644 (file)
@@ -7,6 +7,12 @@ return [
         'action' => 'list'\r
     ],\r
     \r
+    //TODO: test controller, delete later\r
+    '/downloadtest' => [\r
+        'methods' => ['GET'],\r
+        'controller' => 'downloadTest'\r
+    ],\r
+    \r
     '/simfiles/upload' => [\r
         'methods' => ['POST'],\r
         'controller' => 'Simfile',\r
index 7b8f8dd..d4843d3 100644 (file)
@@ -7,6 +7,9 @@ header("Access-Control-Allow-Origin: http://roll.divinelegy.meeples:8000");
 \r
 require_once('../vendor/autoload.php');\r
 \r
+// Everything time related should be UTC+0 based\r
+date_default_timezone_set('UTC');\r
+\r
 // Set up the DI container\r
 $containerBuilder = new DI\ContainerBuilder();\r
 $containerBuilder->addDefinitions('../config/DI.php');\r