Moving user controller to using a service class
This commit is contained in:
parent
bfc7a58195
commit
06ccdd1937
|
@ -15,6 +15,7 @@ use b8\Exception\HttpException\NotFoundException;
|
|||
use b8\Form;
|
||||
use PHPCI\Controller;
|
||||
use PHPCI\Model\User;
|
||||
use PHPCI\Service\UserService;
|
||||
|
||||
/**
|
||||
* User Controller - Allows an administrator to view, add, edit and delete users.
|
||||
|
@ -29,9 +30,15 @@ class UserController extends Controller
|
|||
*/
|
||||
protected $userStore;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Service\UserService
|
||||
*/
|
||||
protected $userService;
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->userStore = b8\Store\Factory::getStore('User');
|
||||
$this->userService = new UserService($this->userStore);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -53,16 +60,11 @@ class UserController extends Controller
|
|||
$values = $user->getDataArray();
|
||||
|
||||
if ($this->request->getMethod() == 'POST') {
|
||||
$values = $this->getParams();
|
||||
$name = $this->getParam('name', null);
|
||||
$email = $this->getParam('email', null);
|
||||
$password = $this->getParam('password', null);
|
||||
|
||||
if (!empty($values['password'])) {
|
||||
$values['hash'] = password_hash($values['password'], PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
$this->view->updated = true;
|
||||
|
||||
$user->setValues($values);
|
||||
$_SESSION['user'] = $this->userStore->save($user);
|
||||
$_SESSION['user'] = $this->userService->updateUser($name, $email, $password);
|
||||
}
|
||||
|
||||
$form = new Form();
|
||||
|
@ -132,13 +134,13 @@ class UserController extends Controller
|
|||
return $view->render();
|
||||
}
|
||||
|
||||
$values = $form->getValues();
|
||||
$values['hash'] = password_hash($values['password'], PASSWORD_DEFAULT);
|
||||
|
||||
$user = new User();
|
||||
$user->setValues($values);
|
||||
$name = $this->getParam('name', null);
|
||||
$email = $this->getParam('email', null);
|
||||
$password = $this->getParam('password', null);
|
||||
$isAdmin = (int)$this->getParam('is_admin', 0);
|
||||
|
||||
$user = $this->userStore->save($user);
|
||||
$this->userService->createUser($name, $email, $password, $isAdmin);
|
||||
|
||||
header('Location: '.PHPCI_URL.'user');
|
||||
die;
|
||||
|
@ -172,18 +174,12 @@ class UserController extends Controller
|
|||
return $view->render();
|
||||
}
|
||||
|
||||
if (!empty($values['password'])) {
|
||||
$values['hash'] = password_hash($values['password'], PASSWORD_DEFAULT);
|
||||
}
|
||||
$name = $this->getParam('name', null);
|
||||
$email = $this->getParam('email', null);
|
||||
$password = $this->getParam('password', null);
|
||||
$isAdmin = (int)$this->getParam('is_admin', 0);
|
||||
|
||||
$user->setValues($values);
|
||||
|
||||
$isAdmin = $this->getParam('is_admin');
|
||||
if (empty($isAdmin)) {
|
||||
$user->setIsAdmin(0);
|
||||
}
|
||||
|
||||
$this->userStore->save($user);
|
||||
$this->userService->updateUser($user, $name, $email, $password, $isAdmin);
|
||||
|
||||
header('Location: '.PHPCI_URL.'user');
|
||||
die;
|
||||
|
@ -258,7 +254,7 @@ class UserController extends Controller
|
|||
throw new NotFoundException('User with ID: ' . $userId . ' does not exist.');
|
||||
}
|
||||
|
||||
$this->userStore->delete($user);
|
||||
$this->userService->delete($user);
|
||||
|
||||
header('Location: '.PHPCI_URL.'user');
|
||||
die;
|
||||
|
|
61
PHPCI/Service/UserService.php
Normal file
61
PHPCI/Service/UserService.php
Normal file
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCI\Service;
|
||||
|
||||
use PHPCI\Model\User;
|
||||
use PHPCI\Store\UserStore;
|
||||
|
||||
class UserService
|
||||
{
|
||||
/**
|
||||
* @var \PHPCI\Store\UserStore
|
||||
*/
|
||||
protected $store;
|
||||
|
||||
/**
|
||||
* @param UserStore $store
|
||||
*/
|
||||
public function __construct(UserStore $store)
|
||||
{
|
||||
$this->store = $store;
|
||||
}
|
||||
|
||||
public function createUser($name, $emailAddress, $password, $isAdmin = false)
|
||||
{
|
||||
$user = new User();
|
||||
$user->setName($name);
|
||||
$user->setEmail($emailAddress);
|
||||
$user->setHash(password_hash($password, PASSWORD_DEFAULT));
|
||||
$user->setIsAdmin(($isAdmin ? 1 : 0));
|
||||
|
||||
return $this->store->save($user);
|
||||
}
|
||||
|
||||
public function updateUser(User $user, $name, $emailAddress, $password = null, $isAdmin = null)
|
||||
{
|
||||
$user->setName($name);
|
||||
$user->setEmail($emailAddress);
|
||||
|
||||
if (!empty($password)) {
|
||||
$user->setHash(password_hash($password, PASSWORD_DEFAULT));
|
||||
}
|
||||
|
||||
if (!is_null($isAdmin)) {
|
||||
$user->setIsAdmin(($isAdmin ? 1 : 0));
|
||||
}
|
||||
|
||||
return $this->store->save($user);
|
||||
}
|
||||
|
||||
public function deleteUser(User $user)
|
||||
{
|
||||
return $this->store->delete($user);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue