Client   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 2
cbo 7
dl 0
loc 56
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A request() 0 10 2
A factory() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Polidog\Esa\Client;
6
7
use GuzzleHttp\ClientInterface as HttpClientInterface;
8
use GuzzleHttp\Exception\GuzzleException;
9
use GuzzleHttp\HandlerStack;
10
use Polidog\Esa\Exception\ClientException;
11
12
final class Client implements ClientInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $accessToken;
18
19
    /**
20
     * @var HttpClientInterface
21
     */
22
    private $httpClient;
23
24
    /**
25
     * @var array
26
     */
27
    private static $httpOptions = [
28
        'base_uri' => 'https://api.esa.io/v1/',
29
        'timeout' => 60,
30
        'allow_redirect' => false,
31
        'headers' => [
32
            'User-Agent' => 'esa-php-api v2',
33
            'Accept' => 'application/json',
34
        ],
35
    ];
36
37
    public function __construct(HttpClientInterface $httpClient)
38
    {
39 4
        $this->httpClient = $httpClient;
40
    }
41 4
42 4
    /**
43 4
     * @throws ClientException
44
     * @throws \RuntimeException
45
     */
46
    public function request(string $method, string $path, array $data = []): array
47
    {
48
        try {
49
            $response = $this->httpClient->request($method, $path, $data);
50
51
            return json_decode($response->getBody()->getContents(), true);
52
        } catch (GuzzleException $e) {
53
            throw ClientException::newException($e, $method, $path, $data);
0 ignored issues
show
Documentation introduced by
$e is of type object<GuzzleHttp\Exception\GuzzleException>, but the function expects a object<Exception>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
        }
55 2
    }
56
57
    public static function factory(string $accessToken, array $httpOptions = []): self
58 2
    {
59
        $httpOptions = array_merge(static::$httpOptions, $httpOptions);
0 ignored issues
show
Comprehensibility introduced by
Since Polidog\Esa\Client\Client is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
60 1
        $authorization = new Authorization($accessToken);
61 1
62 1
        $httpOptions['handler'] = HandlerStack::create();
63
        $authorization->push($httpOptions['handler']);
64
65
        return new self(new \GuzzleHttp\Client($httpOptions));
66
    }
67
}
68