Completed
Pull Request — master (#19)
by Yuan
02:05
created

Lift   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 15

Test Coverage

Coverage 86.79%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 15
dl 0
loc 175
ccs 46
cts 53
cp 0.8679
rs 9.1666
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getSlotManager() 0 4 1
A getGoalManager() 0 4 1
A getSegmentManager() 0 4 1
B __construct() 0 42 6
A ping() 0 8 1
A getRuleManager() 0 4 1
A getCaptureManager() 0 4 1
A getDecideManager() 0 4 1
B getAccountAndSiteIdHandler() 0 25 2
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 99
    public function __construct(
43
      $account_id,
44
      $site_id,
45
      $public_key,
46
      $secret_key,
47
      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 99
        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 99
        $handlerStack = isset($config['handler']) ? $config['handler'] : HandlerStack::create();
60 99
        $handlerStack->push($this->getAccountAndSiteIdHandler($account_id, $site_id));
61
62
        // Both stacks are cloned so they are both completely internal now.
63 99
        $unauthenticatedHandlerStack = clone $handlerStack;
64 99
        $authenticatedHandlerStack = clone $handlerStack;
65
66
        // Set an authentication handler accordingly.
67 99
        if (isset($config['auth_middleware'])) {
68 99
            if ($config['auth_middleware'] !== false) {
69 33
                $authenticatedHandlerStack->push($config['auth_middleware']);
70
            }
71 66
        } else {
72
            // A key consists of your UUID and a MIME base64 encoded shared secret.
73
            $authKey = new Key($public_key, $secret_key);
74
            $middleware = new HmacAuthMiddleware($authKey, 'Decision');
75
            $authenticatedHandlerStack->push($middleware);
76
        }
77
78
        // Create both unauthenticated and authenticated handlers.
79 99
        $config['handler'] = $unauthenticatedHandlerStack;
80 99
        $this->unauthenticatedClient = new Client($config);
81 99
        $config['handler'] = $authenticatedHandlerStack;
82 99
        $this->authenticatedClient = new Client($config);
83 99
    }
84
85
    /**
86
     * Get a handler that adds lift account id and site id.
87
     *
88
     * @param string $account_id The Lift Web Account Identifier. Eg.: MYACCOUNT
89
     * @param string $site_id    The Lift Web Site Identifier. Eg.: my-drupal-site
90
     * @return function The handler that adds Lift account id and site id
91
     */
92
    private function getAccountAndSiteIdHandler($account_id, $site_id)
93
    {
94
        // We cannot keep references in such functions.
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 mixed the value encoded in the JSON.
122
     */
123 3
    public function ping()
124
    {
125 3
        $request = new Request('GET', '/ping');
126 3
        $response = $this->authenticatedClient->send($request);
127 3
        $body = (string) $response->getBody();
128
129 3
        return json_decode($body, true);
130
    }
131
132
    /**
133
     * Get the Slot Manager.
134
     *
135
     * @return \Acquia\LiftClient\Manager\SlotManager
136
     */
137 24
    public function getSlotManager()
138
    {
139 24
        return new SlotManager($this->authenticatedClient);
140
    }
141
142
    /**
143
     * Get the Slot Manager.
144
     *
145
     * @return \Acquia\LiftClient\Manager\GoalManager
146
     */
147 27
    public function getGoalManager()
148
    {
149 27
        return new GoalManager($this->authenticatedClient);
150
    }
151
152
    /**
153
     * Get the Segment Manager.
154
     *
155
     * @return \Acquia\LiftClient\Manager\SegmentManager
156
     */
157 6
    public function getSegmentManager()
158
    {
159 6
        return new SegmentManager($this->authenticatedClient);
160
    }
161
162
    /**
163
     * Get the Rules Manager.
164
     *
165
     * @return \Acquia\LiftClient\Manager\RuleManager
166
     */
167 24
    public function getRuleManager()
168
    {
169 24
        return new RuleManager($this->authenticatedClient);
170
    }
171
172
    /**
173
     * Get the Capture Manager.
174
     *
175
     * @return \Acquia\LiftClient\Manager\CaptureManager
176
     */
177 9
    public function getCaptureManager()
178
    {
179 9
        return new CaptureManager($this->unauthenticatedClient);
180
    }
181
182
    /**
183
     * Get the Decide Manager.
184
     *
185
     * @return \Acquia\LiftClient\Manager\DecideManager
186
     */
187 6
    public function getDecideManager()
188
    {
189 6
        return new DecideManager($this->unauthenticatedClient);
190
    }
191
192
}
193