Completed
Push — develop ( 7bb86a...82de8f )
by Wisoot
02:16
created

src/Claim.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace WWON\JwtGuard;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Config;
7
8
class Claim
9
{
10
11
    /**
12
     * @var mixed
13
     */
14
    public $sub;
15
16
    /**
17
     * @var string
18
     */
19
    public $iss;
20
21
    /**
22
     * @var int
23
     */
24
    public $iat;
25
26
    /**
27
     * @var int
28
     */
29
    public $exp;
30
31
    /**
32
     * @var string
33
     */
34
    public $jti;
35
36
    /**
37
     * Claim constructor
38
     *
39
     * @param array $data
40
     */
41
    public function __construct(array $data = [])
42
    {
43
        foreach ($data as $key => $value) {
44
            $attribute = camel_case($key);
45
46
            if (property_exists($this, $attribute)) {
47
                $this->{$attribute} = $value;
48
            }
49
        }
50
51
        if (empty($this->iss)) {
52
            $this->iss = Config::get('app.url');
53
        }
54
55
        if (empty($this->iat)) {
56
            $this->iat = Carbon::now()->timestamp;
57
        }
58
59
        if (empty($this->iat)) {
60
            $this->exp = $this->iat + (Config::get('jwt.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 + \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...
61
        }
62
63
        if (empty($this->jti)) {
64
            $this->jti = md5("{$this->sub}.{$this->iat}." . rand(1000, 1999));
65
        }
66
    }
67
68
    /**
69
     * toArray method
70
     *
71
     * @return array
72
     */
73
    public function toArray()
74
    {
75
        $data = [];
76
77
        if (!empty($this->sub)) {
78
            $data['sub'] = $this->sub;
79
        }
80
81
        if (!empty($this->iss)) {
82
            $data['iss'] = $this->iss;
83
        }
84
85
        if (!empty($this->iat)) {
86
            $data['iat'] = $this->iat;
87
        }
88
89
        if (!empty($this->exp)) {
90
            $data['exp'] = $this->exp;
91
        }
92
93
        if (!empty($this->jti)) {
94
            $data['jti'] = $this->jti;
95
        }
96
97
        return $data;
98
    }
99
100
}