Test Failed
Push — master ( c4dde2...b07990 )
by Peter
01:39
created

Client::sendRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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