1
|
|
|
<?php |
2
|
|
|
namespace DevBoardLib\GithubApiFacade\Repo; |
3
|
|
|
|
4
|
|
|
use DevBoardLib\GithubCore\Repo\GithubRepo; |
5
|
|
|
use Github\Client; |
6
|
|
|
use Github\ResultPager; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PaginatedKnpLabsRepoFacade. |
10
|
|
|
*/ |
11
|
|
|
class PaginatedKnpLabsRepoFacade implements RepoFacade |
12
|
|
|
{ |
13
|
|
|
private $client; |
14
|
|
|
private $githubRepo; |
15
|
|
|
|
16
|
|
|
private $paginator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* PaginatedKnpLabsRepoFacade constructor. |
20
|
|
|
* |
21
|
|
|
* @param $client |
22
|
|
|
* @param $githubRepo |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Client $client, GithubRepo $githubRepo) |
25
|
|
|
{ |
26
|
|
|
$this->client = $client; |
27
|
|
|
$this->githubRepo = $githubRepo; |
28
|
|
|
|
29
|
|
|
$this->paginator = new ResultPager($this->client); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array|mixed |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function fetchAllMilestones() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$parameters = [ |
38
|
|
|
$this->githubRepo->getOwner(), |
39
|
|
|
$this->githubRepo->getName(), |
40
|
|
|
['state' => 'all'], |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
return $this->paginator->fetchAll($this->getMilestonesApi(), 'all', $parameters); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns all issues. |
48
|
|
|
* |
49
|
|
|
* IMPORTANT: |
50
|
|
|
* - GitHub API v3 returns both issues and pullRequests together so filtering needs to be done |
51
|
|
|
* - Pull requests have 'pull_request' key. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function fetchAllIssues() |
56
|
|
|
{ |
57
|
|
|
$results = []; |
58
|
|
|
|
59
|
|
|
foreach ($this->fetchAllIssuesAndPullRequests() as $result) { |
60
|
|
|
if (!array_key_exists('pull_request', $result)) { |
61
|
|
|
$results[] = $result; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $results; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array|mixed |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function fetchAllIssuesAndPullRequests() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$parameters = [ |
74
|
|
|
$this->githubRepo->getOwner(), |
75
|
|
|
$this->githubRepo->getName(), |
76
|
|
|
['state' => 'all'], |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
return $this->paginator->fetchAll($this->getIssueApi(), 'all', $parameters); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return \Github\Api\ApiInterface |
84
|
|
|
*/ |
85
|
|
|
private function getIssueApi() |
86
|
|
|
{ |
87
|
|
|
return $this->client->api('issues'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function getMilestonesApi() |
91
|
|
|
{ |
92
|
|
|
return $this->getIssueApi()->milestones(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.