Reduced responsibilities of HttpHelper

This commit is contained in:
Marcin Kurczewski 2014-10-08 19:47:21 +02:00
parent 3744f0429a
commit 6c9954f298
5 changed files with 24 additions and 28 deletions

View file

@ -34,7 +34,7 @@ final class PostContentController extends AbstractController
public function getPostContent($postName) public function getPostContent($postName)
{ {
$post = $this->postService->getByName($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) public function getPostThumbnail($postName, $size)
@ -47,6 +47,6 @@ final class PostContentController extends AbstractController
$this->thumbnailService->generateIfNeeded($sourceName, $size, $size); $this->thumbnailService->generateIfNeeded($sourceName, $size, $size);
$thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size); $thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size);
$this->networkingService->serve($this->fileDao->getFullPath($thumbnailName)); $this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName));
} }
} }

View file

@ -2,7 +2,6 @@
namespace Szurubooru\Controllers; namespace Szurubooru\Controllers;
use Szurubooru\Dao\PublicFileDao; use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\User; use Szurubooru\Entities\User;
use Szurubooru\Helpers\HttpHelper;
use Szurubooru\Router; use Szurubooru\Router;
use Szurubooru\Services\NetworkingService; use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\ThumbnailService; use Szurubooru\Services\ThumbnailService;
@ -13,20 +12,17 @@ final class UserAvatarController extends AbstractController
private $fileDao; private $fileDao;
private $userService; private $userService;
private $networkingService; private $networkingService;
private $httpHelper;
private $thumbnailService; private $thumbnailService;
public function __construct( public function __construct(
PublicFileDao $fileDao, PublicFileDao $fileDao,
UserService $userService, UserService $userService,
NetworkingService $networkingService, NetworkingService $networkingService,
HttpHelper $httpHelper,
ThumbnailService $thumbnailService) ThumbnailService $thumbnailService)
{ {
$this->fileDao = $fileDao; $this->fileDao = $fileDao;
$this->userService = $userService; $this->userService = $userService;
$this->networkingService = $networkingService; $this->networkingService = $networkingService;
$this->httpHelper = $httpHelper;
$this->thumbnailService = $thumbnailService; $this->thumbnailService = $thumbnailService;
} }
@ -70,14 +66,14 @@ final class UserAvatarController extends AbstractController
private function serveFromUrl($url) private function serveFromUrl($url)
{ {
$this->httpHelper->redirect($url); $this->networkingService->redirect($url);
} }
private function serveFromFile($sourceName, $size) private function serveFromFile($sourceName, $size)
{ {
$this->thumbnailService->generateIfNeeded($sourceName, $size, $size); $this->thumbnailService->generateIfNeeded($sourceName, $size, $size);
$thumbnailName = $this->thumbnailService->getThumbnailName($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) private function serveBlankFile($size)

View file

@ -30,11 +30,11 @@ final class Dispatcher
$this->config = $config; $this->config = $config;
$this->databaseConnection = $databaseConnection; $this->databaseConnection = $databaseConnection;
$this->httpHelper = $httpHelper; $this->httpHelper = $httpHelper;
$this->authService = $authService;
$this->tokenService = $tokenService;
//if script fails prematurely, mark it as fail from advance //if script fails prematurely, mark it as fail from advance
$this->httpHelper->setResponseCode(500); $this->httpHelper->setResponseCode(500);
$this->authService = $authService;
$this->tokenService = $tokenService;
foreach ($controllerRepository->getControllers() as $controller) foreach ($controllerRepository->getControllers() as $controller)
$controller->registerRoutes($router); $controller->registerRoutes($router);

View file

@ -13,11 +13,6 @@ class HttpHelper
header($key . ': ' . $value); header($key . ': ' . $value);
} }
public function output($data)
{
echo $data;
}
public function outputJSON($data) public function outputJSON($data)
{ {
$encodedJson = json_encode((array) $data); $encodedJson = json_encode((array) $data);
@ -28,6 +23,11 @@ class HttpHelper
$this->output($encodedJson); $this->output($encodedJson);
} }
public function output($data)
{
echo $data;
}
public function getRequestHeaders() public function getRequestHeaders()
{ {
return getallheaders(); return getallheaders();
@ -50,16 +50,4 @@ class HttpHelper
$requestUri = preg_replace('/\?.*$/', '', $requestUri); $requestUri = preg_replace('/\?.*$/', '', $requestUri);
return $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);
}
} }

View file

@ -11,7 +11,7 @@ class NetworkingService
$this->httpHelper = $httpHelper; $this->httpHelper = $httpHelper;
} }
public function serve($fullPath, $options = []) public function serveFile($fullPath, $options = [])
{ {
$daysToLive = isset($options->daysToLive) $daysToLive = isset($options->daysToLive)
? $options->daysToLive ? $options->daysToLive
@ -93,4 +93,16 @@ class NetworkingService
return $result; 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);
}
} }