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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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: