Completed
Push — master ( 1af7dd...c5badb )
by Ryota
01:36
created

Client::createAuthStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 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 Polidog\Esa\Exception\ClientException;
9
10
final class Client implements ClientInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $accessToken;
16
17
    /**
18
     * @var HttpClientInterface
19
     */
20
    private $httpClient;
21
22
    /**
23
     * @var array
24
     */
25
    private static $httpOptions = [
26
        'base_uri' => 'https://api.esa.io/v1/',
27
        'timeout' => 60,
28
        'allow_redirect' => false,
29
        'headers' => [
30
            'User-Agent' => 'esa-php-api v2',
31
            'Accept' => 'application/json',
32
        ],
33
    ];
34
35
    /**
36
     * @param string              $accessToken
37
     * @param HttpClientInterface $httpClient
38
     */
39
    public function __construct($accessToken, HttpClientInterface $httpClient)
40
    {
41
        $this->accessToken = $accessToken;
42
        $this->httpClient = $httpClient;
43
    }
44
45
    /**
46
     * @param string $method
47
     * @param string $path
48
     * @param array  $data
49
     *
50
     * @return array
51
     *
52
     * @throws ClientException
53
     * @throws \RuntimeException
54
     */
55
    public function request($method, $path, array $data = [])
56
    {
57
        try {
58
            $response = $this->httpClient->request($method, $path, $data);
59
60
            return json_decode($response->getBody()->getContents(), true);
61
        } catch (GuzzleException $e) {
62
            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...
63
        }
64
    }
65
66
    /**
67
     * @param       $accessToken
68
     * @param array $httpOptions
69
     *
70
     * @return Client
71
     */
72
    public static function factory($accessToken, $httpOptions = [])
73
    {
74
        $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...
75
        $authorization = new Authorization($accessToken);
76
77
        $httpOptions['handler'] = HandlerStack::create();
78
        $authorization->push($httpOptions['handler']);
79
80
        return new self($accessToken, new \GuzzleHttp\Client($httpOptions));
81
    }
82
83
}
84