1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acquia\LiftClient\Manager; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
|
8
|
|
|
abstract class ManagerBase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array A list of query parameters that the URL could possibly have |
12
|
|
|
* Format: query_parameter_name => query_parameter_default_value |
13
|
|
|
*/ |
14
|
|
|
protected $queryParameters = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \GuzzleHttp\ClientInterface The request client |
18
|
|
|
*/ |
19
|
|
|
protected $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param \GuzzleHttp\ClientInterface $client The request client |
23
|
|
|
*/ |
24
|
114 |
|
public function __construct(ClientInterface $client) |
25
|
|
|
{ |
26
|
114 |
|
$this->client = $client; |
27
|
114 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the client. |
31
|
|
|
* |
32
|
|
|
* @return \GuzzleHttp\ClientInterface The request client |
33
|
|
|
*/ |
34
|
18 |
|
public function getClient() { |
35
|
18 |
|
return $this->client; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Make the given Request and return as JSON Decoded PHP object. |
40
|
|
|
* |
41
|
|
|
* @param \Psr\Http\Message\RequestInterface $request |
42
|
|
|
* |
43
|
|
|
* @return mixed the value encoded in <i>json</i> in appropriate |
44
|
|
|
* PHP type. Values true, false and |
45
|
|
|
* null (case-insensitive) are returned as <b>TRUE</b>, <b>FALSE</b> |
46
|
|
|
* and <b>NULL</b> respectively. <b>NULL</b> is returned if the |
47
|
|
|
* <i>json</i> cannot be decoded or if the encoded |
48
|
|
|
* data is deeper than the recursion limit |
49
|
|
|
*/ |
50
|
78 |
|
protected function getResponseJson(RequestInterface $request) |
51
|
|
|
{ |
52
|
78 |
|
$response = $this->client->send($request); |
53
|
42 |
|
$body = (string) $response->getBody(); |
54
|
|
|
|
55
|
42 |
|
return json_decode($body, true); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get query string of using the options. |
60
|
|
|
* |
61
|
|
|
* @param $options The options |
62
|
|
|
* @return string The query string |
63
|
|
|
*/ |
64
|
18 |
|
protected function getQueryString($options) { |
65
|
18 |
|
$queries = []; |
66
|
18 |
|
foreach ($this->queryParameters as $queryName => $queryDefaultValue) { |
67
|
|
|
// Use user value if possible. |
68
|
18 |
|
if (isset($options[$queryName])) { |
69
|
9 |
|
$queries[] = $queryName . '=' . rawurlencode($options[$queryName]); |
70
|
9 |
|
continue; |
71
|
|
|
} |
72
|
|
|
// Use default value if possible. |
73
|
9 |
|
if (is_string($queryDefaultValue)) { |
74
|
|
|
$queries[] = $queryName . '=' . rawurlencode($queryDefaultValue); |
75
|
3 |
|
continue; |
76
|
|
|
} |
77
|
12 |
|
} |
78
|
18 |
|
$queryString = implode('&', $queries); |
79
|
18 |
|
return empty($queryString) ? '' : '?' . $queryString; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|