From 623de4b5e08625ef1bbf214e265e85b162d3de61 Mon Sep 17 00:00:00 2001 From: Sergio Brighenti Date: Mon, 2 Mar 2020 21:43:34 +0100 Subject: [PATCH] Refactoring --- app/Controllers/Auth/RegisterController.php | 23 ++++------- app/Controllers/Controller.php | 4 +- app/Controllers/UserController.php | 42 +++++++------------- app/Validators/ValidateUser.php | 44 +++++++++++++++++++++ 4 files changed, 67 insertions(+), 46 deletions(-) create mode 100644 app/Validators/ValidateUser.php diff --git a/app/Controllers/Auth/RegisterController.php b/app/Controllers/Auth/RegisterController.php index 32cbb55..bc7e273 100644 --- a/app/Controllers/Auth/RegisterController.php +++ b/app/Controllers/Auth/RegisterController.php @@ -3,7 +3,9 @@ namespace App\Controllers\Auth; +use App\Controllers\Common\ValidateUser; use App\Controllers\Controller; +use App\Exceptions\ValidationException; use App\Web\Mail; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -11,6 +13,7 @@ use Slim\Exception\HttpNotFoundException; class RegisterController extends Controller { + use ValidateUser; /** * @param Request $request @@ -51,10 +54,10 @@ class RegisterController extends Controller throw new HttpNotFoundException($request); } - if (param($request, 'email') === null && !filter_var(param($request, 'email'), FILTER_VALIDATE_EMAIL)) { - $this->session->alert(lang('email_required'), 'danger'); - - return redirect($response, route('register.show')); + try { + $this->validateUser($request, $response, route('register.show')); + } catch (ValidationException $e) { + return $e->response(); } if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `email` = ?', param($request, 'email'))->fetch()->count > 0) { @@ -63,18 +66,6 @@ class RegisterController extends Controller return redirect($response, route('register.show')); } - if (param($request, 'username') === null) { - $this->session->alert(lang('username_required'), 'danger'); - - return redirect($response, route('register.show')); - } - - if (param($request, 'password') === null) { - $this->session->alert(lang('password_required'), 'danger'); - - return redirect($response, route('register.show')); - } - if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `username` = ?', param($request, 'username'))->fetch()->count > 0) { $this->session->alert(lang('username_taken'), 'danger'); diff --git a/app/Controllers/Controller.php b/app/Controllers/Controller.php index ca77268..ad05bdd 100644 --- a/app/Controllers/Controller.php +++ b/app/Controllers/Controller.php @@ -16,9 +16,9 @@ use Slim\Exception\HttpNotFoundException; use Slim\Exception\HttpUnauthorizedException; /** - * @property Session|null session + * @property Session session * @property View view - * @property DB|null database + * @property DB database * @property Logger|null logger * @property Filesystem|null storage * @property Lang lang diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 45a9abf..d904ddf 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -2,6 +2,8 @@ namespace App\Controllers; +use App\Exceptions\ValidationException; +use App\Validators\ValidateUser; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Exception\HttpNotFoundException; @@ -9,6 +11,8 @@ use Slim\Exception\HttpUnauthorizedException; class UserController extends Controller { + use ValidateUser; + const PER_PAGE = 15; /** @@ -66,10 +70,10 @@ class UserController extends Controller */ public function store(Request $request, Response $response): Response { - if (param($request, 'email') === null && !filter_var(param($request, 'email'), FILTER_VALIDATE_EMAIL)) { - $this->session->alert(lang('email_required'), 'danger'); - - return redirect($response, route('user.create')); + try { + $this->validateUser($request, $response, route('user.create')); + } catch (ValidationException $e) { + return $e->response(); } if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `email` = ?', param($request, 'email'))->fetch()->count > 0) { @@ -78,18 +82,6 @@ class UserController extends Controller return redirect($response, route('user.create')); } - if (param($request, 'username') === null) { - $this->session->alert(lang('username_required'), 'danger'); - - return redirect($response, route('user.create')); - } - - if (param($request, 'password') === null) { - $this->session->alert(lang('password_required'), 'danger'); - - return redirect($response, route('user.create')); - } - if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `username` = ?', param($request, 'username'))->fetch()->count > 0) { $this->session->alert(lang('username_taken'), 'danger'); @@ -169,26 +161,20 @@ class UserController extends Controller */ public function update(Request $request, Response $response, int $id): Response { - $user = $this->getUser($request, $id, false); - - if (param($request, 'email') === null && !filter_var(param($request, 'email'), FILTER_VALIDATE_EMAIL)) { - $this->session->alert(lang('email_required'), 'danger'); - - return redirect($response, route('user.edit', ['id' => $id])); + try { + $this->validateUser($request, $response, route('user.edit', ['id' => $id])); + } catch (ValidationException $e) { + return $e->response(); } + $user = $this->getUser($request, $id, false); + if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `email` = ? AND `email` <> ?', [param($request, 'email'), $user->email])->fetch()->count > 0) { $this->session->alert(lang('email_taken'), 'danger'); return redirect($response, route('user.edit', ['id' => $id])); } - if (param($request, 'username') === null) { - $this->session->alert(lang('username_required'), 'danger'); - - return redirect($response, route('user.edit', ['id' => $id])); - } - if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `username` = ? AND `username` <> ?', [param($request, 'username'), $user->username])->fetch()->count > 0) { $this->session->alert(lang('username_taken'), 'danger'); diff --git a/app/Validators/ValidateUser.php b/app/Validators/ValidateUser.php new file mode 100644 index 0000000..4ed2a91 --- /dev/null +++ b/app/Validators/ValidateUser.php @@ -0,0 +1,44 @@ +session->alert(lang('email_required'), 'danger'); + + throw new ValidationException(redirect($response, $routeOnFail)); + } + + if (param($request, 'username') === null) { + $this->session->alert(lang('username_required'), 'danger'); + + throw new ValidationException(redirect($response, $routeOnFail)); + } + + if (param($request, 'password') === null) { + $this->session->alert(lang('password_required'), 'danger'); + + throw new ValidationException(redirect($response, $routeOnFail)); + } + + return true; + } +} \ No newline at end of file