From 11329651956b2b0ecc8b321346ecd4d1e16a14d9 Mon Sep 17 00:00:00 2001 From: Hector Date: Sun, 5 Jan 2014 00:10:10 +0100 Subject: [PATCH] Version 2.23, released 04/01/2014. [Refactor] - Modified libraries concept to Service concepts. A Service is tightly related to a controller. It stores intelligence regarding what to do with validated information, which models request a CRUD, and informs the controller about the message to put into the related view. [Refactor] - Modified Hecnel Framework in order to use the new Model architecture. --- application/controllers/Login.php | 14 +- application/controllers/dashboard.php | 21 +-- application/controllers/help.php | 12 +- application/controllers/releaseLog.php | 10 +- application/controllers/usersManagement.php | 20 +-- application/engine/Controller.php | 6 +- application/engine/Library.php | 20 --- application/engine/Service.php | 20 +++ application/libraries/DashboardLibrary.php | 43 ------ application/libraries/LoginLibrary.php | 48 ------- .../libraries/UsersManagementLibrary.php | 52 -------- application/models/Data.php | 33 ++--- application/models/User.php | 126 +++++------------- application/services/DashboardService.php | 48 +++++++ .../HelpService.php} | 6 +- application/services/LoginService.php | 48 +++++++ .../services/UsersManagementService.php | 69 ++++++++++ engine/Controller.php | 27 ++-- engine/Library.php | 40 ------ engine/Model.php | 2 +- engine/Service.php | 26 ++++ index.php | 6 - 22 files changed, 317 insertions(+), 380 deletions(-) delete mode 100644 application/engine/Library.php create mode 100644 application/engine/Service.php delete mode 100644 application/libraries/DashboardLibrary.php delete mode 100644 application/libraries/LoginLibrary.php delete mode 100644 application/libraries/UsersManagementLibrary.php create mode 100644 application/services/DashboardService.php rename application/{libraries/HelpLibrary.php => services/HelpService.php} (81%) create mode 100644 application/services/LoginService.php create mode 100644 application/services/UsersManagementService.php delete mode 100644 engine/Library.php create mode 100644 engine/Service.php diff --git a/application/controllers/Login.php b/application/controllers/Login.php index 876f109..5c60293 100644 --- a/application/controllers/Login.php +++ b/application/controllers/Login.php @@ -8,7 +8,7 @@ namespace application\controllers; use application\engine\Controller; -use application\libraries\LoginLibrary; +use application\services\LoginService; use engine\Encrypter; use engine\Form; use engine\Input; @@ -16,17 +16,17 @@ class Login extends Controller { /** - * Defining $_library Library type. - * @var LoginLibrary $_library + * Defining $_service Service type. + * @var LoginService $_service */ - protected $_library; + protected $_service; /** * Login constructor. */ public function __construct() { - parent::__construct(new LoginLibrary); + parent::__construct(new LoginService); } /** @@ -58,13 +58,13 @@ public function run() ); // Logic - $login = $this->_library->login( + $login = $this->_service->login( $form->getInput('name')->getValue(), $form->getInput('password')->getValue() ); // Resolution - if ($login === TRUE) { + if (true === $login) { header('location: ' . _SYSTEM_BASE_URL . 'dashboard'); exit; } else { diff --git a/application/controllers/dashboard.php b/application/controllers/dashboard.php index 9730948..c167e90 100644 --- a/application/controllers/dashboard.php +++ b/application/controllers/dashboard.php @@ -8,19 +8,22 @@ namespace application\controllers; use application\engine\Controller; +use application\services\DashboardService; use engine\Form; use engine\Input; use engine\Session; -use application\libraries\DashboardLibrary; - +/** + * Class Dashboard + * @package application\controllers + */ class Dashboard extends Controller { /** - * Defining $_library Library type. - * @var DashboardLibrary $_library + * Defining $_service Service type. + * @var DashboardService $_service */ - protected $_library; + protected $_service; /** * Dashboard constructor. @@ -28,7 +31,7 @@ class Dashboard extends Controller */ public function __construct() { - parent::__construct(new DashboardLibrary); + parent::__construct(new DashboardService); $logged = Session::get('isUserLoggedIn'); if (FALSE === $logged) { Session::destroy(); @@ -76,7 +79,7 @@ public function ajaxInsert() ->addRule('minLength', 50) ); - $json_response = $this->_library->ajaxInsert($form->getInput('data')->getValue()); + $json_response = $this->_service->ajaxInsert($form->getInput('data')->getValue()); print json_encode($json_response); } @@ -88,7 +91,7 @@ public function ajaxInsert() */ public function getListings() { - $json_response = $this->_library->getListings(); + $json_response = $this->_service->getListings(); print json_encode($json_response); } @@ -105,6 +108,6 @@ public function deleteData() ->addRule('min', 1) ); - $this->_library->deleteData($form->getInput('id')->getValue()); + $this->_service->deleteData($form->getInput('id')->getValue()); } } \ No newline at end of file diff --git a/application/controllers/help.php b/application/controllers/help.php index 1d94345..cbba099 100644 --- a/application/controllers/help.php +++ b/application/controllers/help.php @@ -8,15 +8,15 @@ namespace application\controllers; use application\engine\Controller; -use application\libraries\HelpLibrary; +use application\services\HelpService; class Help extends Controller { /** - * Defining $_library Library type. - * @var HelpLibrary $_library + * Defining $_service Service type. + * @var HelpService $_service */ - protected $_library; + protected $_service; /** * Help constructor. @@ -24,7 +24,7 @@ class Help extends Controller */ public function __construct() { - parent::__construct(new HelpLibrary); + parent::__construct(new HelpService); $this->_view->addLibrary('css', 'views/help/css/help.css'); } @@ -45,7 +45,7 @@ public function index() */ public function helpMeWith($request) { - $helpMessage = $this->_library->helpMeWith($request); + $helpMessage = $this->_service->helpMeWith($request); $this->_view->setParameter('msg', $helpMessage); diff --git a/application/controllers/releaseLog.php b/application/controllers/releaseLog.php index cdf5f60..9a5762f 100644 --- a/application/controllers/releaseLog.php +++ b/application/controllers/releaseLog.php @@ -31,15 +31,19 @@ public function __construct() parent::__construct(); // Setting version under construction - $this->_setDevelopmentVersion('2.22', '04/01/2014', array( + $this->_setDevelopmentVersion('2.23', '04/01/2014', array( + '[Refactor] - Modified libraries concept to Service concepts. A Service is tightly related to a controller. It stores intelligence regarding what to do with validated information, which models request a CRUD, and informs the controller about the message to put into the related view.', + '[Refactor] - Modified Hecnel Framework in order to use the new Model architecture.' + )); + + // Setting Historical Log of releases + $this->_addHistoryLog('2.22', '04/01/2014', array( '[Refactor] - Mayor change in Model architecture. Now all models extend the same Model object, which handles all commands that a Model offers and instantiates and communicates with the Database.', '[Refactor] - Major change in Database architecture. Now it is allowed to have different storing systems.', '[Feature] - New database system: Mock Database. Basically a database that always says yes, always returns data. For development purposes.', '[Feature] - New Model Exception. When caught, its code indicates what happened. ', '[Feature] - Connected Test3 to models. It allows CRUD to Dog, plus model exceptions catching and error display.' )); - - // Setting Historical Log of releases $this->_addHistoryLog('2.21', '03/01/2014', array( 'HAPPY NEW YEAR!!', '[Feature] - Created basic skeleton for the new Model architecture.', diff --git a/application/controllers/usersManagement.php b/application/controllers/usersManagement.php index afbff85..12230cf 100644 --- a/application/controllers/usersManagement.php +++ b/application/controllers/usersManagement.php @@ -8,7 +8,7 @@ namespace application\controllers; use application\engine\Controller; -use application\libraries\UsersManagementLibrary; +use application\services\UsersManagementService; use engine\Input; use engine\Session; use engine\Form; @@ -16,15 +16,15 @@ class usersManagement extends Controller { /** - * Defining $_library Library type. - * @var UsersManagementLibrary $_library + * Defining $_service Services type. + * @var UsersManagementService $_service */ - protected $_library; + protected $_service; /** Validates user access to this controller */ public function __construct() { - parent::__construct(new UsersManagementLibrary); + parent::__construct(new UsersManagementService); $logged = Session::get('isUserLoggedIn'); $userRole = Session::get('userRole'); @@ -40,7 +40,7 @@ public function __construct() */ public function index() { - $this->_view->setParameter('usersList', $this->_library->getUsersList()); + $this->_view->setParameter('usersList', $this->_service->getUsersList()); $this->_view->addChunk('usersManagement/index'); } @@ -50,7 +50,7 @@ public function index() */ public function openUserEdition($userId) { - $userData = $this->_library->getUser($userId); + $userData = $this->_service->getUser($userId); if ($userData === FALSE) { $this->_view->setParameter('error', 'The user you are trying to edit does not exist.'); @@ -88,7 +88,7 @@ public function createUser() )) ); - $this->_library->createUser( + $this->_service->createUser( $form->getInput('userName')->getValue(), $form->getInput('password')->getValue(), $form->getInput('userRole')->getValue()); @@ -125,7 +125,7 @@ public function editUser() )) ); - $this->_library->editUser( + $this->_service->editUser( $form->getInput('userId')->getValue(), $form->getInput('userName')->getValue(), $form->getInput('password')->getValue(), @@ -141,7 +141,7 @@ public function editUser() */ public function deleteUser($userId) { - $this->_library->deleteUser($userId); + $this->_service->deleteUser($userId); header('location: ' . _SYSTEM_BASE_URL . 'usersManagement'); exit; diff --git a/application/engine/Controller.php b/application/engine/Controller.php index cf2317e..6454710 100644 --- a/application/engine/Controller.php +++ b/application/engine/Controller.php @@ -17,11 +17,11 @@ class Controller extends engineController /** * Controller constructor. * - * @param Library $library in which this Controller can search for the Model + * @param Service $service in which this Controller can search for the Model */ - public function __construct(Library $library = NULL) + public function __construct(Service $service = NULL) { - parent::__construct($library); + parent::__construct($service); } /** diff --git a/application/engine/Library.php b/application/engine/Library.php deleted file mode 100644 index 04411fe..0000000 --- a/application/engine/Library.php +++ /dev/null @@ -1,20 +0,0 @@ -_model->insert($data); - return array('id' => $newDataId, 'data' => $data); - } - - public function getListings() - { - $allData = $this->_model->selectAll(); - return $allData; - } - - public function deleteData($dataId) - { - $this->_model->delete($dataId); - } -} \ No newline at end of file diff --git a/application/libraries/LoginLibrary.php b/application/libraries/LoginLibrary.php deleted file mode 100644 index ed2ef2b..0000000 --- a/application/libraries/LoginLibrary.php +++ /dev/null @@ -1,48 +0,0 @@ -_model->selectUserForLogin($name, $password); - - if ($result !== FALSE) { - Session::set('isUserLoggedIn', true); - Session::set('userName', $result['name']); - Session::set('userRole', $result['role']); - return TRUE; - } else { - return FALSE; - } - } -} \ No newline at end of file diff --git a/application/libraries/UsersManagementLibrary.php b/application/libraries/UsersManagementLibrary.php deleted file mode 100644 index 0f40ba1..0000000 --- a/application/libraries/UsersManagementLibrary.php +++ /dev/null @@ -1,52 +0,0 @@ -_model->selectById($userId); - } - - public function getUsersList() - { - return $this->_model->selectAll(); - } - - public function createUser($userName, $password, $userRole) - { - $this->_model->insert($userName, $password, $userRole); - } - - public function editUser($userId, $userName, $password, $userRole) - { - $this->_model->update($userId, $userName, $password, $userRole); - } - - public function deleteUser($userId) - { - $this->_model->delete($userId); - } - -} \ No newline at end of file diff --git a/application/models/Data.php b/application/models/Data.php index dfa97b6..89b8704 100644 --- a/application/models/Data.php +++ b/application/models/Data.php @@ -10,31 +10,24 @@ use application\engine\Model; +/** + * Class Data + * @package application\models + */ class Data extends Model { - public function __construct() - { - parent::__construct(); - } - - public function insert($data) - { - $id = $this->db->insert('data', array('data' => $data)); - - return $id; - } + /** + * Data + * @var string $data + */ + public $data; - public function delete($dataId) + public function __construct() { - $conditionsArray = array( - 'id' => $dataId - ); + $this->setModelName('data'); - $this->db->delete('data', $conditionsArray); - } + parent::__construct(); - public function selectAll() - { - return $this->db->select('data', array('id', 'data')); + $this->addField('data'); } } \ No newline at end of file diff --git a/application/models/User.php b/application/models/User.php index 7aeeedb..e9c1d78 100644 --- a/application/models/User.php +++ b/application/models/User.php @@ -14,117 +14,51 @@ class User extends Model { /** - * User Model constructor. + * User name. + * @var string $name */ - public function __construct() - { - parent::__construct(); - } + public $name; /** - * Receives a User name and its password. Verifies if these parameters are right and, if so, returns its data. - * - * @param string $userName - * @param string $password Unencrypted password. - * @return mixed array/bool User data if found, false if not. + * User password. + * It is protected as it is not allowed direct definition. Password has to be set through its setter, which applies Encryption. + * @var string $password */ - public function selectUserForLogin($userName, $password) - { - $fields = array( - 'id', - 'name', - 'password', - 'role' - ); - - $conditions = array( - 'name' => $userName - ); - - $result = $this->db->select('user', $fields, $conditions); - - if (count($result) != 0) { - if (Encrypter::verify($password, $result[0]['password']) === TRUE) { - return $result[0]; - } - } - return FALSE; - } - + protected $password; + /** - * Collects the data from a specific user. - * @param int $userId User Id. - * @return array User Data + * User role. + * @var string $role */ - public function selectById($userId) - { - $fields = array( - 'id', - 'name', - 'password', - 'role' - ); - - $conditions = array( - 'id' => $userId - ); - - $result = $this->db->select('user', $fields, $conditions); - - if (count($result) > 0) { - return $result[0]; - } else { - return FALSE; - } - } + public $role; - public function selectAll() + public function __construct() { - $result = $this->db->select('user'); + $this->setModelName('user'); + + parent::__construct(); - return $result; + $this->addField('name'); + $this->addField('password'); + $this->addField('role'); } /** - * Creates user with the specified parameters. - * @param string $userName - * @param string $password Password will be encrypted. - * @param string $userRole (owner, admin or basic) + * @param string $password */ - public function insert($userName, $password, $userRole) + public function setPassword($password) { - $valuesArray = array( - 'name' => $userName, - 'password' => Encrypter::encrypt($password), - 'role' => $userRole - ); - - $this->db->insert('user', $valuesArray); + $this->password = Encrypter::encrypt($password); } - - - public function update($userId, $userName, $password, $userRole) - { - $setArray = array( - 'name' => $userName, - 'password' => Encrypter::encrypt($password), - 'role' => $userRole - ); - - $conditionsArray = array( - 'id' => $userId - ); - - $this->db->update('user', $setArray, $conditionsArray); - } - - - public function delete($userId) + + /** + * Verifies that the passed password matches with User's. + * + * @param string $password + * @return bool Whether this User name and password matches + */ + public function verify($password) { - $conditionsArray = array( - 'id' => $userId - ); - - $this->db->delete('user', $conditionsArray); + return Encrypter::verify($password, $this->password); } } \ No newline at end of file diff --git a/application/services/DashboardService.php b/application/services/DashboardService.php new file mode 100644 index 0000000..02d1c3e --- /dev/null +++ b/application/services/DashboardService.php @@ -0,0 +1,48 @@ +data = $data; + $dataModel->save(); + + return $dataModel->toArray(); + } + + /** + * @return array + * @todo SELECT ALL! + */ + public function getListings() + { + $dataModel = new Data(); + $dataModel->find(); + + return array($dataModel->toArray()); + } + + public function deleteData($dataId) + { + $dataModel = new Data(); + $dataModel->find(array('id' => $dataId)); + $dataModel->delete(); + } +} \ No newline at end of file diff --git a/application/libraries/HelpLibrary.php b/application/services/HelpService.php similarity index 81% rename from application/libraries/HelpLibrary.php rename to application/services/HelpService.php index 23176fe..4bc304c 100644 --- a/application/libraries/HelpLibrary.php +++ b/application/services/HelpService.php @@ -6,11 +6,11 @@ * Date: 11/07/13 16:30 */ -namespace application\libraries; +namespace application\services; -use application\engine\Library; +use application\engine\Service; -class HelpLibrary extends Library +class HelpService extends Service { public function __construct() { diff --git a/application/services/LoginService.php b/application/services/LoginService.php new file mode 100644 index 0000000..134c777 --- /dev/null +++ b/application/services/LoginService.php @@ -0,0 +1,48 @@ +find(array('name' => $name)); + + if ($user->verify($password)) { + Session::set('isUserLoggedIn', true); + Session::set('userName', $user->name); + Session::set('userRole', $user->role); + return true; + } + } catch (ModelException $mEx) { + // Catching this exception just to avoid having it jumping up. Other Exceptions need to break execution, as are unexpected. + } + + return false; + } +} \ No newline at end of file diff --git a/application/services/UsersManagementService.php b/application/services/UsersManagementService.php new file mode 100644 index 0000000..641700e --- /dev/null +++ b/application/services/UsersManagementService.php @@ -0,0 +1,69 @@ +find(array('id'=>$userId)); + + return $user->toArray(); + } + + /** + * @todo Select All!! + * @return array + */ + public function getUsersList() + { + $user = new User(); + $user->find(); + + return array($user->toArray()); + } + + public function createUser($userName, $password, $userRole) + { + $user = new User(); + $user->name = $userName; + $user->setPassword($password); + $user->role = $userRole; + $user->save(); + } + + public function editUser($userId, $userName, $password, $userRole) + { + $user = new User(); + $user->find(array('id'=>$userId)); + + $user->name = $userName; + $user->setPassword($password); + $user->role = $userRole; + + $user->save(); + } + + public function deleteUser($userId) + { + $user = new User(); + $user->find(array('id'=>$userId)); + $user->delete(); + } + +} \ No newline at end of file diff --git a/engine/Controller.php b/engine/Controller.php index 43afd42..947af06 100644 --- a/engine/Controller.php +++ b/engine/Controller.php @@ -5,7 +5,7 @@ * Description: * The Controller class of the Engine is the master of the Controllers, extended by the Controller of the application engine and, that one, extended by all the controllers that the Application needs. * - * The Controllers are design to manage Users requests, validating their data and to decide which Libraries use to build the data that needs to be shown in the Views that the User request has assigned. + * The Controllers are design to manage Users requests, validating their data and to decide which Services use to build the data that needs to be shown in the Views that the User request has assigned. * * Although the Controller is "blinded" of the logic required for building the data, the final data to be shown in the View travels through this class. Because of this, the Controller * must understand what the data looks like - that means, the Controller does not pass the final data to the view blindly; the Controller gets the final data and extracts from it the pieces that will be passed to the View. @@ -14,14 +14,15 @@ * Because of this the Controllers have access to the methods setAutoRender and render (for more info read the documentation in these methods comments), which allows the logic not to follow the default behavior of the system, which is rendering a web page after the Controller ends processing the request. * So when an asynchronous call hits a Controller, this will have to disable the auto rendering, in order to only show the information that the asynchronous request requires. * - * Controllers have the duty to manage the Exceptions that the Libraries throw; Controllers must know what to do when an error arises, even if this means calling another library to manage the error final data. + * Controllers have the duty to manage the Exceptions that the Services throw; Controllers must know what to do when an error arises, even if this means calling another service to manage the error final data. * @date: 11/06/13 12:00 */ namespace engine; -use application\engine\Library; +use application\engine\Service; use application\engine\View; + use engine\drivers\Exceptions\ViewException; /** @@ -39,7 +40,7 @@ class Controller /** * @var null */ - protected $_library = NULL; + protected $_service = NULL; /*************************/ /* Controller Settings **/ @@ -51,12 +52,12 @@ class Controller * Initializes the User Session. * Initializes the View and the Model. * - * @param Library $library in which this Controller can search for the Model + * @param Service $service */ - public function __construct(Library $library = NULL) + public function __construct(Service $service = NULL) { $this->_setView(); - $this->_setLibrary($library); + $this->_setService($service); } protected function _setView() @@ -65,15 +66,15 @@ protected function _setView() } /** - * Auto-loading of the library related to this controller. - * Checks if there is a library related to this controller and, if so, instantiates it. + * Auto-loading of the service related to this controller. + * Checks if there is a service related to this controller and, if so, instantiates it. * - * @param Library $library in which this controller can search for the library + * @param Service $service in which this controller can search for the service */ - protected function _setLibrary(Library $library = NULL) + protected function _setService(Service $service = NULL) { - if (!is_null($library)) { - $this->_library = $library; + if (!is_null($service)) { + $this->_service = $service; } } diff --git a/engine/Library.php b/engine/Library.php deleted file mode 100644 index c173bb6..0000000 --- a/engine/Library.php +++ /dev/null @@ -1,40 +0,0 @@ -_model = $model; - } -} \ No newline at end of file diff --git a/engine/Model.php b/engine/Model.php index 52504d1..83b443f 100644 --- a/engine/Model.php +++ b/engine/Model.php @@ -4,7 +4,7 @@ * User: Hector Ordonez * Description: * The Model class of the Engine is the master of the Models, extended by the Model of the application engine and, that one, extended by all the models that the Application needs. - * A Model is the logical representation of a Database entity; it easies the Libraries task of requesting data to the Database. + * A Model is the logical representation of a Database entity; it easies the Services task of requesting data to the Database. * @date: 11/06/13 12:30 */ diff --git a/engine/Service.php b/engine/Service.php new file mode 100644 index 0000000..a3a0b8f --- /dev/null +++ b/engine/Service.php @@ -0,0 +1,26 @@ +