Moving user controller to using a service class
This commit is contained in:
parent
bfc7a58195
commit
06ccdd1937
2 changed files with 83 additions and 26 deletions
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…
Add table
Add a link
Reference in a new issue