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
|
|
|
* |
13
|
|
|
* @package AcquiaCloudApi\CloudApi |
14
|
|
|
*/ |
15
|
|
|
class Client implements ClientInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ConnectorInterface The API connector. |
19
|
|
|
*/ |
20
|
|
|
protected $connector; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array Query strings to be applied to the request. |
24
|
|
|
*/ |
25
|
|
|
protected $query = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array Guzzle options to be applied to the request. |
29
|
|
|
*/ |
30
|
|
|
protected $options = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Client constructor. |
34
|
|
|
* |
35
|
|
|
* @param ConnectorInterface $connector |
36
|
|
|
*/ |
37
|
|
|
public function __construct(ConnectorInterface $connector) |
38
|
|
|
{ |
39
|
|
|
$this->connector = $connector; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Client factory method for instantiating . |
44
|
|
|
* |
45
|
|
|
* @param ConnectorInterface $connector |
46
|
|
|
* |
47
|
|
|
* @return static |
48
|
|
|
*/ |
49
|
|
|
public static function factory(ConnectorInterface $connector) |
50
|
|
|
{ |
51
|
|
|
$client = new static( |
52
|
|
|
$connector |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
return $client; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function request(string $verb, string $path, array $options = []) |
62
|
|
|
{ |
63
|
|
|
// @TODO follow this up by removing $options from the parameters able |
64
|
|
|
// to be passed into this function and instead solely relying on the |
65
|
|
|
// addOption() method as this can then be tested. |
66
|
|
|
$options = $this->options + $options; |
67
|
|
|
$options['query'] = $this->query; |
68
|
|
|
|
69
|
|
|
if (!empty($options['query']['filter']) && is_array($options['query']['filter'])) { |
70
|
|
|
// Default to an AND filter. |
71
|
|
|
$options['query']['filter'] = implode(',', $options['query']['filter']); |
72
|
|
|
} |
73
|
|
|
try { |
74
|
|
|
$request = $this->connector->createRequest($verb, $path); |
75
|
|
|
$response = $this->connector->sendRequest($request, $options); |
76
|
|
|
} catch (BadResponseException $e) { |
77
|
|
|
$response = $e->getResponse(); |
78
|
|
|
} |
79
|
|
|
return $this->processResponse($response); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// /** |
83
|
|
|
// * @inheritdoc |
84
|
|
|
// */ |
85
|
|
|
// public function makeRequest(string $verb, string $path, array $options = []) |
86
|
|
|
// { |
87
|
|
|
// try { |
88
|
|
|
// $response = $this->connector->sendRequest($verb, $path, $options); |
89
|
|
|
// } catch (BadResponseException $e) { |
90
|
|
|
// $response = $e->getResponse(); |
91
|
|
|
// } |
92
|
|
|
|
93
|
|
|
// return $response; |
94
|
|
|
// } |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
|
|
public function processResponse(ResponseInterface $response) |
100
|
|
|
{ |
101
|
|
|
|
102
|
|
|
$body = $response->getBody(); |
103
|
|
|
|
104
|
|
|
$object = json_decode($body); |
105
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
106
|
|
|
return $body; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (property_exists($object, '_embedded') && property_exists($object->_embedded, 'items')) { |
110
|
|
|
return $object->_embedded->items; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (property_exists($object, 'error') && property_exists($object, 'message')) { |
114
|
|
|
throw new ApiErrorException($object); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $object; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
|
|
public function getQuery() |
124
|
|
|
{ |
125
|
|
|
return $this->query; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritdoc |
130
|
|
|
*/ |
131
|
|
|
public function clearQuery() |
132
|
|
|
{ |
133
|
|
|
$this->query = []; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritdoc |
138
|
|
|
*/ |
139
|
|
|
public function addQuery($name, $value) |
140
|
|
|
{ |
141
|
|
|
$this->query = array_merge_recursive($this->query, [$name => $value]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritdoc |
146
|
|
|
*/ |
147
|
|
|
public function getOptions() |
148
|
|
|
{ |
149
|
|
|
return $this->options; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritdoc |
154
|
|
|
*/ |
155
|
|
|
public function clearOptions() |
156
|
|
|
{ |
157
|
|
|
$this->options = []; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritdoc |
162
|
|
|
*/ |
163
|
|
|
public function addOption($name, $value) |
164
|
|
|
{ |
165
|
|
|
$this->options = array_merge_recursive($this->options, [$name => $value]); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|