1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DanielPieper\MergeReminder\Service; |
4
|
|
|
|
5
|
|
|
use DanielPieper\MergeReminder\Exception\UserNotFoundException; |
6
|
|
|
use DanielPieper\MergeReminder\ValueObject\User; |
7
|
|
|
|
8
|
|
|
class UserService |
9
|
|
|
{ |
10
|
|
|
/** @var \Gitlab\Client */ |
11
|
|
|
private $gitlabClient; |
12
|
|
|
|
13
|
4 |
|
public function __construct(\Gitlab\Client $gitlabClient) |
14
|
|
|
{ |
15
|
4 |
|
$this->gitlabClient = $gitlabClient; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param int $id |
20
|
|
|
* @return User|null |
21
|
|
|
*/ |
22
|
1 |
|
public function find(int $id): ?User |
23
|
|
|
{ |
24
|
1 |
|
$user = $this->gitlabClient->users()->show($id); |
25
|
1 |
|
if (!is_array($user)) { |
26
|
1 |
|
return null; |
27
|
|
|
} |
28
|
|
|
return User::fromArray($user); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $username |
33
|
|
|
* @return User |
34
|
|
|
*/ |
35
|
1 |
|
public function findByName(string $username): ?User |
36
|
|
|
{ |
37
|
1 |
|
$users = $this->all($username); |
38
|
1 |
|
if (count($users) == 0) { |
39
|
1 |
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return array_shift($users); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return User |
47
|
|
|
* @throws UserNotFoundException |
48
|
|
|
*/ |
49
|
1 |
|
public function getAuthenticated(): User |
50
|
|
|
{ |
51
|
1 |
|
$user = $this->gitlabClient->users()->user(); |
52
|
1 |
|
if (!is_array($user)) { |
53
|
1 |
|
throw new UserNotFoundException(); |
54
|
|
|
} |
55
|
|
|
return User::fromArray($user); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param int $id |
60
|
|
|
* @return User |
61
|
|
|
* @throws UserNotFoundException |
62
|
|
|
*/ |
63
|
1 |
|
public function get(int $id): User |
64
|
|
|
{ |
65
|
1 |
|
$user = $this->find($id); |
66
|
1 |
|
if (!$user) { |
67
|
1 |
|
throw new UserNotFoundException(); |
68
|
|
|
} |
69
|
|
|
return $user; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $username |
74
|
|
|
* @return User |
75
|
|
|
* @throws UserNotFoundException |
76
|
|
|
*/ |
77
|
1 |
|
public function getByName(string $username): User |
78
|
|
|
{ |
79
|
1 |
|
$user = $this->findByName($username); |
80
|
1 |
|
if (!$user) { |
81
|
1 |
|
throw new UserNotFoundException(); |
82
|
|
|
} |
83
|
|
|
return $user; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|null $username |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
2 |
|
public function all(string $username = null): array |
91
|
|
|
{ |
92
|
2 |
|
$parameters = ['active' => true]; |
93
|
2 |
|
if ($username) { |
94
|
1 |
|
$parameters['username'] = $username; |
95
|
|
|
} |
96
|
2 |
|
$users = $this->gitlabClient->users()->all($parameters); |
97
|
2 |
|
if (!is_array($users)) { |
98
|
2 |
|
return []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return array_map(function ($user) { |
102
|
|
|
return User::fromArray($user); |
103
|
|
|
}, $users); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|