--- /dev/null
+<?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
+++ /dev/null
-<?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
--- /dev/null
+<?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
--- /dev/null
+<?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);
+}
+
+
--- /dev/null
+<?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
--- /dev/null
+<?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
+++ /dev/null
-<?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
--- /dev/null
+<?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;
+ }
+}
+
--- /dev/null
+<?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
+ );
+ }
+}
--- /dev/null
+<?php
+
+namespace Domain\Entities;
+
+interface IDownload
+{
+ public function getUser();
+ public function getFile();
+ public function getTimestamp();
+ public function getIp();
+}
+
interface IUserSession
{
public function getCurrentUser();
+ public function getCurrentUserQuota();
}
return $this->_currentUser;
}
+ public function getCurrentUserQuota() {
+ ;
+ }
+
private function findToken()
{
if($this->_request->isPost())
//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
'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
'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
\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