|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service; |
|
4
|
|
|
|
|
5
|
|
|
use App\Controller\Base; |
|
6
|
|
|
use App\Repository\UsersRepository; |
|
7
|
|
|
use Respect\Validation\Validator as v; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Users Service. |
|
11
|
|
|
*/ |
|
12
|
|
|
class UsersService extends Base |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Constructor of the class. |
|
16
|
|
|
* |
|
17
|
|
|
* @param object $database |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(\PDO $database) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->database = $database; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Check if the user exists. |
|
26
|
|
|
* |
|
27
|
|
|
* @param int $userId |
|
28
|
|
|
* @return object $user |
|
29
|
|
|
* @throws \Exception |
|
30
|
|
|
*/ |
|
31
|
|
View Code Duplication |
public function checkUser($userId) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$repo = new UsersRepository; |
|
34
|
|
|
$stmt = $this->database->prepare($repo->getUserQuery()); |
|
35
|
|
|
$stmt->bindParam('id', $userId); |
|
36
|
|
|
$stmt->execute(); |
|
37
|
|
|
$user = $stmt->fetchObject(); |
|
38
|
|
|
if (!$user) { |
|
39
|
|
|
throw new \Exception(self::USER_NOT_FOUND, 404); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $user; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get all users. |
|
47
|
|
|
* |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getUsers() |
|
51
|
|
|
{ |
|
52
|
|
|
$repository = new UsersRepository; |
|
53
|
|
|
$query = $repository->getUsersQuery(); |
|
54
|
|
|
$statement = $this->database->prepare($query); |
|
55
|
|
|
$statement->execute(); |
|
56
|
|
|
|
|
57
|
|
|
return $statement->fetchAll(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get one user by id. |
|
62
|
|
|
* |
|
63
|
|
|
* @param int $userId |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getUser($userId) |
|
67
|
|
|
{ |
|
68
|
|
|
$user = $this->checkUser($userId); |
|
69
|
|
|
|
|
70
|
|
|
return $user; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Search users by name. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $str |
|
77
|
|
|
* @return array |
|
78
|
|
|
* @throws \Exception |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function searchUsers($str) |
|
81
|
|
|
{ |
|
82
|
|
|
$repo = new UsersRepository; |
|
83
|
|
|
$stmt = $this->database->prepare($repo->searchUsersQuery()); |
|
84
|
|
|
$name = '%' . $str . '%'; |
|
85
|
|
|
$stmt->bindParam('name', $name); |
|
86
|
|
|
$stmt->execute(); |
|
87
|
|
|
$users = $stmt->fetchAll(); |
|
88
|
|
|
|
|
89
|
|
|
if (!$users) { |
|
90
|
|
|
throw new \Exception(self::USER_NAME_NOT_FOUND, 404); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $users; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function validateInput($input) |
|
97
|
|
|
{ |
|
98
|
|
|
if (!isset($input['name'])) { |
|
99
|
|
|
throw new \Exception(self::USER_NAME_REQUIRED, 400); |
|
100
|
|
|
} |
|
101
|
|
|
$name = $input['name']; |
|
102
|
|
|
$usernameValidator = v::alnum()->length(1, 100); |
|
103
|
|
|
if (!$usernameValidator->validate($name)) { |
|
104
|
|
|
throw new \Exception(self::USER_NAME_INVALID, 400); |
|
105
|
|
|
} |
|
106
|
|
|
$email = null; |
|
107
|
|
|
if (isset($input['email'])) { |
|
108
|
|
|
$email = $this->validateEmail($input['email']); |
|
109
|
|
|
} |
|
110
|
|
|
$data = [ |
|
111
|
|
|
'name' => $name, |
|
112
|
|
|
'email' => $email, |
|
113
|
|
|
]; |
|
114
|
|
|
return $data; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Create a user. |
|
119
|
|
|
* |
|
120
|
|
|
* @param array $input |
|
121
|
|
|
* @return array |
|
122
|
|
|
* @throws \Exception |
|
123
|
|
|
*/ |
|
124
|
|
|
public function createUser($input) |
|
125
|
|
|
{ |
|
126
|
|
|
$data = $this->validateInput($input); |
|
127
|
|
|
$name = $data['name']; |
|
128
|
|
|
$email = $data['email']; |
|
129
|
|
|
$repository = new UsersRepository; |
|
130
|
|
|
$query = $repository->createUserQuery(); |
|
131
|
|
|
$statement = $this->database->prepare($query); |
|
132
|
|
|
$statement->bindParam('name', $name); |
|
133
|
|
|
$statement->bindParam('email', $email); |
|
134
|
|
|
$statement->execute(); |
|
135
|
|
|
$user = $this->checkUser($this->database->lastInsertId()); |
|
136
|
|
|
|
|
137
|
|
|
return $user; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Update a user. |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $input |
|
144
|
|
|
* @param int $userId |
|
145
|
|
|
* @return array |
|
146
|
|
|
* @throws \Exception |
|
147
|
|
|
*/ |
|
148
|
|
View Code Duplication |
public function updateUser($input, $userId) |
|
149
|
|
|
{ |
|
150
|
|
|
$user = $this->checkUser($userId); |
|
151
|
|
|
if (empty($input['name']) && empty($input['email'])) { |
|
152
|
|
|
throw new \Exception(self::USER_INFO_REQUIRED, 400); |
|
153
|
|
|
} |
|
154
|
|
|
$username = isset($input['name']) ? $input['name'] : $user->name; |
|
155
|
|
|
$email = $user->email; |
|
156
|
|
|
if (isset($input['email'])) { |
|
157
|
|
|
$email = $this->validateEmail($input['email']); |
|
158
|
|
|
} |
|
159
|
|
|
$repository = new UsersRepository; |
|
160
|
|
|
$query = $repository->updateUserQuery(); |
|
161
|
|
|
$statement = $this->database->prepare($query); |
|
162
|
|
|
$statement->bindParam('id', $userId); |
|
163
|
|
|
$statement->bindParam('name', $username); |
|
164
|
|
|
$statement->bindParam('email', $email); |
|
165
|
|
|
$statement->execute(); |
|
166
|
|
|
|
|
167
|
|
|
return $this->checkUser($userId); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Delete a user. |
|
172
|
|
|
* |
|
173
|
|
|
* @param int $userId |
|
174
|
|
|
* @return array |
|
175
|
|
|
*/ |
|
176
|
|
|
public function deleteUser($userId) |
|
177
|
|
|
{ |
|
178
|
|
|
$this->checkUser($userId); |
|
179
|
|
|
$repository = new UsersRepository; |
|
180
|
|
|
$query = $repository->deleteUserQuery(); |
|
181
|
|
|
$statement = $this->database->prepare($query); |
|
182
|
|
|
$statement->bindParam('id', $userId); |
|
183
|
|
|
$statement->execute(); |
|
184
|
|
|
|
|
185
|
|
|
return self::USER_DELETED; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.