Asserts and error handling #3
@ -15,7 +15,8 @@
|
|||||||
"php": ">=5.5.0",
|
"php": ">=5.5.0",
|
||||||
"slim/slim": "^3.1",
|
"slim/slim": "^3.1",
|
||||||
"slim/php-view": "^2.0",
|
"slim/php-view": "^2.0",
|
||||||
"monolog/monolog": "^1.17"
|
"monolog/monolog": "^1.17",
|
||||||
|
"respect/validation": "^1.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": ">=4.8 < 6.0"
|
"phpunit/phpunit": ">=4.8 < 6.0"
|
||||||
|
126
api-payroll/composer.lock
generated
126
api-payroll/composer.lock
generated
@ -4,8 +4,8 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"hash": "9f4397e11cb2603e7754216c4f59c7ad",
|
"hash": "677ee7ce2e986cfa3ab0df77d78e0a4c",
|
||||||
"content-hash": "5e16cb7781829836a704bd8767830833",
|
"content-hash": "fddce0c9f8dd9b23d45f6d6e4b4b6310",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "container-interop/container-interop",
|
"name": "container-interop/container-interop",
|
||||||
@ -358,6 +358,69 @@
|
|||||||
],
|
],
|
||||||
"time": "2016-10-10 12:19:37"
|
"time": "2016-10-10 12:19:37"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "respect/validation",
|
||||||
|
"version": "1.1.22",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/Respect/Validation.git",
|
||||||
|
"reference": "19d6ec893994912d21b390c43d287816ab070772"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/Respect/Validation/zipball/19d6ec893994912d21b390c43d287816ab070772",
|
||||||
|
"reference": "19d6ec893994912d21b390c43d287816ab070772",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=5.4",
|
||||||
|
"symfony/polyfill-mbstring": "^1.2"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"egulias/email-validator": "~1.2",
|
||||||
|
"mikey179/vfsstream": "^1.5",
|
||||||
|
"phpunit/phpunit": "~4.0",
|
||||||
|
"symfony/validator": "~2.6.9",
|
||||||
|
"zendframework/zend-validator": "~2.3"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"egulias/email-validator": "Strict (RFC compliant) email validation",
|
||||||
|
"ext-bcmath": "Arbitrary Precision Mathematics",
|
||||||
|
"ext-mbstring": "Multibyte String Functions",
|
||||||
|
"friendsofphp/php-cs-fixer": "Fix PSR2 and other coding style issues",
|
||||||
|
"symfony/validator": "Use Symfony validator through Respect\\Validation",
|
||||||
|
"zendframework/zend-validator": "Use Zend Framework validator through Respect\\Validation"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-master": "1.1-dev"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Respect\\Validation\\": "library/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"BSD Style"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Respect/Validation Contributors",
|
||||||
|
"homepage": "https://github.com/Respect/Validation/graphs/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "The most awesome validation engine ever created for PHP",
|
||||||
|
"homepage": "http://respect.github.io/Validation/",
|
||||||
|
"keywords": [
|
||||||
|
"respect",
|
||||||
|
"validation",
|
||||||
|
"validator"
|
||||||
|
],
|
||||||
|
"time": "2018-08-01 13:06:54"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "slim/php-view",
|
"name": "slim/php-view",
|
||||||
"version": "2.2.0",
|
"version": "2.2.0",
|
||||||
@ -477,6 +540,65 @@
|
|||||||
"router"
|
"router"
|
||||||
],
|
],
|
||||||
"time": "2018-04-19 19:29:08"
|
"time": "2018-04-19 19:29:08"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "symfony/polyfill-mbstring",
|
||||||
|
"version": "v1.8.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/symfony/polyfill-mbstring.git",
|
||||||
|
"reference": "3296adf6a6454a050679cde90f95350ad604b171"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/3296adf6a6454a050679cde90f95350ad604b171",
|
||||||
|
"reference": "3296adf6a6454a050679cde90f95350ad604b171",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=5.3.3"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"ext-mbstring": "For best performance"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-master": "1.8-dev"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Symfony\\Polyfill\\Mbstring\\": ""
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"bootstrap.php"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Nicolas Grekas",
|
||||||
|
"email": "p@tchwork.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Symfony Community",
|
||||||
|
"homepage": "https://symfony.com/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Symfony polyfill for the Mbstring extension",
|
||||||
|
"homepage": "https://symfony.com",
|
||||||
|
"keywords": [
|
||||||
|
"compatibility",
|
||||||
|
"mbstring",
|
||||||
|
"polyfill",
|
||||||
|
"portable",
|
||||||
|
"shim"
|
||||||
|
],
|
||||||
|
"time": "2018-04-26 10:06:28"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
|
@ -17,6 +17,20 @@ session_start();
|
|||||||
$settings = require __DIR__ . '/../src/settings.php';
|
$settings = require __DIR__ . '/../src/settings.php';
|
||||||
$app = new \Slim\App($settings);
|
$app = new \Slim\App($settings);
|
||||||
|
|
||||||
|
// Custom error handling
|
||||||
|
$c = $app->getContainer();
|
||||||
|
$c['errorHandler'] = function ($c) {
|
||||||
|
return function ($request, $response, $exception) use ($c) {
|
||||||
|
$data = [
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => $exception->getMessage()
|
||||||
|
];
|
||||||
|
return $c['response']->withStatus(500)
|
||||||
|
->withHeader('Content-Type', 'application/json')
|
||||||
|
->write(json_encode($data));
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// Set up dependencies
|
// Set up dependencies
|
||||||
require __DIR__ . '/../src/dependencies.php';
|
require __DIR__ . '/../src/dependencies.php';
|
||||||
|
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace App\Application;
|
namespace App\Application;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
class SessionApplication{
|
class SessionApplication{
|
||||||
private $pdo;
|
private $pdo;
|
||||||
private $cryptographyService;
|
private $cryptographyService;
|
||||||
|
private $asserts;
|
||||||
|
|
||||||
function __construct($mysql, $cryptographyService){
|
function __construct($mysql, $cryptographyService, $asserts){
|
||||||
$this->cryptographyService = $cryptographyService;
|
$this->cryptographyService = $cryptographyService;
|
||||||
$this->pdo = $mysql;
|
$this->pdo = $mysql;
|
||||||
|
$this->asserts = $asserts;
|
||||||
|
|
||||||
$this->databaseSelectQueryErrorMessage = 'There was an error inserting the record.';
|
$this->databaseSelectQueryErrorMessage = 'There was an error inserting the record.';
|
||||||
}
|
}
|
||||||
@ -39,6 +43,8 @@ class SessionApplication{
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
function getPassword($userName){
|
function getPassword($userName){
|
||||||
|
$this->asserts->userName($userName);
|
||||||
|
|
||||||
$stmt = $this->pdo->prepare("SELECT password FROM users WHERE name = :userName");
|
$stmt = $this->pdo->prepare("SELECT password FROM users WHERE name = :userName");
|
||||||
$stmt->execute(array(':userName' => $userName));
|
$stmt->execute(array(':userName' => $userName));
|
||||||
$results = $stmt->fetchAll();
|
$results = $stmt->fetchAll();
|
||||||
@ -56,6 +62,9 @@ class SessionApplication{
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
function newSession($userName, $password){
|
function newSession($userName, $password){
|
||||||
|
$this->asserts->userName($userName);
|
||||||
|
$this->asserts->password($password);
|
||||||
|
|
||||||
$storedPassword = $this->getPassword($userName);
|
$storedPassword = $this->getPassword($userName);
|
||||||
|
|
||||||
// If the credentials don't match anything in the the records
|
// If the credentials don't match anything in the the records
|
||||||
|
@ -18,7 +18,7 @@ $container['logger'] = function ($c) {
|
|||||||
return $logger;
|
return $logger;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Mysql connecrion
|
// Mysql connection
|
||||||
$container['mysql'] = function ($c) {
|
$container['mysql'] = function ($c) {
|
||||||
$mysqlSettings = $c->get('settings')['mysql'];
|
$mysqlSettings = $c->get('settings')['mysql'];
|
||||||
|
|
||||||
@ -32,8 +32,6 @@ $container['mysql'] = function ($c) {
|
|||||||
|
|
||||||
// Generic error messages
|
// Generic error messages
|
||||||
$databaseConnectionErrorMessage = $mysqlSettings['databaseConnectionErrorMessage'];
|
$databaseConnectionErrorMessage = $mysqlSettings['databaseConnectionErrorMessage'];
|
||||||
$databaseSelectQueryErrorMessage = $mysqlSettings['databaseSelectQueryErrorMessage'];
|
|
||||||
$databaseInsertQueryErrorMessage = $mysqlSettings['databaseInsertQueryErrorMessage'];
|
|
||||||
|
|
||||||
// Initiate the connection
|
// Initiate the connection
|
||||||
$dsn = "mysql:host=$host;dbname=$database;charset=$charset";
|
$dsn = "mysql:host=$host;dbname=$database;charset=$charset";
|
||||||
@ -53,8 +51,14 @@ $container['cryptographyService'] = function ($c) {
|
|||||||
return $cryptographyService;
|
return $cryptographyService;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Assert functions
|
||||||
|
$container['asserts'] = function ($c) {
|
||||||
|
$asserts = new App\Service\Asserts();
|
||||||
|
return $asserts;
|
||||||
|
};
|
||||||
|
|
||||||
// The session application
|
// The session application
|
||||||
$container['sessionApplication'] = function ($c) {
|
$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;
|
return $sessionApplication;
|
||||||
};
|
};
|
||||||
|
84
api-payroll/src/service/Asserts.php
Normal file
84
api-payroll/src/service/Asserts.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
Loading…
Reference in New Issue
Block a user