Completed
Pull Request — master (#6)
by Ryota
04:18
created

Client   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 8
dl 0
loc 73
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A request() 0 10 2
A factory() 0 7 1
A createAuthStack() 0 9 1
1
<?php
2
3
namespace Polidog\Esa\Client;
4
5
use GuzzleHttp\ClientInterface as HttpClientInterface;
6
use GuzzleHttp\Exception\GuzzleException;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Middleware;
9
use Polidog\Esa\Exception\ApiErrorException;
10
use Psr\Http\Message\RequestInterface;
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
    /**
38
     * @param string              $accessToken
39
     * @param HttpClientInterface $httpClient
40
     */
41
    public function __construct($accessToken, HttpClientInterface $httpClient)
42
    {
43
        $this->accessToken = $accessToken;
44
        $this->httpClient = $httpClient;
45
    }
46
47
    /**
48
     * @param string $method
49
     * @param string $path
50
     * @param array  $data
51
     *
52
     * @return array
53
     *
54
     * @throws ApiErrorException
55
     */
56
    public function request($method, $path, array $data = [])
57
    {
58
        try {
59
            $response = $this->httpClient->request($method, $path, $data);
60
61
            return json_decode($response->getBody()->getContents(), true);
62
        } catch (GuzzleException $e) {
63
            throw ApiErrorException::newException($e, $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...
64
        }
65
    }
66
67
    public static function factory($accessToken, $httpOptions = [])
68
    {
69
        $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...
70
        $httpOptions['handler'] = static::createAuthStack($accessToken);
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...
71
72
        return new self($accessToken, new \GuzzleHttp\Client($httpOptions));
73
    }
74
75
    private static function createAuthStack($accessToken)
76
    {
77
        $stack = HandlerStack::create();
78
        $stack->push(Middleware::mapRequest(function (RequestInterface $request) use ($accessToken) {
79
            return $request->withHeader('Authorization', 'Bearer '.$accessToken);
80
        }));
81
82
        return $stack;
83
    }
84
}
85