Completed
Push — master ( aaccd5...c6e65b )
by Jonathan
10s
created

IssueFetcher::getNextUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ChangelogGenerator;
6
7
use function sprintf;
8
use function str_replace;
9
use function urlencode;
10
11
class IssueFetcher
12
{
13
    private const ROOT_URL = 'https://api.github.com';
14
15
    /** @var IssueClient */
16
    private $issueClient;
17
18 1
    public function __construct(IssueClient $issueClient)
19
    {
20 1
        $this->issueClient = $issueClient;
21 1
    }
22
23
    /**
24
     * @return Issue[]
25
     */
26 1
    public function fetchMilestoneIssues(string $user, string $repository, string $milestone) : array
27
    {
28 1
        $url = $this->getMilestoneIssuesUrl($user, $repository, $milestone);
29
30 1
        $issues = [];
31
32 1
        while (true) {
33 1
            $response = $this->issueClient->execute($url);
34
35 1
            $body = $response->getBody();
36
37 1
            foreach ($body['items'] as $item) {
38 1
                $issues[] = $item;
39
            }
40
41 1
            $nextUrl = $response->getNextUrl();
42
43 1
            if ($nextUrl !== null) {
44 1
                $url = $nextUrl;
45
46 1
                continue;
47
            }
48
49 1
            break;
50
        }
51
52 1
        return $issues;
53
    }
54
55 1
    private function getMilestoneIssuesUrl(string $user, string $repository, string $milestone) : string
56
    {
57 1
        $milestoneQuery = str_replace('"', '\"', $milestone);
58
59 1
        $query = urlencode(sprintf(
60 1
            'milestone:"%s" repo:%s/%s state:closed',
61 1
            $milestoneQuery,
62 1
            $user,
63 1
            $repository
64
        ));
65
66 1
        return sprintf('%s/search/issues?q=%s', self::ROOT_URL, $query);
67
    }
68
}
69