1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AcquiaCloudApi\Connector; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
7
|
|
|
use AcquiaCloudApi\Exception\ApiErrorException; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Client |
12
|
|
|
* @package AcquiaCloudApi\CloudApi |
13
|
|
|
*/ |
14
|
|
|
class Client implements ClientInterface |
15
|
|
|
{ |
16
|
|
|
/** @var ConnectorInterface The API connector. */ |
17
|
|
|
protected $connector; |
18
|
|
|
|
19
|
|
|
/** @var array Query strings to be applied to the request. */ |
20
|
|
|
protected $query = []; |
21
|
|
|
|
22
|
|
|
/** @var array Guzzle options to be applied to the request. */ |
23
|
|
|
protected $options = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Client constructor. |
27
|
|
|
* |
28
|
|
|
* @param ConnectorInterface $connector |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ConnectorInterface $connector) |
31
|
|
|
{ |
32
|
|
|
$this->connector = $connector; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Client factory method for instantiating . |
37
|
|
|
* |
38
|
|
|
* @param ConnectorInterface $connector |
39
|
|
|
* |
40
|
|
|
* @return static |
41
|
|
|
*/ |
42
|
|
|
public static function factory(ConnectorInterface $connector) |
43
|
|
|
{ |
44
|
|
|
$client = new static( |
45
|
|
|
$connector |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
return $client; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Takes parameters passed in, makes a request to the API, and processes the response. |
53
|
|
|
* |
54
|
|
|
* @param string $verb |
55
|
|
|
* @param string $path |
56
|
|
|
* @param array $options |
57
|
|
|
* |
58
|
|
|
* @return mixed|StreamInterface |
59
|
|
|
*/ |
60
|
|
|
public function request(string $verb, string $path, array $options = []) |
61
|
|
|
{ |
62
|
|
|
$options = $this->options; |
63
|
|
|
$options['query'] = $this->query; |
64
|
|
|
|
65
|
|
|
if (!empty($options['query']['filter']) && is_array($options['query']['filter'])) { |
66
|
|
|
// Default to an AND filter. |
67
|
|
|
$options['query']['filter'] = implode(',', $options['query']['filter']); |
68
|
|
|
} |
69
|
|
|
$response = $this->makeRequest($verb, $path, $options); |
70
|
|
|
|
71
|
|
|
return $this->processResponse($response); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Makes a request to the API. |
76
|
|
|
* |
77
|
|
|
* @param string $verb |
78
|
|
|
* @param string $path |
79
|
|
|
* @param array $options |
80
|
|
|
* |
81
|
|
|
* @return ResponseInterface |
82
|
|
|
*/ |
83
|
|
|
public function makeRequest(string $verb, string $path, array $options = []) |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
$response = $this->connector->sendRequest($verb, $path, $options); |
87
|
|
|
} catch (BadResponseException $e) { |
88
|
|
|
$response = $e->getResponse(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $response; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Processes the returned response from the API. |
96
|
|
|
* |
97
|
|
|
* @param ResponseInterface $response |
98
|
|
|
* @return mixed|StreamInterface |
99
|
|
|
* @throws ApiErrorException |
100
|
|
|
*/ |
101
|
|
|
public function processResponse(ResponseInterface $response) |
102
|
|
|
{ |
103
|
|
|
|
104
|
|
|
$body = $response->getBody(); |
105
|
|
|
|
106
|
|
|
$object = json_decode($body); |
107
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
108
|
|
|
return $body; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (property_exists($object, '_embedded') && property_exists($object->_embedded, 'items')) { |
112
|
|
|
return $object->_embedded->items; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (property_exists($object, 'error') && property_exists($object, 'message')) { |
116
|
|
|
throw new ApiErrorException($object); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $object; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get query from Client. |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getQuery() |
128
|
|
|
{ |
129
|
|
|
return $this->query; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Clear query. |
134
|
|
|
*/ |
135
|
|
|
public function clearQuery() |
136
|
|
|
{ |
137
|
|
|
$this->query = []; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Add a query parameter to filter results. |
142
|
|
|
* |
143
|
|
|
* @param string $name |
144
|
|
|
* @param string|int $value |
145
|
|
|
*/ |
146
|
|
|
public function addQuery($name, $value) |
147
|
|
|
{ |
148
|
|
|
$this->query = array_merge_recursive($this->query, [$name => $value]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get options from Client. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function getOptions() |
157
|
|
|
{ |
158
|
|
|
return $this->options; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Clear options. |
163
|
|
|
*/ |
164
|
|
|
public function clearOptions() |
165
|
|
|
{ |
166
|
|
|
$this->options = []; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Add an option to the Guzzle request object. |
171
|
|
|
* |
172
|
|
|
* @param string $name |
173
|
|
|
* @param string|int|array $value |
174
|
|
|
*/ |
175
|
|
|
public function addOption($name, $value) |
176
|
|
|
{ |
177
|
|
|
$this->options = array_merge_recursive($this->options, [$name => $value]); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|