From 6c9954f298b899eb93f2c7c1e80d249b4fb5b746 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 8 Oct 2014 19:47:21 +0200 Subject: [PATCH] Reduced responsibilities of HttpHelper --- src/Controllers/PostContentController.php | 4 ++-- src/Controllers/UserAvatarController.php | 8 ++------ src/Dispatcher.php | 4 ++-- src/Helpers/HttpHelper.php | 22 +++++----------------- src/Services/NetworkingService.php | 14 +++++++++++++- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/Controllers/PostContentController.php b/src/Controllers/PostContentController.php index 4c4b0708..f27e37c4 100644 --- a/src/Controllers/PostContentController.php +++ b/src/Controllers/PostContentController.php @@ -34,7 +34,7 @@ final class PostContentController extends AbstractController public function getPostContent($postName) { $post = $this->postService->getByName($postName); - $this->networkingService->serve($this->fileDao->getFullPath($post->getContentPath())); + $this->networkingService->serveFile($this->fileDao->getFullPath($post->getContentPath())); } public function getPostThumbnail($postName, $size) @@ -47,6 +47,6 @@ final class PostContentController extends AbstractController $this->thumbnailService->generateIfNeeded($sourceName, $size, $size); $thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size); - $this->networkingService->serve($this->fileDao->getFullPath($thumbnailName)); + $this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName)); } } diff --git a/src/Controllers/UserAvatarController.php b/src/Controllers/UserAvatarController.php index 23b97d09..01808cf8 100644 --- a/src/Controllers/UserAvatarController.php +++ b/src/Controllers/UserAvatarController.php @@ -2,7 +2,6 @@ namespace Szurubooru\Controllers; use Szurubooru\Dao\PublicFileDao; use Szurubooru\Entities\User; -use Szurubooru\Helpers\HttpHelper; use Szurubooru\Router; use Szurubooru\Services\NetworkingService; use Szurubooru\Services\ThumbnailService; @@ -13,20 +12,17 @@ final class UserAvatarController extends AbstractController private $fileDao; private $userService; private $networkingService; - private $httpHelper; private $thumbnailService; public function __construct( PublicFileDao $fileDao, UserService $userService, NetworkingService $networkingService, - HttpHelper $httpHelper, ThumbnailService $thumbnailService) { $this->fileDao = $fileDao; $this->userService = $userService; $this->networkingService = $networkingService; - $this->httpHelper = $httpHelper; $this->thumbnailService = $thumbnailService; } @@ -70,14 +66,14 @@ final class UserAvatarController extends AbstractController private function serveFromUrl($url) { - $this->httpHelper->redirect($url); + $this->networkingService->redirect($url); } private function serveFromFile($sourceName, $size) { $this->thumbnailService->generateIfNeeded($sourceName, $size, $size); $thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size); - $this->networkingService->serve($this->fileDao->getFullPath($thumbnailName)); + $this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName)); } private function serveBlankFile($size) diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 8667e60d..b785142b 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -30,11 +30,11 @@ final class Dispatcher $this->config = $config; $this->databaseConnection = $databaseConnection; $this->httpHelper = $httpHelper; + $this->authService = $authService; + $this->tokenService = $tokenService; //if script fails prematurely, mark it as fail from advance $this->httpHelper->setResponseCode(500); - $this->authService = $authService; - $this->tokenService = $tokenService; foreach ($controllerRepository->getControllers() as $controller) $controller->registerRoutes($router); diff --git a/src/Helpers/HttpHelper.php b/src/Helpers/HttpHelper.php index b3da9448..448e92e8 100644 --- a/src/Helpers/HttpHelper.php +++ b/src/Helpers/HttpHelper.php @@ -13,11 +13,6 @@ class HttpHelper header($key . ': ' . $value); } - public function output($data) - { - echo $data; - } - public function outputJSON($data) { $encodedJson = json_encode((array) $data); @@ -28,6 +23,11 @@ class HttpHelper $this->output($encodedJson); } + public function output($data) + { + echo $data; + } + public function getRequestHeaders() { return getallheaders(); @@ -50,16 +50,4 @@ class HttpHelper $requestUri = preg_replace('/\?.*$/', '', $requestUri); return $requestUri; } - - public function redirect($destination) - { - $this->setResponseCode(307); - $this->setHeader('Location', $destination); - } - - public function nonCachedRedirect($destination) - { - $this->setResponseCode(303); - $this->setHeader('Location', $destination); - } } diff --git a/src/Services/NetworkingService.php b/src/Services/NetworkingService.php index 8432985f..ddd12f4d 100644 --- a/src/Services/NetworkingService.php +++ b/src/Services/NetworkingService.php @@ -11,7 +11,7 @@ class NetworkingService $this->httpHelper = $httpHelper; } - public function serve($fullPath, $options = []) + public function serveFile($fullPath, $options = []) { $daysToLive = isset($options->daysToLive) ? $options->daysToLive @@ -93,4 +93,16 @@ class NetworkingService return $result; } + + public function redirect($destination) + { + $this->httpHelper->setResponseCode(307); + $this->httpHelper->setHeader('Location', $destination); + } + + public function nonCachedRedirect($destination) + { + $this->httpHelper->setResponseCode(303); + $this->httpHelper->setHeader('Location', $destination); + } }