Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
UserService | |
100.00% |
9 / 9 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
getUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUsers | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createUser | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
updateUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deleteUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Vultr\VultrPhp\Services\Users; |
6 | |
7 | use Vultr\VultrPhp\Services\VultrService; |
8 | use Vultr\VultrPhp\Util\ListOptions; |
9 | |
10 | /** |
11 | * User service handler, for all users endpoints. |
12 | * |
13 | * @see https://www.vultr.com/api/#tag/users |
14 | */ |
15 | class UserService extends VultrService |
16 | { |
17 | /** |
18 | * @param $user_id - string - UUID of the user |
19 | * @throws UserException |
20 | * @throws VultrException |
21 | * @return User |
22 | */ |
23 | public function getUser(string $user_id) : User |
24 | { |
25 | return $this->getObject('users/'.$user_id, new User()); |
26 | } |
27 | |
28 | /** |
29 | * @param $options - ListOptions - Interact via reference. |
30 | * @throws UserException |
31 | * @return User[] |
32 | */ |
33 | public function getUsers(?ListOptions &$options = null) : array |
34 | { |
35 | return $this->getListObjects('users', new User(), $options); |
36 | } |
37 | |
38 | /** |
39 | * @param $password - string |
40 | * @param $user - User Model with any properties defined will be used in the response. |
41 | * @throws UserException |
42 | * @throws VultrException |
43 | * @return User |
44 | */ |
45 | public function createUser(string $password, User $user) : User |
46 | { |
47 | $params = $user->toArray(); |
48 | $params['password'] = $password; |
49 | foreach ($params as $attr => $param) |
50 | { |
51 | if (empty($param)) unset($params[$attr]); |
52 | } |
53 | |
54 | return $this->createObject('users', new User(), $params); |
55 | } |
56 | |
57 | /** |
58 | * @param $user - User |
59 | * @throws UserException |
60 | * @return void |
61 | */ |
62 | public function updateUser(User $user) : void |
63 | { |
64 | $this->patchObject('users/'.$user->getId(), $user); |
65 | } |
66 | |
67 | /** |
68 | * @param $user_id - string |
69 | * @throws UserException |
70 | * @return void |
71 | */ |
72 | public function deleteUser(string $user_id) : void |
73 | { |
74 | $this->deleteObject('users/'.$user_id, new User()); |
75 | } |
76 | } |
77 |