Refactored controller DTOs

This commit is contained in:
Marcin Kurczewski 2014-09-05 19:18:49 +02:00
parent 4c0a408152
commit 926c5af0d6
9 changed files with 82 additions and 51 deletions

View file

@ -7,17 +7,23 @@ final class AuthController extends AbstractController
private $userService; private $userService;
private $passwordService; private $passwordService;
private $inputReader; private $inputReader;
private $userViewProxy;
private $tokenViewProxy;
public function __construct( public function __construct(
\Szurubooru\Services\AuthService $authService, \Szurubooru\Services\AuthService $authService,
\Szurubooru\Services\UserService $userService, \Szurubooru\Services\UserService $userService,
\Szurubooru\Services\PasswordService $passwordService, \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->authService = $authService;
$this->userService = $userService; $this->userService = $userService;
$this->passwordService = $passwordService; $this->passwordService = $passwordService;
$this->inputReader = $inputReader; $this->inputReader = $inputReader;
$this->userViewProxy = $userViewProxy;
$this->tokenViewProxy = $tokenViewProxy;
} }
public function registerRoutes(\Szurubooru\Router $router) public function registerRoutes(\Szurubooru\Router $router)
@ -43,8 +49,8 @@ final class AuthController extends AbstractController
return return
[ [
'token' => new \Szurubooru\ViewProxies\Token($this->authService->getLoginToken()), 'token' => $this->tokenViewProxy->fromEntity($this->authService->getLoginToken()),
'user' => new \Szurubooru\ViewProxies\User($this->authService->getLoggedInUser()), 'user' => $this->userViewProxy->fromEntity($this->authService->getLoggedInUser()),
'privileges' => $this->authService->getCurrentPrivileges(), 'privileges' => $this->authService->getCurrentPrivileges(),
]; ];
} }

View file

@ -6,15 +6,18 @@ final class UserController extends AbstractController
private $authService; private $authService;
private $userService; private $userService;
private $inputReader; private $inputReader;
private $userViewProxy;
public function __construct( public function __construct(
\Szurubooru\Services\AuthService $authService, \Szurubooru\Services\AuthService $authService,
\Szurubooru\Services\UserService $userService, \Szurubooru\Services\UserService $userService,
\Szurubooru\Helpers\InputReader $inputReader) \Szurubooru\Helpers\InputReader $inputReader,
\Szurubooru\Controllers\ViewProxies\UserViewProxy $userViewProxy)
{ {
$this->authService = $authService; $this->authService = $authService;
$this->userService = $userService; $this->userService = $userService;
$this->inputReader = $inputReader; $this->inputReader = $inputReader;
$this->userViewProxy = $userViewProxy;
} }
public function registerRoutes(\Szurubooru\Router $router) public function registerRoutes(\Szurubooru\Router $router)
@ -33,20 +36,16 @@ final class UserController extends AbstractController
$user = $this->userService->getByName($name); $user = $this->userService->getByName($name);
if (!$user) if (!$user)
throw new \DomainException('User with name "' . $name . '" was not found.'); throw new \DomainException('User with name "' . $name . '" was not found.');
return new \Szurubooru\ViewProxies\User($user); return $this->userViewProxy->fromEntity($user);
} }
public function getFiltered() public function getFiltered()
{ {
$this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_LIST_USERS); $this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_LIST_USERS);
//todo: move this to form data constructor $searchFormData = new \Szurubooru\FormData\SearchFormData($this->inputReader);
$searchFormData = new \Szurubooru\FormData\SearchFormData;
$searchFormData->query = $this->inputReader->query;
$searchFormData->order = $this->inputReader->order;
$searchFormData->pageNumber = $this->inputReader->page;
$searchResult = $this->userService->getFiltered($searchFormData); $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 [ return [
'data' => $entities, 'data' => $entities,
'pageSize' => $searchResult->filter->pageSize, 'pageSize' => $searchResult->filter->pageSize,
@ -57,13 +56,9 @@ final class UserController extends AbstractController
{ {
$this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_REGISTER); $this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_REGISTER);
$input = new \Szurubooru\FormData\RegistrationFormData; $input = new \Szurubooru\FormData\RegistrationFormData($this->inputReader);
//todo: move this to form data constructor
$input->name = $this->inputReader->userName;
$input->password = $this->inputReader->password;
$input->email = $this->inputReader->email;
$user = $this->userService->register($input); $user = $this->userService->register($input);
return new \Szurubooru\ViewProxies\User($user); return $this->userViewProxy->fromEntity($user);
} }
public function update($name) public function update($name)

View file

@ -0,0 +1,12 @@
<?php
namespace Szurubooru\Controllers\ViewProxies;
abstract class AbstractViewProxy
{
public abstract function fromEntity($entity);
public function fromArray($entities)
{
return array_map(function($entity) { return static::fromEntity($entity); }, $entities);
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace Szurubooru\Controllers\ViewProxies;
class TokenViewProxy extends AbstractViewProxy
{
public function fromEntity($token)
{
$result = new \StdClass;
if ($token)
{
$result->name = $token->name;
$result->purpose = $token->purpose;
}
return $result;
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace Szurubooru\Controllers\ViewProxies;
class UserViewProxy extends AbstractViewProxy
{
public function fromEntity($user)
{
$result = new \StdClass;
if ($user)
{
$result->id = $user->id;
$result->name = $user->name;
}
return $result;
}
}

View file

@ -6,4 +6,14 @@ class RegistrationFormData
public $name; public $name;
public $password; public $password;
public $email; public $email;
public function __construct($inputReader = null)
{
if ($inputReader !== null)
{
$this->name = $inputReader->userName;
$this->password = $inputReader->password;
$this->email = $inputReader->email;
}
}
} }

View file

@ -6,4 +6,14 @@ class SearchFormData
public $query; public $query;
public $order; public $order;
public $pageNumber; public $pageNumber;
public function __construct($inputReader = null)
{
if ($inputReader !== null)
{
$this->query = $inputReader->query;
$this->order = $inputReader->order;
$this->pageNumber = $inputReader->page;
}
}
} }

View file

@ -1,17 +0,0 @@
<?php
namespace Szurubooru\ViewProxies;
class Token
{
public $name;
public $purpose;
public function __construct($token)
{
if (!$token)
return;
$this->name = $token->name;
$this->purpose = $token->purpose;
}
}

View file

@ -1,17 +0,0 @@
<?php
namespace Szurubooru\ViewProxies;
class User
{
public $id;
public $name;
public function __construct($user)
{
if (!$user)
return;
$this->id = $user->id;
$this->name = $user->name;
}
}