Completed
Push — master ( 738644...63cce9 )
by Peter
02:17
created

Client::sendRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace PtrTn\Battlerite;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Psr7\Request;
8
use InvalidArgumentException;
9
use Psr\Http\Message\ResponseInterface;
10
use PtrTn\Battlerite\Dto\Matches;
11
use PtrTn\Battlerite\Exception\FailedRequestException;
12
use PtrTn\Battlerite\Exception\InvalidRequestException;
13
14
class Client
15
{
16
    private const BASE_URL = 'https://api.dc01.gamelockerapp.com/shards/global';
17
18
    /**
19
     * @var ClientInterface
20
     */
21
    private $httpClient;
22
    /**
23
     * @var string
24
     */
25
    private $apiKey;
26
27 6
    public function __construct(ClientInterface $httpClient, string $apiKey)
28
    {
29 6
        $this->httpClient = $httpClient;
30 6
        $this->apiKey = $apiKey;
31 6
    }
32
33 6
    public function getMatches(): Matches
34
    {
35 6
        $request = $this->createRequestForEndpoint('/matches');
36
37 6
        $response = $this->sendRequest($request);
38
39 3
        $responseData = $this->getDataFromResponse($response);
40
41 3
        return Matches::createFromArray($responseData);
42
    }
43
44 6
    private function createRequestForEndpoint(string $endpoint): Request
45
    {
46 6
        return new Request(
47 6
            'GET',
48 6
            self::BASE_URL . $endpoint,
49
            [
50 6
                'Accept' => 'application/json',
51 6
                'Authorization' => $this->apiKey
52
            ]
53
        );
54
    }
55
56 6
    private function sendRequest($request): ResponseInterface
57
    {
58
        try {
59 6
            return $this->httpClient->send($request);
60 3
        } catch (ClientException $e) {
61 3
            if ($e->getResponse()->getStatusCode() === 401) {
62 1
                throw InvalidRequestException::invalidApiKey();
63
            }
64 2
            if ($e->getResponse()->getStatusCode() === 429) {
65 1
                throw InvalidRequestException::rateLimitReached();
66
            }
67
68 1
            throw FailedRequestException::createForException($e);
69
        }
70
    }
71
72 3
    private function getDataFromResponse(ResponseInterface $response): array
73
    {
74 3
        $responseBody = $response->getBody()->getContents();
75 3
        if (!isset($responseBody)) {
76
            throw new FailedRequestException('No response');
77
        }
78
79
        try {
80 3
            $responseData = \GuzzleHttp\json_decode($responseBody, true);
81
        } catch (InvalidArgumentException $e) {
82
            throw FailedRequestException::invalidResponseJson($e);
83
        }
84
85 3
        return $responseData;
86
    }
87
}
88