Identity   B
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 290
Duplicated Lines 0 %

Coupling/Cohesion

Components 6
Dependencies 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 41
lcom 6
cbo 6
dl 0
loc 290
rs 7.5774
c 2
b 0
f 1

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A setId() 0 4 1
A getType() 0 4 1
A setType() 0 4 1
A getAddress() 0 4 1
A setAddress() 0 8 4
A getHeaders() 0 4 1
A setHeaders() 0 6 1
A getSignature() 0 4 1
A setSignature() 0 8 3
A getUserAgent() 0 4 1
A setUserAgent() 0 8 3
A getReputation() 0 4 1
A getStatus() 0 7 3
A isSuspicious() 0 4 1
A isBad() 0 4 1
A toArray() 0 10 2
A __construct() 0 9 3
A getSession() 0 4 1
A setSession() 0 8 4
A setReputation() 0 4 1
A isNice() 0 4 1
A getAgentName() 0 7 2
A getSystemName() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like Identity often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Identity, and based on these observations, apply Extract Interface, too.

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\Resource;
16
17
class Identity extends Resource
18
{
19
20
    /**
21
     * The unique id
22
     *
23
     * @var string
24
     */
25
    protected $id;
26
27
    /**
28
     * Type: browser or robot
29
     *
30
     * @var string
31
     */
32
    protected $type;
33
34
    /**
35
     * The Address
36
     *
37
     * @var Address
38
     */
39
    protected $address;
40
41
    /**
42
     * The Signature
43
     *
44
     * @var Signature
45
     */
46
    protected $signature;
47
48
    /**
49
     * The HTTP Headers
50
     *
51
     * @var array
52
     */
53
    protected $headers;
54
55
    /**
56
     * The User Agent
57
     *
58
     * @var UserAgent
59
     */
60
    protected $userAgent;
61
62
    /**
63
     * Session
64
     *
65
     * @var Session
66
     */
67
    protected $session;
68
69
    /**
70
     * Reputation
71
     *
72
     * @var array
73
     */
74
    protected $reputation;
75
76
    /**
77
     * @param array $attributes
78
     */
79
    public function __construct($attributes = null)
80
    {
81
        parent::__construct($attributes);
82
        $address = $this->getAddress();
83
        $signature = $this->getSignature();
84
        if ($address && $signature) {
85
            $this->id = Bouncer::hash($signature->getId() . $address->getId());
86
        }
87
    }
88
89
    /*
90
     * @return string|null
91
     */
92
    public function getId()
93
    {
94
        return $this->id;
95
    }
96
97
    /*
98
     * @param string
99
     */
100
    public function setId($id)
101
    {
102
        $this->id = $id;
103
    }
104
105
    /*
106
     * @return string|null
107
     */
108
    public function getType()
109
    {
110
        return $this->type;
111
    }
112
113
    /*
114
     * @param string
115
     */
116
    public function setType($type)
117
    {
118
        $this->type = $type;
119
    }
120
121
    /*
122
     * @return Address|null
123
     */
124
    public function getAddress()
125
    {
126
        return $this->address;
127
    }
128
129
    /*
130
     * @param string|array|object $address
131
     */
132
    public function setAddress($address)
133
    {
134
        if (is_object($address)) {
135
            $this->address = $address;
136
        } elseif (is_string($address) || is_array($address)) {
137
            $this->address = new Address($address);
138
        }
139
    }
140
141
    /*
142
     * @return array
143
     */
144
    public function getHeaders()
145
    {
146
        return $this->headers;
147
    }
148
149
    /*
150
     * @param array $headers
151
     */
152
    public function setHeaders($headers)
153
    {
154
        $this->headers = $headers;
155
        $signature = new Signature(array('headers' => $headers));
156
        $this->setSignature($signature);
157
    }
158
159
    /*
160
     * @return Signature|null
161
     */
162
    public function getSignature()
163
    {
164
        return $this->signature;
165
    }
166
167
    /*
168
     * @param array|object $signature
169
     */
170
    public function setSignature($signature)
171
    {
172
        if (is_object($signature)) {
173
            $this->signature = $signature;
174
        } elseif (is_array($signature)) {
175
            $this->signature = new Signature($signature);
176
        }
177
    }
178
179
    /*
180
     * @return UserAgent|null
181
     */
182
    public function getUserAgent()
183
    {
184
        return $this->userAgent;
185
    }
186
187
    /*
188
     * @param array|object $userAgent
189
     */
190
    public function setUserAgent($userAgent)
191
    {
192
        if (is_object($userAgent)) {
193
            $this->userAgent = $userAgent;
194
        } elseif (is_array($userAgent)) {
195
            $this->userAgent = new UserAgent($userAgent);
196
        }
197
    }
198
199
    /*
200
     * @return Session|null
201
     */
202
    public function getSession()
203
    {
204
        return $this->session;
205
    }
206
207
    /*
208
     * @param string|array|object $session
209
     */
210
    public function setSession($session)
211
    {
212
        if (is_object($session)) {
213
            $this->session = $session;
214
        } elseif (is_string($session) || is_array($session)) {
215
            $this->session = new Session($session);
216
        }
217
    }
218
219
    /*
220
     * @return array|null
221
     */
222
    public function getReputation()
223
    {
224
        return $this->reputation;
225
    }
226
227
    /*
228
     * @param array $reputation
229
     */
230
    public function setReputation($reputation)
231
    {
232
        $this->reputation = $reputation;
233
    }
234
235
    /*
236
     * @return string|null
237
     */
238
    public function getStatus()
239
    {
240
        $reputation = $this->getReputation();
241
        if (is_array($reputation) && array_key_exists('status', $reputation)) {
242
            return $reputation['status'];
243
        }
244
    }
245
246
    /*
247
     * @return bool
248
     */
249
    public function isNice()
250
    {
251
        return $this->getStatus() === Bouncer::NICE;
252
    }
253
254
    /*
255
     * @return bool
256
     */
257
    public function isSuspicious()
258
    {
259
        return $this->getStatus() === Bouncer::SUSPICIOUS;
260
    }
261
262
    /*
263
     * @return bool
264
     */
265
    public function isBad()
266
    {
267
        return $this->getStatus() === Bouncer::BAD;
268
    }
269
270
    /*
271
     * @return string|null
272
     */
273
    public function getAgentName()
274
    {
275
        $userAgent = $this->getUserAgent();
276
        if ($userAgent) {
277
            return $userAgent->getAgentName();
278
        }
279
    }
280
281
    /*
282
     * @return string|null
283
     */
284
    public function getSystemName()
285
    {
286
        $userAgent = $this->getUserAgent();
287
        if ($userAgent) {
288
            return $userAgent->getSystemName();
289
        }
290
    }
291
292
    /*
293
     * @return array
294
     */
295
    public function toArray()
296
    {
297
        $identity = array();
298
299
        if ($this->id) {
300
            $identity['id'] = $this->id;
301
        }
302
303
        return $identity;
304
    }
305
306
}
307