diff options
author | Olivier Paroz <github@oparoz.com> | 2015-01-07 21:27:49 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-01-07 21:27:49 +0300 |
commit | cc1f908d222b82747821c8b9ebff5b1bb9b6372b (patch) | |
tree | 65c4c86a1d9dfdfa2cc175dbf50d3fe70e2efeb4 /middleware/checkmiddleware.php | |
parent | 70c5e85d84adea1df5016088f0bd18d5b017e9b1 (diff) |
Shorter methods in CheckMiddleware
Diffstat (limited to 'middleware/checkmiddleware.php')
-rw-r--r-- | middleware/checkmiddleware.php | 95 |
1 files changed, 55 insertions, 40 deletions
diff --git a/middleware/checkmiddleware.php b/middleware/checkmiddleware.php index 9456c13e..79dd2e10 100644 --- a/middleware/checkmiddleware.php +++ b/middleware/checkmiddleware.php @@ -114,46 +114,65 @@ abstract class CheckMiddleware extends Middleware { * @return RedirectResponse|TemplateResponse */ private function sendHtmlResponse($message, $code) { - $appName = $this->appName; - $this->logger->debug( "[CheckException] HTML response", array( - 'app' => $appName + 'app' => $this->appName ) ); + /** + * We need to render a template for 401 or we'll have an endless loop as + * this is called before the controller gets a chance to render anything + */ if ($code === 401) { - $params = $this->request->getParams(); + $response = $this->sendHtml401(); + } else { + $response = $this->redirectToErrorPage($message, $code); + } - $this->logger->debug( - '[CheckException] Unauthorised Request params: {params}', - array( - 'app' => $appName, - 'params' => $params - ) - ); + return $response; + } - /** - * We need to render a template or we'll have an endless - * loop as this is called before the controller can render - * a template - */ + /** + * Shows an authentication form + * + * @return TemplateResponse + */ + private function sendHtml401() { + $appName = $this->appName; + $params = $this->request->getParams(); - return new TemplateResponse( - $appName, 'authenticate', $params, - 'guest' - ); + $this->logger->debug( + '[CheckException] Unauthorised Request params: {params}', + array( + 'app' => $appName, + 'params' => $params + ) + ); - } else { - $url = $this->urlGenerator->linkToRoute( - $this->appName . '.page.error_page', - array( - 'message' => $message, - 'code' => $code - ) - ); - } + return new TemplateResponse( + $appName, 'authenticate', $params, + 'guest' + ); + } + + /** + * Redirects the client to an error page + * + * @param string $message + * @param int $code + * + * @return RedirectResponse|TemplateResponse + */ + private function redirectToErrorPage($message, $code) { + $url = $this->urlGenerator->linkToRoute( + $this->appName . '.page.error_page', + array( + 'message' => $message, + 'code' => $code + ) + ); return new RedirectResponse($url); } @@ -167,23 +186,19 @@ abstract class CheckMiddleware extends Middleware { * @return JSONResponse */ private function sendJsonResponse($message, $code) { - $appName = $this->appName; - $response = new JSONResponse( - array( - 'message' => $message, - 'success' => false - ), - $code - ); - $this->logger->debug( "[TokenCheckException] JSON response", array( - 'app' => $appName + 'app' => $this->appName ) ); - return $response; + $jsonData = array( + 'message' => $message, + 'success' => false + ); + + return new JSONResponse($jsonData, $code); } }
\ No newline at end of file |