Completed
Push — master ( 43dd0a...110067 )
by Nick
19s
created

Lift::getRuleManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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