Completed
Push — master ( c75a1a...5e3b3c )
by François
02:10
created

AccessWatch::identityAnalyzer()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Bouncer\Analyzer;
4
5
class AccessWatch
6
{
7
8
    protected $baseUrl = 'https://access.watch/api/1.0';
9
10
    protected $apiKey;
11
12
    protected $httpClient;
13
14
    public function __construct($params)
15
    {
16
        if (isset($params['baseUrl'])) {
17
            $this->baseUrl = $params['baseUrl'];
18
        }
19
        if (isset($params['apiKey'])) {
20
            $this->apiKey = $params['apiKey'];
21
        }
22
        if (isset($params['httpClient'])) {
23
            $this->httpClient = $params['httpClient'];
24
        }
25
    }
26
27
    public function getHttpClient($apiKey = null)
28
    {
29
        if (empty($this->httpClient)) {
30
            $this->httpClient = new \Bouncer\Http\SimpleClient($apiKey);
31
        }
32
        if ($apiKey) {
33
            $this->httpClient->setApiKey($apiKey);
34
        }
35
        return $this->httpClient;
36
    }
37
38
    public function identityAnalyzer($identity)
39
    {
40
        $result = $this->getHttpClient($this->apiKey)->post(
41
            "{$this->baseUrl}/session",
42
            array(
43
                'address' => $identity->getAddress()->getValue(),
44
                'headers' => $identity->getHeaders(),
45
            )
46
        );
47
48
        if (isset($result['identity']) && is_array($result['identity'])) {
49
            $identity->setAttributes($result['identity']);
50
        }
51
52
        if (isset($result['session']) && is_array($result['session'])) {
53
            $identity->setAttribute('session', $result['session']);
54
        }
55
56
        return $identity;
57
    }
58
59
}
60