Tentative work on controllers and repositories.
authorCameron Ball <cameron@getapproved.com.au>
Mon, 8 Sep 2014 09:05:10 +0000 (17:05 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Mon, 8 Sep 2014 09:05:10 +0000 (17:05 +0800)
Controllers/AbstractController.php [new file with mode: 0644]
Controllers/HomeController.php [new file with mode: 0644]
Controllers/IDivineController.php [new file with mode: 0644]
DataAccess/DataMapper/DataMapper.php
DataAccess/IRepository.php [new file with mode: 0644]
DataAccess/StepMania/SimfileRepository.php [new file with mode: 0644]
Services/HttpResponse.php [new file with mode: 0644]
Services/IHttpResponse.php [new file with mode: 0644]

diff --git a/Controllers/AbstractController.php b/Controllers/AbstractController.php
new file mode 100644 (file)
index 0000000..d6f0637
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+namespace Controllers;
+
+use Controllers\IDivineController;
+
+abstract class AbstractController implements IDivineController
+{
+    protected $_isJsonResponse;
+    protected $_response;
+    
+    
+    public function __construct
+    
+    public function setJsonResponse()
+    {
+        $this->_isJsonResponse = true;
+    }
+}
\ No newline at end of file
diff --git a/Controllers/HomeController.php b/Controllers/HomeController.php
new file mode 100644 (file)
index 0000000..fa1c3a5
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+class HomeController
+{
+    
+    private $content;
+    
+    
+    public function defaultAction()
+    {
+        
+    }
+}
diff --git a/Controllers/IDivineController.php b/Controllers/IDivineController.php
new file mode 100644 (file)
index 0000000..c11b7fd
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+namespace Controllers;
+
+interface IDivineController
+{
+    public function setJsonResponse();
+    public function getAction();
+}
index 7026d16..be1bf0a 100644 (file)
@@ -15,7 +15,7 @@ class DataMapper implements IDataMapper
     \r
     public function __construct($maps)\r
     {\r
-        //should probably do all this through a configuration object or something\r
+        //TODO: should probably do all this through a configuration object or something\r
         $dsn = 'mysql:host=localhost;dbname=divinelegy;charset=utf8';\r
         $username = 'root';\r
         $password = 'toor';\r
@@ -100,6 +100,7 @@ class DataMapper implements IDataMapper
         echo '</pre>';\r
     }\r
     \r
+    //TODO: Implement\r
     public function remove(IDivineEntity $entity) {\r
         ;\r
     }\r
diff --git a/DataAccess/IRepository.php b/DataAccess/IRepository.php
new file mode 100644 (file)
index 0000000..1dd8c75
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+namespace DataAccess;
+
+use Domain\Entities\IDivineEntity;
+
+interface IRepository
+{
+    public function find($id);
+    public function save(IDivineEntity $entity);
+    public function remove(IDivineEntity $entity);
+}
+    
diff --git a/DataAccess/StepMania/SimfileRepository.php b/DataAccess/StepMania/SimfileRepository.php
new file mode 100644 (file)
index 0000000..dcc97a7
--- /dev/null
@@ -0,0 +1,30 @@
+<?php\r
+\r
+namespace DataAccess\StepMania;\r
+\r
+use DataAccess\IRepository;\r
+use DataAccess\DataMapper\IDataMapper;\r
+use Domain\Entities\IDivineEntity;\r
+\r
+//TODO: Implement some sort of caching. Probably OK for now not to worry.\r
+class SimfileRepository implements IRepository\r
+{\r
+    private $dataMapper;\r
+    \r
+    public function __construct(IDataMapper $dataMapper) {\r
+        $this->dataMapper = $dataMapper;\r
+    }\r
+    \r
+    public function find($id) {\r
+        return $this->dataMapper->find($id, 'simfiles');\r
+    }\r
+    \r
+    public function save(IDivineEntity $entity) {\r
+        $this->dataMapper->save($entity);\r
+    }\r
+    \r
+    //TODO: Implement\r
+    public function remove(IDivineEntity $entity) {\r
+        ;\r
+    }\r
+}\r
diff --git a/Services/HttpResponse.php b/Services/HttpResponse.php
new file mode 100644 (file)
index 0000000..f3c05c9
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+namespace Services;
+
+use Services\IHttpResponse;
+use Exception;
+
+class HttpResponse implements IHttpResponse
+{
+    private $_statusCode = 200;
+    private $_headers = array();
+    private $_body;
+    private $_isRedirect = false;
+    
+    public function setStatusCode($code)
+    {
+        if(!is_int($code) || (100 > $code) || (599 < $code)) {
+            throw new Exception(sprintf('Invalid HTTP response code, %u', $code));
+        }
+        
+        $this->_isRedirect = (300 <= $code) && (307 >= $code);
+        $this->_statusCode = $code;
+        
+        return $this;
+    }
+    
+    public function isRedirect()
+    {
+        return $this->_isRedirect;
+    }
+    
+    public function setHeader($name, $value)
+    {
+        $value = (string) $value;
+        
+        $this->_headers[$name] = $value;
+        
+        return $this;
+    }
+    
+    public function getHeaders()
+    {
+        return $this->_headers;
+    }
+    
+    private function sendHeaders()
+    {
+        $statusCodeSent = false;
+        
+        if(!count($this->_headers)) {
+            return $this;
+        }
+        
+        foreach($this->_headers as $headerName => $headerValue) {
+            if(!$statusCodeSent) {
+                header(
+                    sprintf('%s:%s', $headerName, $headerValue),
+                    false,
+                    $this->_statusCode);
+                
+                $statusCodeSent = true;
+            }
+        }
+        
+        return $this;
+    }
+    
+    public function setBody($content)
+    {
+        $this->_body = $content;
+    }
+    
+    public function getBody()
+    {
+        return $this->_body;
+    }
+    
+    
+    private function sendBody()
+    {
+        echo $this->_body;
+    }
+        
+    public function sendResponse()
+    {
+        $this->sendHeaders()
+             ->sendBody();
+    }
+}
diff --git a/Services/IHttpResponse.php b/Services/IHttpResponse.php
new file mode 100644 (file)
index 0000000..ab3feed
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+namespace Services;
+
+interface IHttpResponse
+{
+    // TODO: Maybe we want a method to do an internal redirect to a different controller.
+    // Maybe that should be done by the abstract controller class, though?
+    public function setStatusCode($code);
+    public function setHeader($name, $value);
+    public function getHeaders();
+    public function setBody();
+    public function getBody();
+    public function isRedirect();
+    public function sendResponse();
+}
\ No newline at end of file