1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DanielPieper\MergeReminder\Service; |
4
|
|
|
|
5
|
|
|
use DanielPieper\MergeReminder\Exception\ProjectNotFoundException; |
6
|
|
|
use DanielPieper\MergeReminder\ValueObject\Project; |
7
|
|
|
use DanielPieper\MergeReminder\ValueObject\User; |
8
|
|
|
use Gitlab\Client; |
9
|
|
|
use Gitlab\ResultPager; |
10
|
|
|
|
11
|
|
|
class ProjectService |
12
|
|
|
{ |
13
|
|
|
/** @var Client */ |
14
|
|
|
private $gitlabClient; |
15
|
|
|
|
16
|
|
|
/** @var ResultPager */ |
17
|
|
|
private $resultPager; |
18
|
|
|
|
19
|
|
|
public function __construct(Client $gitlabClient, ResultPager $resultPager) |
20
|
|
|
{ |
21
|
|
|
$this->gitlabClient = $gitlabClient; |
22
|
|
|
$this->resultPager = $resultPager; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param int $id |
27
|
|
|
* @return Project|null |
28
|
|
|
*/ |
29
|
|
|
public function find(int $id): ?Project |
30
|
|
|
{ |
31
|
|
|
$project = $this->gitlabClient->projects()->show($id); |
32
|
|
|
if (!is_array($project)) { |
33
|
|
|
return null; |
34
|
|
|
} |
35
|
|
|
return Project::fromArray($project); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param int $id |
40
|
|
|
* @return Project |
41
|
|
|
* @throws ProjectNotFoundException |
42
|
|
|
*/ |
43
|
|
|
public function get(int $id): Project |
44
|
|
|
{ |
45
|
|
|
$project = $this->find($id); |
46
|
|
|
if (!$project) { |
47
|
|
|
throw new ProjectNotFoundException(); |
48
|
|
|
} |
49
|
|
|
return $project; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function allByUser(User $user): array |
53
|
|
|
{ |
54
|
|
|
$projects = $this->gitlabClient->users()->usersProjects($user->getId()); |
55
|
|
|
if (!is_array($projects)) { |
56
|
|
|
return []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return array_map(function ($project) { |
60
|
|
|
return Project::fromArray($project); |
61
|
|
|
}, $projects); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return Project[] |
66
|
|
|
*/ |
67
|
|
|
public function all(): array |
68
|
|
|
{ |
69
|
|
|
$projects = $this->resultPager->fetchAll( |
70
|
|
|
$this->gitlabClient->projects(), |
71
|
|
|
'all', |
72
|
|
|
[ |
73
|
|
|
[ |
74
|
|
|
'page' => 1, |
75
|
|
|
'per_page' => 100, |
76
|
|
|
'archived' => false, |
77
|
|
|
'with_merge_requests_enabled' => true, |
78
|
|
|
] |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
return array_map(function ($project) { |
83
|
|
|
return Project::fromArray($project); |
84
|
|
|
}, $projects); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|