From 926c5af0d6e288c3ee03a072f21d52055ec50d4f Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 5 Sep 2014 19:18:49 +0200 Subject: [PATCH] Refactored controller DTOs --- src/Controllers/AuthController.php | 12 +++++++--- src/Controllers/UserController.php | 23 ++++++++----------- .../ViewProxies/AbstractViewProxy.php | 12 ++++++++++ .../ViewProxies/TokenViewProxy.php | 16 +++++++++++++ src/Controllers/ViewProxies/UserViewProxy.php | 16 +++++++++++++ src/FormData/RegistrationFormData.php | 10 ++++++++ src/FormData/SearchFormData.php | 10 ++++++++ src/ViewProxies/Token.php | 17 -------------- src/ViewProxies/User.php | 17 -------------- 9 files changed, 82 insertions(+), 51 deletions(-) create mode 100644 src/Controllers/ViewProxies/AbstractViewProxy.php create mode 100644 src/Controllers/ViewProxies/TokenViewProxy.php create mode 100644 src/Controllers/ViewProxies/UserViewProxy.php delete mode 100644 src/ViewProxies/Token.php delete mode 100644 src/ViewProxies/User.php diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index 0a14f615..9b753d04 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -7,17 +7,23 @@ final class AuthController extends AbstractController private $userService; private $passwordService; private $inputReader; + private $userViewProxy; + private $tokenViewProxy; public function __construct( \Szurubooru\Services\AuthService $authService, \Szurubooru\Services\UserService $userService, \Szurubooru\Services\PasswordService $passwordService, - \Szurubooru\Helpers\InputReader $inputReader) + \Szurubooru\Helpers\InputReader $inputReader, + \Szurubooru\Controllers\ViewProxies\UserViewProxy $userViewProxy, + \Szurubooru\Controllers\ViewProxies\TokenViewProxy $tokenViewProxy) { $this->authService = $authService; $this->userService = $userService; $this->passwordService = $passwordService; $this->inputReader = $inputReader; + $this->userViewProxy = $userViewProxy; + $this->tokenViewProxy = $tokenViewProxy; } public function registerRoutes(\Szurubooru\Router $router) @@ -43,8 +49,8 @@ final class AuthController extends AbstractController return [ - 'token' => new \Szurubooru\ViewProxies\Token($this->authService->getLoginToken()), - 'user' => new \Szurubooru\ViewProxies\User($this->authService->getLoggedInUser()), + 'token' => $this->tokenViewProxy->fromEntity($this->authService->getLoginToken()), + 'user' => $this->userViewProxy->fromEntity($this->authService->getLoggedInUser()), 'privileges' => $this->authService->getCurrentPrivileges(), ]; } diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 7f39ff5f..782f0317 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -6,15 +6,18 @@ final class UserController extends AbstractController private $authService; private $userService; private $inputReader; + private $userViewProxy; public function __construct( \Szurubooru\Services\AuthService $authService, \Szurubooru\Services\UserService $userService, - \Szurubooru\Helpers\InputReader $inputReader) + \Szurubooru\Helpers\InputReader $inputReader, + \Szurubooru\Controllers\ViewProxies\UserViewProxy $userViewProxy) { $this->authService = $authService; $this->userService = $userService; $this->inputReader = $inputReader; + $this->userViewProxy = $userViewProxy; } public function registerRoutes(\Szurubooru\Router $router) @@ -33,20 +36,16 @@ final class UserController extends AbstractController $user = $this->userService->getByName($name); if (!$user) throw new \DomainException('User with name "' . $name . '" was not found.'); - return new \Szurubooru\ViewProxies\User($user); + return $this->userViewProxy->fromEntity($user); } public function getFiltered() { $this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_LIST_USERS); - //todo: move this to form data constructor - $searchFormData = new \Szurubooru\FormData\SearchFormData; - $searchFormData->query = $this->inputReader->query; - $searchFormData->order = $this->inputReader->order; - $searchFormData->pageNumber = $this->inputReader->page; + $searchFormData = new \Szurubooru\FormData\SearchFormData($this->inputReader); $searchResult = $this->userService->getFiltered($searchFormData); - $entities = array_map(function($user) { return new \Szurubooru\ViewProxies\User($user); }, $searchResult->entities); + $entities = $this->userViewProxy->fromArray($searchResult->entities); return [ 'data' => $entities, 'pageSize' => $searchResult->filter->pageSize, @@ -57,13 +56,9 @@ final class UserController extends AbstractController { $this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_REGISTER); - $input = new \Szurubooru\FormData\RegistrationFormData; - //todo: move this to form data constructor - $input->name = $this->inputReader->userName; - $input->password = $this->inputReader->password; - $input->email = $this->inputReader->email; + $input = new \Szurubooru\FormData\RegistrationFormData($this->inputReader); $user = $this->userService->register($input); - return new \Szurubooru\ViewProxies\User($user); + return $this->userViewProxy->fromEntity($user); } public function update($name) diff --git a/src/Controllers/ViewProxies/AbstractViewProxy.php b/src/Controllers/ViewProxies/AbstractViewProxy.php new file mode 100644 index 00000000..5e532b9e --- /dev/null +++ b/src/Controllers/ViewProxies/AbstractViewProxy.php @@ -0,0 +1,12 @@ +name = $token->name; + $result->purpose = $token->purpose; + } + return $result; + } +} diff --git a/src/Controllers/ViewProxies/UserViewProxy.php b/src/Controllers/ViewProxies/UserViewProxy.php new file mode 100644 index 00000000..a1f21525 --- /dev/null +++ b/src/Controllers/ViewProxies/UserViewProxy.php @@ -0,0 +1,16 @@ +id = $user->id; + $result->name = $user->name; + } + return $result; + } +} diff --git a/src/FormData/RegistrationFormData.php b/src/FormData/RegistrationFormData.php index 99f2a9b2..7ed83a42 100644 --- a/src/FormData/RegistrationFormData.php +++ b/src/FormData/RegistrationFormData.php @@ -6,4 +6,14 @@ class RegistrationFormData public $name; public $password; public $email; + + public function __construct($inputReader = null) + { + if ($inputReader !== null) + { + $this->name = $inputReader->userName; + $this->password = $inputReader->password; + $this->email = $inputReader->email; + } + } } diff --git a/src/FormData/SearchFormData.php b/src/FormData/SearchFormData.php index 4c31332f..fc71e35b 100644 --- a/src/FormData/SearchFormData.php +++ b/src/FormData/SearchFormData.php @@ -6,4 +6,14 @@ class SearchFormData public $query; public $order; public $pageNumber; + + public function __construct($inputReader = null) + { + if ($inputReader !== null) + { + $this->query = $inputReader->query; + $this->order = $inputReader->order; + $this->pageNumber = $inputReader->page; + } + } } diff --git a/src/ViewProxies/Token.php b/src/ViewProxies/Token.php deleted file mode 100644 index b31218ad..00000000 --- a/src/ViewProxies/Token.php +++ /dev/null @@ -1,17 +0,0 @@ -name = $token->name; - $this->purpose = $token->purpose; - } -} diff --git a/src/ViewProxies/User.php b/src/ViewProxies/User.php deleted file mode 100644 index 9e7bc49d..00000000 --- a/src/ViewProxies/User.php +++ /dev/null @@ -1,17 +0,0 @@ -id = $user->id; - $this->name = $user->name; - } -}