ApiClient   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 83
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A sendRequestToEndPoint() 0 10 2
A createRequestForEndpoint() 0 16 2
B sendRequest() 0 18 5
A getDataFromResponse() 0 15 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\Exception\FailedRequestException;
11
use PtrTn\Battlerite\Exception\InvalidRequestException;
12
use PtrTn\Battlerite\Exception\InvalidResourceException;
13
use PtrTn\Battlerite\Query\QueryInterface;
14
15
class ApiClient
16
{
17
    private const BASE_URL = 'https://api.dc01.gamelockerapp.com';
18
19
    /**
20
     * @var string
21
     */
22
    private $apiKey;
23
24
    /**
25
     * @var ClientInterface
26
     */
27
    private $httpClient;
28
29 12
    public function __construct(string $apiKey, ClientInterface $httpClient)
30
    {
31 12
        $this->apiKey = $apiKey;
32 12
        $this->httpClient = $httpClient;
33 12
    }
34
35 9
    public function sendRequestToEndPoint(string $endpoint, ?QueryInterface $query = null)
36
    {
37 9
        $request = $this->createRequestForEndpoint(
38 9
            $endpoint,
39 9
            $query ? $query->toQueryString() : null
40
        );
41 9
        $response = $this->sendRequest($request);
42
43 5
        return $this->getDataFromResponse($response);
44
    }
45
46 9
    private function createRequestForEndpoint(string $endpoint, string $query = null): Request
47
    {
48 9
        $uri = self::BASE_URL . $endpoint;
49 9
        if (isset($query)) {
50 1
            $uri .= '?' . $query;
51
        }
52
53 9
        return new Request(
54 9
            'GET',
55 9
            $uri,
56
            [
57 9
                'Accept' => 'application/json',
58 9
                'Authorization' => $this->apiKey
59
            ]
60
        );
61
    }
62
63 9
    private function sendRequest($request): ResponseInterface
64
    {
65
        try {
66 9
            return $this->httpClient->send($request);
67 4
        } catch (ClientException $e) {
68 4
            if ($e->getResponse()->getStatusCode() === 401) {
69 1
                throw InvalidRequestException::invalidApiKey($this->apiKey);
70
            }
71 3
            if ($e->getResponse()->getStatusCode() === 429) {
72 1
                throw InvalidRequestException::rateLimitReached();
73
            }
74 2
            if ($e->getResponse()->getStatusCode() === 404) {
75 1
                throw InvalidResourceException::resourceNotFound();
76
            }
77
78 1
            throw FailedRequestException::createForException($e);
79
        }
80
    }
81
82 5
    private function getDataFromResponse(ResponseInterface $response): array
83
    {
84 5
        $responseBody = $response->getBody()->getContents();
85 5
        if (!isset($responseBody) || $responseBody === '') {
86 1
            throw new FailedRequestException('No response');
87
        }
88
89
        try {
90 4
            $responseData = \GuzzleHttp\json_decode($responseBody, true, 512, JSON_BIGINT_AS_STRING);
91 1
        } catch (InvalidArgumentException $e) {
92 1
            throw FailedRequestException::invalidResponseJson($e);
93
        }
94
95 3
        return $responseData;
96
    }
97
}
98