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