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

Identity::setSession()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Bouncer package.
5
 *
6
 * (c) François Hodierne <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bouncer\Resource;
13
14
use Bouncer\Bouncer;
15
use Bouncer\Hash;
16
use Bouncer\Resource;
17
18
class Identity extends Resource
19
{
20
21
    /**
22
     * The unique id
23
     *
24
     * @var string
25
     */
26
    protected $id;
27
28
    /**
29
     * Type: browser or robot
30
     *
31
     * @var string
32
     */
33
    protected $type;
34
35
    /**
36
     * The Address
37
     *
38
     * @var Address
39
     */
40
    protected $address;
41
42
    /**
43
     * The Signature
44
     *
45
     * @var Signature
46
     */
47
    protected $signature;
48
49
    /**
50
     * The HTTP Headers
51
     *
52
     * @var array
53
     */
54
    protected $headers;
55
56
    /**
57
     * The User Agent
58
     *
59
     * @var UserAgent
60
     */
61
    protected $userAgent;
62
63
    /**
64
     * Session
65
     *
66
     * @var Session
67
     */
68
    protected $session;
69
70
    /**
71
     * Reputation
72
     *
73
     * @var array
74
     */
75
    protected $reputation;
76
77
    public function __construct($attributes = null)
78
    {
79
        parent::__construct($attributes);
80
        $address = $this->getAddress();
81
        $signature = $this->getSignature();
82
        if ($address && $signature) {
83
            $this->id = Hash::hash($signature->getId() . $address->getId());
84
        }
85
    }
86
87
    public function getId()
88
    {
89
        return $this->id;
90
    }
91
92
    public function setId($id)
93
    {
94
        $this->id = $id;
95
    }
96
97
    public function getType()
98
    {
99
        return $this->type;
100
    }
101
102
    public function setType($type)
103
    {
104
        $this->type = $type;
105
    }
106
107
    public function getAddress()
108
    {
109
        return $this->address;
110
    }
111
112
    public function setAddress($address)
113
    {
114
        if (is_object($address)) {
115
            $this->address = $address;
116
        } elseif (is_string($address) || is_array($address)) {
117
            $this->address = new Address($address);
118
        }
119
    }
120
121
    public function getHeaders()
122
    {
123
        return $this->headers;
124
    }
125
126
    public function setHeaders($headers)
127
    {
128
        $this->headers = $headers;
129
        $signature = new Signature(array('headers' => $headers));
130
        $this->setSignature($signature);
131
    }
132
133
    public function getSignature()
134
    {
135
        return $this->signature;
136
    }
137
138
    public function setSignature($signature)
139
    {
140
        if (is_object($signature)) {
141
            $this->signature = $signature;
142
        } elseif (is_array($signature)) {
143
            $this->signature = new Signature($signature);
144
        }
145
    }
146
147
    public function getUserAgent()
148
    {
149
        return $this->userAgent;
150
    }
151
152
    public function setUserAgent($userAgent)
153
    {
154
        if (is_object($userAgent)) {
155
            $this->userAgent = $userAgent;
156
        } elseif (is_array($userAgent)) {
157
            $this->userAgent = new UserAgent($userAgent);
158
        }
159
    }
160
161
    public function getSession()
162
    {
163
        return $this->session;
164
    }
165
166
    public function setSession($session)
167
    {
168
        if (is_object($session)) {
169
            $this->session = $session;
170
        } elseif (is_array($session)) {
171
            $this->session = new Session($session);
172
        }
173
    }
174
175
    public function getReputation()
176
    {
177
        return $this->reputation;
178
    }
179
180
    public function getStatus()
181
    {
182
        $reputation = $this->getReputation();
183
        if (is_array($reputation) && array_key_exists('status', $reputation)) {
184
            return $reputation['status'];
185
        }
186
    }
187
188
    public function toArray()
189
    {
190
        $identity = array();
191
192
        if ($this->id) {
193
            $identity['id'] = $this->id;
194
        }
195
196
        return $identity;
197
    }
198
199
}
200