From 27ac7e8063d497402a704ce92fc48c1043fd11e4 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 7 Jan 2016 20:48:18 -0500 Subject: [PATCH] Fix http verb for update route, add correct http codes for http errors --- app/config/routes.php | 2 +- app/views/error.php | 4 +-- sonar-project.properties | 2 +- src/Aviat/AnimeClient/Controller.php | 11 +++--- src/Aviat/AnimeClient/Dispatcher.php | 2 ++ src/Aviat/Ion/View.php | 31 ++++------------ src/Aviat/Ion/View/HttpView.php | 54 ++++++++++++++++++++-------- 7 files changed, 59 insertions(+), 47 deletions(-) diff --git a/app/config/routes.php b/app/config/routes.php index 9f2cd6fd..0e636722 100644 --- a/app/config/routes.php +++ b/app/config/routes.php @@ -99,7 +99,6 @@ return [ 'login' => [ 'path' => '/{controller}/login', 'action' => 'login', - 'verb' => 'get' ], 'login_post' => [ 'path' => '/{controller}/login', @@ -113,6 +112,7 @@ return [ 'update' => [ 'path' => '/{controller}/update', 'action' => 'update', + 'verb' => 'post', 'tokens' => [ 'controller' => '[a-z_]+' ] diff --git a/app/views/error.php b/app/views/error.php index e26f7883..f4bcac5a 100644 --- a/app/views/error.php +++ b/app/views/error.php @@ -1,5 +1,5 @@

-
-
+
+ \ No newline at end of file diff --git a/sonar-project.properties b/sonar-project.properties index 81418be2..b18cdc39 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,6 +1,6 @@ sonar.projectKey=animeclient sonar.projectName=Anime Client -sonar.projectVersion=2.0.0 +sonar.projectVersion=2.1.0 sonar.sources=src sonar.php.coverage.reportPath=build/logs/clover.xml sonar.php.tests.reportPath=build/logs/junit.xml \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Controller.php b/src/Aviat/AnimeClient/Controller.php index a7dd5069..3bb5b3a6 100644 --- a/src/Aviat/AnimeClient/Controller.php +++ b/src/Aviat/AnimeClient/Controller.php @@ -306,24 +306,25 @@ class Controller { { $this->outputHTML('404', [ 'title' => 'Sorry, page not found' - ]); + ], NULL, 404); } /** * Display a generic error page * + * @param int $http_code * @param string $title * @param string $message * @param string $long_message * @return void */ - public function error_page($title, $message, $long_message = "") + public function error_page($http_code, $title, $message, $long_message = "") { $this->outputHTML('error', [ 'title' => $title, 'message' => $message, 'long_message' => $long_message - ]); + ], NULL, $http_code); } /** @@ -365,15 +366,17 @@ class Controller { * @param string $template * @param array $data * @param HtmlView|null $view + * @param int $code * @return void */ - protected function outputHTML($template, array $data = [], $view = NULL) + protected function outputHTML($template, array $data = [], $view = NULL, $code = 200) { if (is_null($view)) { $view = new HtmlView($this->container); } + $view->setStatusCode($code); $this->render_full_page($view, $template, $data); } diff --git a/src/Aviat/AnimeClient/Dispatcher.php b/src/Aviat/AnimeClient/Dispatcher.php index 05117c6e..7462d88a 100644 --- a/src/Aviat/AnimeClient/Dispatcher.php +++ b/src/Aviat/AnimeClient/Dispatcher.php @@ -113,11 +113,13 @@ class Dispatcher extends RoutingBase { switch(TRUE) { case $failure->failedMethod(): + $params['http_code'] = 405; $params['title'] = '405 Method Not Allowed'; $params['message'] = 'Invalid HTTP Verb'; break; case $failure->failedAccept(): + $params['http_code'] = 406; $params['title'] = '406 Not Acceptable'; $params['message'] = 'Unacceptable content type'; break; diff --git a/src/Aviat/Ion/View.php b/src/Aviat/Ion/View.php index c9a0a2b6..5899a0b2 100644 --- a/src/Aviat/Ion/View.php +++ b/src/Aviat/Ion/View.php @@ -72,7 +72,7 @@ abstract class View { { $this->send(); } - } + } /** * Return rendered output @@ -119,28 +119,11 @@ abstract class View { public function getOutput() { return $this->string($this->output)->__toString(); - } - - /** - * Send output to client - */ - public function send() - { - $this->hasRendered = TRUE; - $this->output(); - } - - /** - * Send the appropriate response - * - * @return void - */ - protected function output() - { - $content =& $this->response->content; - $content->set($this->output); - $content->setType($this->contentType); - $content->setCharset('utf-8'); - } + } + + /** + * Send output to client + */ + abstract public function send(); } // End of View.php \ No newline at end of file diff --git a/src/Aviat/Ion/View/HttpView.php b/src/Aviat/Ion/View/HttpView.php index 37f3f240..1a62f853 100644 --- a/src/Aviat/Ion/View/HttpView.php +++ b/src/Aviat/Ion/View/HttpView.php @@ -31,20 +31,44 @@ class HttpView extends BaseView { public function redirect($url, $code) { $this->response->redirect->to($url, $code); - } - - /** - * Send the appropriate response - * - * @codeCoverageIgnore - * @return void - */ - protected function output() - { - parent::output(); - - $sender = new ResponseSender($this->response); - $sender->__invoke(); - } + } + + /** + * Set the status code of the request + * + * @param int $code + * @return HttpView + */ + public function setStatusCode($code) + { + $this->response->status->setCode($code); + $this->response->status->setVersion(1.1); + return $this; + } + + /** + * Send output to client + */ + public function send() + { + $this->hasRendered = TRUE; + $this->output(); + } + + /** + * Send the appropriate response + * + * @return void + */ + protected function output() + { + $content =& $this->response->content; + $content->set($this->output); + $content->setType($this->contentType); + $content->setCharset('utf-8'); + + $sender = new ResponseSender($this->response); + $sender->__invoke(); + } } \ No newline at end of file