Test Failed
Push — master ( 2fa9b7...efd8a4 )
by Peter
02:50
created

Client::getMatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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