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