-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathUserService.php
More file actions
87 lines (72 loc) · 2.14 KB
/
Copy pathUserService.php
File metadata and controls
87 lines (72 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
declare(strict_types=1);
namespace PHPCensor\Service;
use PHPCensor\Model\User;
use PHPCensor\Store\UserStore;
use PHPCensor\StoreRegistry;
/**
* The user service handles the creation, modification and deletion of users.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dan Cryer <dan@block8.co.uk>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class UserService
{
private UserStore $store;
private StoreRegistry $storeRegistry;
public function __construct(
StoreRegistry $storeRegistry,
UserStore $store
) {
$this->storeRegistry = $storeRegistry;
$this->store = $store;
}
public function createUser(string $name, string $email, string $providerKey, array $providerData, string $password, bool $isAdmin = false): ?User
{
$user = new User($this->storeRegistry);
$user->setName($name);
$user->setEmail($email);
$user->setHash(\password_hash($password, PASSWORD_DEFAULT));
$user->setProviderKey($providerKey);
$user->setProviderData($providerData);
$user->setIsAdmin($isAdmin);
return $this->store->save($user);
}
/**
* Update a user.
*
* @param string $password
* @param bool $isAdmin
* @param string $language
* @param int $perPage
*
* @return User
*/
public function updateUser(User $user, string $name, string $emailAddress, ?string $password = null, ?bool $isAdmin = null, ?string $language = null, ?int $perPage = null): ?User
{
$user->setName($name);
$user->setEmail($emailAddress);
if (!empty($password)) {
$user->setHash(\password_hash($password, PASSWORD_DEFAULT));
}
if (!\is_null($isAdmin)) {
$user->setIsAdmin($isAdmin);
}
$user->setLanguage($language);
$user->setPerPage($perPage);
return $this->store->save($user);
}
/**
* Delete a user.
*/
public function deleteUser(User $user): bool
{
if (!$user->getId()) {
return false;
}
return $this->store->delete($user);
}
}