Completed
Push — develop ( bd9a33...3fa966 )
by Wisoot
02:32
created

Claim::__construct()   F

Complexity

Conditions 10
Paths 288

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 4
Metric Value
c 4
b 0
f 4
dl 0
loc 39
rs 3.1304
cc 10
eloc 20
nc 288
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace WWON\JwtGuard;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Config;
7
use WWON\JwtGuard\Exceptions\InaccessibleException;
8
use WWON\JwtGuard\Exceptions\MalformedException;
9
use WWON\JwtGuard\Exceptions\TokenExpiredException;
10
11
class Claim
12
{
13
14
    /**
15
     * subject
16
     *
17
     * @var mixed
18
     */
19
    public $sub;
20
21
    /**
22
     * issuer
23
     *
24
     * @var string
25
     */
26
    public $iss;
27
28
    /**
29
     * issued at
30
     *
31
     * @var int
32
     */
33
    public $iat;
34
35
    /**
36
     * expiration time
37
     *
38
     * @var int
39
     */
40
    public $exp;
41
42
    /**
43
     * not before
44
     *
45
     * @var int
46
     */
47
    public $nbf;
48
49
    /**
50
     * not after
51
     *
52
     * @var int
53
     */
54
    public $nat;
55
56
    /**
57
     * JWT identity
58
     *
59
     * @var string
60
     */
61
    public $jti;
62
63
    /**
64
     * leeway for using in comparing time
65
     *
66
     * @var int
67
     */
68
    public $leeway;
69
70
    /**
71
     * refreshable - whether the token can be refreshed
72
     *
73
     * @var bool
74
     */
75
    public $refresh = false;
76
77
    /**
78
     * timestamp when this object is instantiate
79
     *
80
     * @var int
81
     */
82
    protected $now;
83
84
    /**
85
     * Claim constructor
86
     *
87
     * @param array $data
88
     * @throws InaccessibleException
89
     * @throws MalformedException
90
     * @throws TokenExpiredException
91
     */
92
    public function __construct(array $data = [])
93
    {
94
        foreach ($data as $key => $value) {
95
            $attribute = camel_case($key);
96
97
            if (property_exists($this, $attribute)) {
98
                $this->{$attribute} = $value;
99
            }
100
        }
101
102
        if (empty($this->iss)) {
103
            $this->iss = Config::get('app.url');
104
        }
105
106
        if (empty($this->iat)) {
107
            $this->iat = Carbon::now()->timestamp;
108
        }
109
110
        if (empty($this->exp)) {
111
            $ttl = $this->refresh ? Config::get('jwt.refresh_ttl') : Config::get('jwt.ttl');
112
            $this->exp = $this->iat + ($ttl * 60); // turns minute into second
0 ignored issues
show
Documentation Bug introduced by
The property $exp was declared of type integer, but $this->iat + $ttl * 60 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
113
        }
114
115
        if (empty($this->nat)) {
116
            $this->nat = $this->iat + (Config::get('jwt.ttl') * 60); // turns minute into second
0 ignored issues
show
Documentation Bug introduced by
The property $nat was declared of type integer, but $this->iat + \Illuminate...ig::get('jwt.ttl') * 60 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
117
        }
118
119
        if (empty($this->jti)) {
120
            $this->jti = md5("{$this->sub}.{$this->iat}." . rand(1000, 1999));
121
        }
122
123
        if (empty($this->leeway)) {
124
            $this->leeway = Config::get('jwt.leeway');
125
        }
126
127
        $this->now = Carbon::now()->timestamp;
128
129
        $this->validate();
130
    }
131
132
    /**
133
     * validate method
134
     *
135
     * @throws InaccessibleException
136
     * @throws MalformedException
137
     * @throws TokenExpiredException
138
     */
139
    protected function validate()
140
    {
141
        $compareTime = $this->now + $this->leeway;
142
143
        if ($this->iat > $this->exp || $this->iat > $this->nat) {
144
            throw new MalformedException;
145
        }
146
147
        if ($this->exp < $compareTime) {
148
            throw new TokenExpiredException;
149
        }
150
    }
151
152
    /**
153
     * validateAccessible method
154
     *
155
     * @throws InaccessibleException
156
     */
157
    public function validateAccessible()
158
    {
159
        $compareTime = $this->now + $this->leeway;
160
161
        if ($this->nat < $compareTime) {
162
            throw new InaccessibleException;
163
        }
164
    }
165
166
    /**
167
     * toArray method
168
     *
169
     * @return array
170
     */
171
    public function toArray()
172
    {
173
        $data = [];
174
175
        if (!empty($this->sub)) {
176
            $data['sub'] = $this->sub;
177
        }
178
179
        if (!empty($this->iss)) {
180
            $data['iss'] = $this->iss;
181
        }
182
183
        if (!empty($this->iat)) {
184
            $data['iat'] = $this->iat;
185
        }
186
187
        if (!empty($this->exp)) {
188
            $data['exp'] = $this->exp;
189
        }
190
191
        if (!empty($this->nbf)) {
192
            $data['nbf'] = $this->nbf;
193
        }
194
195
        if (!empty($this->nat)) {
196
            $data['nat'] = $this->nat;
197
        }
198
199
        if (!empty($this->jti)) {
200
            $data['jti'] = $this->jti;
201
        }
202
203
        if ($this->refresh) {
204
            $data['refresh'] = true;
205
        }
206
207
        return $data;
208
    }
209
210
}