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