From 21013cf6ac0225040402052544b801ca28f31dfe Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Thu, 9 Aug 2018 21:55:39 -0600 Subject: [PATCH] [add] Generic asserts with custom errors --- .../src/application/SessionApplication.php | 20 +++++++-- api-payroll/src/service/Asserts.php | 41 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/api-payroll/src/application/SessionApplication.php b/api-payroll/src/application/SessionApplication.php index 62ff2fd..77aa0b6 100644 --- a/api-payroll/src/application/SessionApplication.php +++ b/api-payroll/src/application/SessionApplication.php @@ -43,7 +43,9 @@ class SessionApplication{ * @return mixed */ function getPassword($userName){ - $this->asserts->userName($userName); + $this->asserts->isNotEmpty($userName, "The username can't be empty"); + $this->asserts->isString($userName, "The username must be a string."); + $this->asserts->betweenLength($userName, 1, 50, "The username must have a length between 1 and 50 characters."); $stmt = $this->pdo->prepare("SELECT password FROM users WHERE name = :userName"); $stmt->execute(array(':userName' => $userName)); @@ -62,8 +64,12 @@ class SessionApplication{ * @throws Exception */ function newSession($userName, $password){ - $this->asserts->userName($userName); - $this->asserts->password($password); + $this->asserts->isNotEmpty($userName, "The username can't be empty"); + $this->asserts->isString($userName, "The username must be a string."); + $this->asserts->betweenLength($userName, 1, 50, "The username must have a length between 1 and 50 characters."); + $this->asserts->isNotEmpty($password, "The password can't be empty"); + $this->asserts->isString($password, "The password must be a string."); + $this->asserts->betweenLength($password, 1, 50, "The password must have a length between 1 and 50 characters."); $storedPassword = $this->getPassword($userName); @@ -93,6 +99,14 @@ class SessionApplication{ * @throws Exception */ function login($userName, $password){ + $this->asserts->isNotEmpty($userName, "The username can't be empty"); + $this->asserts->isString($userName, "The username must be a string."); + $this->asserts->betweenLength($userName, 1, 50, "The username must have a length between 1 and 50 characters."); + $this->asserts->isNotEmpty($password, "The password can't be empty"); + $this->asserts->isString($password, "The password must be a string."); + $this->asserts->betweenLength($password, 1, 50, "The password must have a length between 1 and 50 characters."); + + if($this->newSession($userName, $password)){ return array('status' => 'success', 'message' => 'Logged in successfully.'); } diff --git a/api-payroll/src/service/Asserts.php b/api-payroll/src/service/Asserts.php index 94b3023..9f9b96d 100644 --- a/api-payroll/src/service/Asserts.php +++ b/api-payroll/src/service/Asserts.php @@ -5,6 +5,47 @@ use Exception; use Respect\Validation\Validator as v; class Asserts{ + /** + * @param $string string + * @param $errorMessage string + * @throws Exception + */ + function isString($string, $errorMessage){ + $validation = v::stringType()->validate($string); + + if(!$validation){ + throw new Exception($errorMessage); + } + } + + /** + * @param $string string + * @param $errorMessage string + * @throws Exception + */ + function isNotEmpty($string, $errorMessage){ + $validation = v::notEmpty()->validate($string); + + if(!$validation){ + throw new Exception($errorMessage); + } + } + + /** + * @param $string string + * @param $min integer + * @param $max integer + * @param $errorMessage string + * @throws Exception + */ + function betweenLength($string, $min, $max, $errorMessage){ + $validation = v::length($min, $max)->validate($string); + + if(!$validation){ + throw new Exception($errorMessage); + } + } + /** * @param $string * @throws Exception