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

IssueClient::getNextUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ChangelogGenerator;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Psr7\Response;
9
use function json_decode;
10
use function preg_match;
11
12
class IssueClient
13
{
14
    /** @var Client */
15
    private $client;
16
17 2
    public function __construct(Client $client)
18
    {
19 2
        $this->client = $client;
20 2
    }
21
22 2
    public function execute(string $url) : IssueClientResponse
23
    {
24 2
        $response = $this->client->request('GET', $url);
25
26 2
        $body = (string) $response->getBody();
27
28 2
        $body = json_decode($body, true);
29
30 2
        return new IssueClientResponse($body, $this->getNextUrl($response));
31
    }
32
33 2
    private function getNextUrl(Response $response) : ?string
34
    {
35 2
        $links = $response->getHeader('Link');
36
37 2
        foreach ($links as $link) {
38 1
            $matches = [];
39
40 1
            if (preg_match('#<(?P<url>.*)>; rel="next"#', $link, $matches)) {
41 1
                return $matches['url'];
42
            }
43
        }
44
45 1
        return null;
46
    }
47
}
48