[add] Asserts

This commit is contained in:
2018-08-05 20:37:31 -06:00
parent e033e1ce58
commit 882a9ccad1
5 changed files with 228 additions and 8 deletions

View File

@@ -1,13 +1,17 @@
<?php
namespace App\Application;
use Exception;
class SessionApplication{
private $pdo;
private $cryptographyService;
private $asserts;
function __construct($mysql, $cryptographyService){
function __construct($mysql, $cryptographyService, $asserts){
$this->cryptographyService = $cryptographyService;
$this->pdo = $mysql;
$this->asserts = $asserts;
$this->databaseSelectQueryErrorMessage = 'There was an error inserting the record.';
}
@@ -39,6 +43,8 @@ class SessionApplication{
* @return mixed
*/
function getPassword($userName){
$this->asserts->userName($userName);
$stmt = $this->pdo->prepare("SELECT password FROM users WHERE name = :userName");
$stmt->execute(array(':userName' => $userName));
$results = $stmt->fetchAll();
@@ -56,6 +62,9 @@ class SessionApplication{
* @throws Exception
*/
function newSession($userName, $password){
$this->asserts->userName($userName);
$this->asserts->password($password);
$storedPassword = $this->getPassword($userName);
// If the credentials don't match anything in the the records

View File

@@ -18,7 +18,7 @@ $container['logger'] = function ($c) {
return $logger;
};
// Mysql connecrion
// Mysql connection
$container['mysql'] = function ($c) {
$mysqlSettings = $c->get('settings')['mysql'];
@@ -32,8 +32,6 @@ $container['mysql'] = function ($c) {
// Generic error messages
$databaseConnectionErrorMessage = $mysqlSettings['databaseConnectionErrorMessage'];
$databaseSelectQueryErrorMessage = $mysqlSettings['databaseSelectQueryErrorMessage'];
$databaseInsertQueryErrorMessage = $mysqlSettings['databaseInsertQueryErrorMessage'];
// Initiate the connection
$dsn = "mysql:host=$host;dbname=$database;charset=$charset";
@@ -53,8 +51,14 @@ $container['cryptographyService'] = function ($c) {
return $cryptographyService;
};
// Assert functions
$container['asserts'] = function ($c) {
$asserts = new App\Service\Asserts();
return $asserts;
};
// The session application
$container['sessionApplication'] = function ($c) {
$sessionApplication = new App\Application\SessionApplication($c['mysql'], $c['cryptographyService']);
$sessionApplication = new App\Application\SessionApplication($c['mysql'], $c['cryptographyService'], $c['asserts']);
return $sessionApplication;
};

View File

@@ -0,0 +1,84 @@
<?php
namespace App\Service;
use Exception;
use Respect\Validation\Validator as v;
class Asserts{
/**
* @param $string
* @throws Exception
*/
function userName($string){
$validateFirstName = v::stringType()->notEmpty()->length(1, 50)->validate($string);
if(!$validateFirstName){
throw new Exception('The user name must be a string between 1 and 50 characters');
}
}
/**
* @param $string
* @throws Exception
*/
function password($string){
$validateFirstName = v::stringType()->notEmpty()->length(1, 50)->validate($string);
if(!$validateFirstName){
throw new Exception('The password must be a string between 1 and 50 characters');
}
}
/**
* @param $string
* @throws Exception
*/
function firstName($string){
$validateFirstName = v::stringType()->notEmpty()->length(1, 100)->validate($string);
if(!$validateFirstName){
throw new Exception('The first name must be a string between 1 and 100 characters');
}
}
/**
* @param $string
* @throws Exception
*/
function middleName($string){
if(!v::stringType()->notEmpty()->length(1, 100)->validate($string)){
throw new Exception('The middle name must be a string between 1 and 100 characters');
}
}
/**
* @param $string
* @throws Exception
*/
function birthDate($string){
if(!v::date('Y-m-d')->notEmpty()->validate($string)){
throw new Exception('The birth date must be in the yyyy-mm-dd format');
}
}
/**
* @param $string
* @throws Exception
*/
function email($string){
if(!v::stringType()->notEmpty()->length(1, 100)->validate($string)){
throw new Exception('The email must be a string between 1 and 100 characters');
}
}
/**
* @param $string
* @throws Exception
*/
function phone($string){
if(!v::digit()->notEmpty()->length(10, 10)->validate($string)){
throw new Exception('The phone must be a numeric value of 10 digits');
}
}
}
?>