Completed
Push — master ( a8cb62...764ba2 )
by François
03:52
created

Identity::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
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;
13
14
class Identity extends Resource
15
{
16
17
    /**
18
     * The unique id
19
     *
20
     * @var string
21
     */
22
    protected $id;
23
24
    /**
25
     * Type: browser or robot
26
     *
27
     * @var string
28
     */
29
    protected $type;
30
31
    /**
32
     * The Address
33
     *
34
     * @var Address
35
     */
36
    protected $address;
37
38
    /**
39
     * The HTTP Headers
40
     *
41
     * @var array
42
     */
43
    protected $headers;
44
45
    /**
46
     * The User Agent
47
     *
48
     * @var UserAgent
49
     */
50
    protected $userAgent;
51
52
    /**
53
     * Reputation
54
     *
55
     * @var array
56
     */
57
    protected $reputation;
58
59
    public function __construct($attributes = null)
60
    {
61
        parent::__construct($attributes);
62
        $address = $this->getAddress();
63
        $signature = $this->getSignature();
64
        if ($address && $signature) {
65
            $this->id = Bouncer::hash($signature->getId() . $address->getId());
66
        }
67
    }
68
69
    public function getId()
70
    {
71
        return $this->id;
72
    }
73
74
    public function setId($id)
75
    {
76
        $this->id = $id;
77
    }
78
79
    public function getType()
80
    {
81
        return $this->type;
82
    }
83
84
    public function setType($type)
85
    {
86
        $this->type = $type;
87
    }
88
89
    public function getAddress()
90
    {
91
        return $this->address;
92
    }
93
94
    public function setAddress($address)
95
    {
96
        if (is_object($address)) {
97
            $this->address = $address;
98
        } elseif (is_string($address) || is_array($address)) {
99
            $this->address = new Address($address);
100
        }
101
    }
102
103
    public function getHeaders()
104
    {
105
        return $this->headers;
106
    }
107
108
    public function setHeaders($headers)
109
    {
110
        $this->headers = $headers;
111
        $signature = new Signature(array('headers' => $headers));
112
        $this->setSignature($signature);
113
    }
114
115
    public function getSignature()
116
    {
117
        return $this->signature;
0 ignored issues
show
Bug introduced by
The property signature does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
118
    }
119
120
    public function setSignature($signature)
121
    {
122
        if (is_object($signature)) {
123
            $this->signature = $signature;
124
        } elseif (is_array($signature)) {
125
            $this->signature = new Signature($signature);
126
        }
127
    }
128
129
    public function getUserAgent()
130
    {
131
        return $this->userAgent;
132
    }
133
134
    public function setUserAgent($userAgent)
135
    {
136
        if (is_object($userAgent)) {
137
            $this->userAgent = $userAgent;
138
        } elseif (is_array($userAgent)) {
139
            $this->userAgent = new UserAgent($userAgent);
140
        }
141
    }
142
143
    public function getReputation()
144
    {
145
        return $this->reputation;
146
    }
147
148
    public function getStatus()
149
    {
150
        $reputation = $this->getReputation();
151
        if (is_array($reputation) && array_key_exists('status', $reputation)) {
152
            return $reputation['status'];
153
        }
154
    }
155
156
    public function toArray()
157
    {
158
        $identity = array();
159
160
        if ($this->id) {
161
            $identity['id'] = $this->id;
162
        }
163
164
        return $identity;
165
    }
166
167
}
168