Refactored controller DTOs
This commit is contained in:
parent
4c0a408152
commit
926c5af0d6
9 changed files with 82 additions and 51 deletions
|
@ -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(),
|
||||
];
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
12
src/Controllers/ViewProxies/AbstractViewProxy.php
Normal file
12
src/Controllers/ViewProxies/AbstractViewProxy.php
Normal 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);
|
||||
}
|
||||
}
|
16
src/Controllers/ViewProxies/TokenViewProxy.php
Normal file
16
src/Controllers/ViewProxies/TokenViewProxy.php
Normal 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;
|
||||
}
|
||||
}
|
16
src/Controllers/ViewProxies/UserViewProxy.php
Normal file
16
src/Controllers/ViewProxies/UserViewProxy.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue