Completed
Pull Request — master (#19)
by Yuan
01:58
created

Lift::getResponseJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Acquia\LiftClient;
4
5
use Acquia\LiftClient\Manager\CaptureManager;
6
use Acquia\LiftClient\Manager\DecideManager;
7
use Acquia\LiftClient\Manager\RuleManager;
8
use Acquia\LiftClient\Manager\SegmentManager;
9
use Acquia\LiftClient\Manager\SlotManager;
10
use Acquia\LiftClient\Manager\GoalManager;
11
use NickVeenhof\Hmac\Guzzle\HmacAuthMiddleware;
12
use NickVeenhof\Hmac\Key;
13
use GuzzleHttp\Client;
14
use GuzzleHttp\HandlerStack;
15
use GuzzleHttp\Psr7\Request;
16
use Psr\Http\Message\RequestInterface;
17
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
        // A key consists of your UUID and a MIME base64 encoded shared secret.
59
        $authKey = new Key($public_key, $secret_key);
60
61 99
        // Set our default HandlerStack if nothing is provided
62
        if (!isset($config['handler'])) {
63
            $config['handler'] = HandlerStack::create();
64 99
        }
65
66 99
        // Add our Account and Site identifiers.
67 99
        $config['handler']->push($this->addLiftAccountAndSiteId($account_id, $site_id));
68
69
        $this->unauthenticatedClient = new Client($config);
70 99
71
        if (isset($config['auth_middleware'])) {
72
            if ($config['auth_middleware'] !== false) {
73
                $config['handler']->push($config['auth_middleware']);
74
            }
75 99
        } else {
76
            $middleware = new HmacAuthMiddleware($authKey, 'Decision');
77 99
            $config['handler']->push($middleware);
78 99
        }
79 33
80
        $this->authenticatedClient = new Client($config);
81 66
    }
82
83
    /**
84
     * Create a handler that adds lift account id and site id.
85
     *
86 99
     * @param string $account_id The Lift Web Account Identifier. Eg.: MYACCOUNT
87 99
     * @param string $site_id    The Lift Web Site Identifier. Eg.: my-drupal-site
88
     * @return function The handler that adds Lift account id and site id
89 99
     */
90
    private function addLiftAccountAndSiteId($account_id, $site_id)
91
    {
92 99
        // We cannot keep references in such functions.
93 99
        return function (callable $handler) use ($account_id, $site_id) {
94
            return function (
95
              RequestInterface $request,
96 99
              array $options
97
            ) use ($handler, $account_id, $site_id) {
98
                $auth_query = "account_id={$account_id}&site_id={$site_id}";
99
                $uri = $request->getUri();
100 99
                $query = $uri->getQuery();
101 99
                if (empty($query)) {
102 99
                    $query = $auth_query;
103 99
                } else {
104 99
                    $query = $query.'&'.$auth_query;
105 66
                }
106
                $uri = $uri->withQuery($query);
107
                $uri->withQuery($query);
108 99
109 99
                $request = $request->withUri($uri);
110
111 99
                return $handler($request, $options);
112
            };
113 99
        };
114 99
    }
115 99
116
    /**
117
     * Pings the service to ensure that it is available.
118
     *
119
     * @return mixed the value encoded in the JSON.
120
     */
121
    public function ping()
122
    {
123
        $request = new Request('GET', '/ping');
124
        $response = $this->authenticatedClient->send($request);
125 3
        $body = (string) $response->getBody();
126
127
        return json_decode($body, true);
128 3
    }
129
130 3
    /**
131
     * Get the Slot Manager.
132
     *
133
     * @return \Acquia\LiftClient\Manager\SlotManager
134
     */
135
    public function getSlotManager()
136
    {
137
        return new SlotManager($this->authenticatedClient);
138 24
    }
139
140 24
    /**
141
     * Get the Slot Manager.
142
     *
143
     * @return \Acquia\LiftClient\Manager\GoalManager
144
     */
145
    public function getGoalManager()
146
    {
147
        return new GoalManager($this->authenticatedClient);
148 27
    }
149
150 27
    /**
151
     * Get the Segment Manager.
152
     *
153
     * @return \Acquia\LiftClient\Manager\SegmentManager
154
     */
155
    public function getSegmentManager()
156
    {
157
        return new SegmentManager($this->authenticatedClient);
158 6
    }
159
160 6
    /**
161
     * Get the Rules Manager.
162
     *
163
     * @return \Acquia\LiftClient\Manager\RuleManager
164
     */
165
    public function getRuleManager()
166
    {
167
        return new RuleManager($this->authenticatedClient);
168 24
    }
169
170 24
    /**
171
     * Get the Capture Manager.
172
     *
173
     * @return \Acquia\LiftClient\Manager\CaptureManager
174
     */
175
    public function getCaptureManager()
176
    {
177
        return new CaptureManager($this->unauthenticatedClient);
178 9
    }
179
180 9
    /**
181
     * Get the Decide Manager.
182
     *
183
     * @return \Acquia\LiftClient\Manager\DecideManager
184
     */
185
    public function getDecideManager()
186
    {
187
        return new DecideManager($this->unauthenticatedClient);
188 6
    }
189
190
}
191