1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AcquiaCloudApi\CloudApi; |
4
|
|
|
|
5
|
|
|
use Acquia\Hmac\Guzzle\HmacAuthMiddleware; |
6
|
|
|
use Acquia\Hmac\Key; |
7
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\StreamInterface; |
11
|
|
|
use GuzzleHttp\HandlerStack; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Connector |
15
|
|
|
* @package AcquiaCloudApi\CloudApi |
16
|
|
|
*/ |
17
|
|
|
class Connector implements ConnectorInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string BASE_URI |
21
|
|
|
*/ |
22
|
|
|
const BASE_URI = 'https://cloud.acquia.com/api'; |
23
|
|
|
|
24
|
|
|
protected $client; |
25
|
|
|
|
26
|
|
|
protected $config; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Connector constructor. |
30
|
|
|
* |
31
|
|
|
* @param array $config |
32
|
|
|
*/ |
33
|
|
|
public function __construct($config) |
34
|
|
|
{ |
35
|
|
|
$key = new Key($config['key'], $config['secret']); |
36
|
|
|
$middleware = new HmacAuthMiddleware($key); |
37
|
|
|
$stack = HandlerStack::create(); |
38
|
|
|
$stack->push($middleware); |
39
|
|
|
|
40
|
|
|
$this->client = new GuzzleClient([ |
41
|
|
|
'handler' => $stack, |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $verb |
47
|
|
|
* @param string $path |
48
|
|
|
* @param array $query |
49
|
|
|
* @param array $options |
50
|
|
|
* |
51
|
|
|
* @return mixed|StreamInterface |
52
|
|
|
*/ |
53
|
|
|
public function request(string $verb, string $path, array $query = [], array $options = array()) |
54
|
|
|
{ |
55
|
|
|
$options['query'] = $query; |
56
|
|
|
|
57
|
|
|
if (!empty($options['query']['filter']) && is_array($options['query']['filter'])) { |
58
|
|
|
// Default to an AND filter. |
59
|
|
|
$options['query']['filter'] = implode(',', $options['query']['filter']); |
60
|
|
|
} |
61
|
|
|
$response = $this->makeRequest($verb, $path, $query, $options); |
62
|
|
|
|
63
|
|
|
return $this->processResponse($response); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $verb |
68
|
|
|
* @param string $path |
69
|
|
|
* @param array $query |
70
|
|
|
* @param array $options |
71
|
|
|
* @return ResponseInterface |
72
|
|
|
*/ |
73
|
|
|
public function makeRequest(string $verb, string $path, array $query = [], array $options = array()) |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$response = $this->client->$verb(self::BASE_URI . $path, $options); |
77
|
|
|
} catch (ClientException $e) { |
78
|
|
|
print $e->getMessage(); |
79
|
|
|
$response = $e->getResponse(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $response; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ResponseInterface $response |
87
|
|
|
* @return mixed|StreamInterface |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
public function processResponse(ResponseInterface $response) |
91
|
|
|
{ |
92
|
|
|
|
93
|
|
|
$body = $response->getBody(); |
94
|
|
|
|
95
|
|
|
$object = json_decode($body); |
96
|
|
|
if (json_last_error() === JSON_ERROR_NONE) { |
97
|
|
|
// JSON is valid |
98
|
|
|
if (property_exists($object, '_embedded') && property_exists($object->_embedded, 'items')) { |
99
|
|
|
$return = $object->_embedded->items; |
100
|
|
|
} elseif (property_exists($object, 'error')) { |
101
|
|
|
throw new \Exception($object->message); |
102
|
|
|
} else { |
103
|
|
|
$return = $object; |
104
|
|
|
} |
105
|
|
|
} else { |
106
|
|
|
$return = $body; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $return; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|