1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acquia\LiftClient; |
4
|
|
|
|
5
|
|
|
use Acquia\LiftClient\Manager\RuleManager; |
6
|
|
|
use Acquia\LiftClient\Manager\SegmentManager; |
7
|
|
|
use Acquia\LiftClient\Manager\SlotManager; |
8
|
|
|
use Acquia\LiftClient\Manager\GoalManager; |
9
|
|
|
use NickVeenhof\Hmac\Guzzle\HmacAuthMiddleware; |
10
|
|
|
use NickVeenhof\Hmac\Key; |
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
use GuzzleHttp\HandlerStack; |
13
|
|
|
use GuzzleHttp\Psr7\Request; |
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
|
16
|
|
|
class Lift extends Client |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \NickVeenhof\Hmac\KeyInterface A sample key |
20
|
|
|
*/ |
21
|
|
|
protected $authKey; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string The account we are using |
25
|
|
|
*/ |
26
|
|
|
protected $accountId; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string The site identifier we are using |
30
|
|
|
*/ |
31
|
|
|
protected $siteId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Overrides \GuzzleHttp\Client::__construct(). |
35
|
|
|
* |
36
|
|
|
* @param string $account_id The Lift Web Account Identifier. Eg.: MYACCOUNT |
37
|
|
|
* @param string $site_id The Lift Web Site Identifier. Eg.: my-drupal-site |
38
|
|
|
* @param string $public_key The Lift Web Public Key. Not all API keys have |
39
|
|
|
* the same permissions so be mindful which key |
40
|
|
|
* you are using |
41
|
|
|
* @param string $secret_key The Lift Web Secret Key belonging to the Public |
42
|
|
|
* Key |
43
|
|
|
* @param array $config |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
$account_id, |
47
|
|
|
$site_id, |
48
|
27 |
|
$public_key, |
49
|
|
|
$secret_key, |
50
|
|
|
array $config = [] |
51
|
|
|
) { |
52
|
|
|
// "base_url" parameter changed to "base_uri" in Guzzle6, so the following line |
53
|
|
|
// is there to make sure it does not disrupt previous configuration. |
54
|
|
|
if (!isset($config['base_uri']) && isset($config['base_url'])) { |
55
|
|
|
$config['base_uri'] = $config['base_url']; |
56
|
|
|
} |
57
|
27 |
|
|
58
|
|
|
// Setting up the headers. |
59
|
|
|
$config['headers']['Content-Type'] = 'application/json'; |
60
|
|
|
|
61
|
|
|
// A key consists of your UUID and a MIME base64 encoded shared secret. |
62
|
27 |
|
$this->authKey = new Key($public_key, $secret_key); |
63
|
|
|
|
64
|
|
|
$this->accountId = $account_id; |
65
|
27 |
|
$this->siteId = $site_id; |
66
|
|
|
|
67
|
27 |
|
// Set our default HandlerStack if nothing is provided |
68
|
27 |
|
if (!isset($config['handler'])) { |
69
|
|
|
$config['handler'] = HandlerStack::create(); |
70
|
|
|
} |
71
|
27 |
|
|
72
|
|
|
// Add our Account and Site identifiers. |
73
|
|
|
$config['handler']->push($this->addLiftAccountAndSiteId()); |
74
|
|
|
|
75
|
|
|
if (isset($config['auth_middleware'])) { |
76
|
27 |
|
if ($config['auth_middleware'] !== false) { |
77
|
|
|
$config['handler']->push($config['auth_middleware']); |
78
|
27 |
|
} |
79
|
27 |
|
} else { |
80
|
9 |
|
$middleware = new HmacAuthMiddleware($this->authKey, 'Decision'); |
81
|
|
|
$config['handler']->push($middleware); |
82
|
18 |
|
} |
83
|
|
|
|
84
|
|
|
parent::__construct($config); |
85
|
|
|
} |
86
|
|
|
|
87
|
27 |
|
public function addLiftAccountAndSiteId() |
88
|
27 |
|
{ |
89
|
|
|
// We cannot keep references in such functions. |
90
|
27 |
|
$account_id = $this->accountId; |
91
|
|
|
$site_id = $this->siteId; |
92
|
|
|
|
93
|
27 |
|
return function (callable $handler) use ($account_id, $site_id) { |
94
|
27 |
|
return function ( |
95
|
|
|
RequestInterface $request, |
96
|
|
|
array $options |
97
|
27 |
|
) use ($handler, $account_id, $site_id) { |
98
|
|
|
$auth_query = "account_id={$account_id}&site_id={$site_id}"; |
99
|
|
|
$uri = $request->getUri(); |
100
|
|
|
$query = $uri->getQuery(); |
101
|
27 |
|
if (empty($query)) { |
102
|
27 |
|
$query = $auth_query; |
103
|
27 |
|
} else { |
104
|
27 |
|
$query = $query.'&'.$auth_query; |
105
|
27 |
|
} |
106
|
18 |
|
$uri = $uri->withQuery($query); |
107
|
|
|
$uri->withQuery($query); |
108
|
|
|
|
109
|
27 |
|
$request = $request->withUri($uri); |
110
|
27 |
|
|
111
|
|
|
return $handler($request, $options); |
112
|
27 |
|
}; |
113
|
|
|
}; |
114
|
27 |
|
} |
115
|
27 |
|
|
116
|
27 |
|
/** |
117
|
|
|
* Pings the service to ensure that it is available. |
118
|
|
|
* |
119
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
120
|
|
|
* |
121
|
|
|
* @throws \GuzzleHttp\Exception\RequestException |
122
|
|
|
*/ |
123
|
|
|
public function ping() |
124
|
|
|
{ |
125
|
|
|
// Now make the request. |
126
|
3 |
|
$request = new Request('GET', '/ping'); |
127
|
|
|
|
128
|
|
|
return $this->getResponseJson($request); |
129
|
3 |
|
} |
130
|
3 |
|
|
131
|
|
|
/** |
132
|
|
|
* Get the Slot Manager. |
133
|
|
|
* |
134
|
|
|
* @return \Acquia\LiftClient\Manager\SlotManager |
135
|
|
|
*/ |
136
|
|
|
public function getSlotManager() |
137
|
|
|
{ |
138
|
24 |
|
return new SlotManager($this); |
139
|
|
|
} |
140
|
24 |
|
|
141
|
|
|
/** |
142
|
|
|
* Get the Slot Manager. |
143
|
|
|
* |
144
|
|
|
* @return \Acquia\LiftClient\Manager\GoalManager |
145
|
|
|
*/ |
146
|
|
|
public function getGoalManager() |
147
|
|
|
{ |
148
|
|
|
return new GoalManager($this); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the Segment Manager. |
153
|
|
|
* |
154
|
|
|
* @return \Acquia\LiftClient\Manager\SegmentManager |
155
|
|
|
*/ |
156
|
|
|
public function getSegmentManager() |
157
|
21 |
|
{ |
158
|
|
|
return new SegmentManager($this); |
159
|
21 |
|
} |
160
|
12 |
|
|
161
|
12 |
|
/** |
162
|
|
|
* Get the Rules Manager. |
163
|
|
|
* |
164
|
2 |
|
* @return \Acquia\LiftClient\Manager\RuleManager |
165
|
|
|
*/ |
166
|
|
|
public function getRuleManager() |
167
|
|
|
{ |
168
|
|
|
return new RuleManager($this); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Make the given Request and return as JSON Decoded PHP object. |
173
|
|
|
* |
174
|
|
|
* @param RequestInterface $request |
175
|
|
|
* |
176
|
|
|
* @return mixed the value encoded in <i>json</i> in appropriate |
177
|
|
|
* PHP type. Values true, false and |
178
|
|
|
* null (case-insensitive) are returned as <b>TRUE</b>, <b>FALSE</b> |
179
|
|
|
* and <b>NULL</b> respectively. <b>NULL</b> is returned if the |
180
|
|
|
* <i>json</i> cannot be decoded or if the encoded |
181
|
|
|
* data is deeper than the recursion limit |
182
|
|
|
* |
183
|
|
|
* @throws \GuzzleHttp\Exception\RequestException |
184
|
|
|
*/ |
185
|
|
|
public function getResponseJson(RequestInterface $request) |
186
|
|
|
{ |
187
|
|
|
$response = $this->send($request); |
188
|
|
|
$body = (string) $response->getBody(); |
189
|
|
|
|
190
|
|
|
return json_decode($body, true); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|