1 | <?php |
||
18 | class Lift |
||
19 | { |
||
20 | /** |
||
21 | * @var \GuzzleHttp\ClientInterface Unauthenticated client |
||
22 | */ |
||
23 | private $unauthenticatedClient; |
||
24 | |||
25 | /** |
||
26 | * @var \GuzzleHttp\ClientInterface Authenticated client |
||
27 | */ |
||
28 | private $authenticatedClient; |
||
29 | |||
30 | /** |
||
31 | * Constructor. |
||
32 | * |
||
33 | * @param string $account_id The Lift Web Account Identifier. Eg.: MYACCOUNT |
||
34 | * @param string $site_id The Lift Web Site Identifier. Eg.: my-drupal-site |
||
35 | * @param string $public_key The Lift Web Public Key. Not all API keys have |
||
36 | * the same permissions so be mindful which key |
||
37 | * you are using |
||
38 | * @param string $secret_key The Lift Web Secret Key belonging to the Public |
||
39 | * Key |
||
40 | * @param array $config Additional configs |
||
41 | */ |
||
42 | public function __construct( |
||
43 | $account_id, |
||
44 | $site_id, |
||
45 | $public_key, |
||
46 | $secret_key, |
||
47 | 99 | array $config = [] |
|
48 | ) { |
||
49 | // "base_url" parameter changed to "base_uri" in Guzzle6, so the following line |
||
50 | // is there to make sure it does not disrupt previous configuration. |
||
51 | if (!isset($config['base_uri']) && isset($config['base_url'])) { |
||
52 | $config['base_uri'] = $config['base_url']; |
||
53 | } |
||
54 | |||
55 | // Setting up the headers. |
||
56 | 99 | $config['headers']['Content-Type'] = 'application/json'; |
|
57 | |||
58 | // Create both unauthenticated and authenticated handler stacks. |
||
59 | $handlerStack = isset($config['handler']) ? $config['handler'] : HandlerStack::create(); |
||
60 | $accountAndSiteIdHandler = $this->getAccountAndSiteIdHandler($account_id, $site_id); |
||
61 | 99 | $handlerStack->push($accountAndSiteIdHandler, 'acquia_lift_account_and_site_ids'); |
|
62 | |||
63 | // Both stacks are cloned so they are both completely internal now. |
||
64 | 99 | $unauthenticatedHandlerStack = clone $handlerStack; |
|
65 | $authenticatedHandlerStack = clone $handlerStack; |
||
66 | 99 | ||
67 | 99 | // Set an authentication handler accordingly. |
|
68 | if (isset($config['auth_middleware'])) { |
||
69 | if ($config['auth_middleware'] !== false) { |
||
70 | 99 | $authenticatedHandlerStack->push($config['auth_middleware'], 'acquia_lift_hmac_auth'); |
|
71 | } |
||
72 | } else { |
||
73 | // A key consists of your UUID and a MIME base64 encoded shared secret. |
||
74 | $authKey = new Key($public_key, $secret_key); |
||
75 | 99 | $middleware = new HmacAuthMiddleware($authKey, 'Decision'); |
|
76 | $authenticatedHandlerStack->push($middleware, 'acquia_lift_hmac_auth'); |
||
77 | 99 | } |
|
78 | 99 | ||
79 | 33 | // Create both unauthenticated and authenticated handlers. |
|
80 | $config['handler'] = $unauthenticatedHandlerStack; |
||
81 | 66 | $this->unauthenticatedClient = new Client($config); |
|
82 | $config['handler'] = $authenticatedHandlerStack; |
||
83 | $this->authenticatedClient = new Client($config); |
||
84 | } |
||
85 | |||
86 | 99 | /** |
|
87 | 99 | * Get a handler that adds lift account id and site id. |
|
88 | * |
||
89 | 99 | * @param string $account_id The Lift Web Account Identifier. Eg.: MYACCOUNT |
|
90 | * @param string $site_id The Lift Web Site Identifier. Eg.: my-drupal-site |
||
91 | * @return function The handler that adds Lift account id and site id |
||
92 | 99 | */ |
|
93 | 99 | private function getAccountAndSiteIdHandler($account_id, $site_id) |
|
94 | { |
||
95 | // We cannot keep references in such functions. |
||
96 | 99 | return function (callable $handler) use ($account_id, $site_id) { |
|
97 | return function ( |
||
98 | RequestInterface $request, |
||
99 | array $options |
||
100 | 99 | ) use ($handler, $account_id, $site_id) { |
|
101 | 99 | $auth_query = "account_id={$account_id}&site_id={$site_id}"; |
|
102 | 99 | $uri = $request->getUri(); |
|
103 | 99 | $query = $uri->getQuery(); |
|
104 | 99 | if (empty($query)) { |
|
105 | 66 | $query = $auth_query; |
|
106 | } else { |
||
107 | $query = $query.'&'.$auth_query; |
||
108 | 99 | } |
|
109 | 99 | $uri = $uri->withQuery($query); |
|
110 | $uri->withQuery($query); |
||
111 | 99 | ||
112 | $request = $request->withUri($uri); |
||
113 | 99 | ||
114 | 99 | return $handler($request, $options); |
|
115 | 99 | }; |
|
116 | }; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Pings the service to ensure that it is available. |
||
121 | * |
||
122 | * @return mixed the value encoded in the JSON. |
||
123 | */ |
||
124 | public function ping() |
||
125 | 3 | { |
|
126 | $request = new Request('GET', '/ping'); |
||
127 | $response = $this->authenticatedClient->send($request); |
||
128 | 3 | $body = (string) $response->getBody(); |
|
129 | |||
130 | 3 | return json_decode($body, true); |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get the Slot Manager. |
||
135 | * |
||
136 | * @return \Acquia\LiftClient\Manager\SlotManager |
||
137 | */ |
||
138 | 24 | public function getSlotManager() |
|
142 | |||
143 | /** |
||
144 | * Get the Slot Manager. |
||
145 | * |
||
146 | * @return \Acquia\LiftClient\Manager\GoalManager |
||
147 | */ |
||
148 | 27 | public function getGoalManager() |
|
152 | |||
153 | /** |
||
154 | * Get the Segment Manager. |
||
155 | * |
||
156 | * @return \Acquia\LiftClient\Manager\SegmentManager |
||
157 | */ |
||
158 | 6 | public function getSegmentManager() |
|
162 | |||
163 | /** |
||
164 | * Get the Rules Manager. |
||
165 | * |
||
166 | * @return \Acquia\LiftClient\Manager\RuleManager |
||
167 | */ |
||
168 | 24 | public function getRuleManager() |
|
172 | |||
173 | /** |
||
174 | * Get the Capture Manager. |
||
175 | * |
||
176 | * @return \Acquia\LiftClient\Manager\CaptureManager |
||
177 | */ |
||
178 | 9 | public function getCaptureManager() |
|
182 | |||
183 | /** |
||
184 | * Get the Decide Manager. |
||
185 | * |
||
186 | * @return \Acquia\LiftClient\Manager\DecideManager |
||
187 | */ |
||
188 | 6 | public function getDecideManager() |
|
192 | |||
193 | } |
||
194 |