JWTPayload   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 117
ccs 30
cts 30
cp 1
rs 10
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A cant() 0 3 1
A jsonSerialize() 0 3 1
A isPast() 0 3 1
A __construct() 0 3 1
A add() 0 9 3
A can() 0 4 2
A toArray() 0 3 1
A isValid() 0 3 1
A toJson() 0 3 2
A abilities() 0 5 2
A get() 0 3 1
A exp() 0 5 2
1
<?php
2
3
4
namespace JWTAuth;
5
6
use \JWTAuth\Contracts\JWTPayloadContract;
0 ignored issues
show
Bug introduced by
The type \JWTAuth\Contracts\JWTPayloadContract was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Carbon\Carbon;
8
use Illuminate\Support\Arr;
9
10
class JWTPayload implements JWTPayloadContract
11
{
12
13
    /**
14
     * JWT payload data
15
     *
16
     * @var array
17
     */
18
    protected array $payload = [];
19
20
    /**
21
     * JWTPayload constructor.
22
     *
23
     * @param array $payload
24
     */
25 7
    public function __construct(array $payload = [])
26
    {
27 7
        $this->payload = $payload;
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33 2
    public function add($key, $value = null): JWTPayloadContract
34
    {
35 2
        if (is_array($key) && count(func_get_args()) == 1) {
36 1
            $this->payload = array_merge($this->payload, $key);
37
        } else {
38 2
            $this->payload[ $key ] = $value;
39
        }
40
41 2
        return $this;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47 2
    public function abilities(): array
48
    {
49 2
        $a = $this->payload['abilities'] ?? [];
50
51 2
        return is_array($a) ? $a : [];
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 4
    public function isValid(): bool
58
    {
59 4
        return !$this->isPast();
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65 4
    public function isPast(): bool
66
    {
67 4
        return Carbon::createFromTimestampUTC($this->exp())->timezone('UTC')->isPast();
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73 4
    public function exp(): int
74
    {
75 4
        $exp = $this->payload['exp'] ?? 0;
76
77 4
        return (int) (is_numeric($exp) ? $exp : 0);
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83 2
    public function can(string $ability): bool
84
    {
85 2
        return in_array('*', $this->abilities()) ||
86 2
               array_key_exists($ability, array_flip($this->abilities()));
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92 2
    public function cant(string $ability): bool
93
    {
94 2
        return !$this->can($ability);
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100 6
    public function toArray(): array
101
    {
102 6
        return $this->payload;
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108 1
    public function toJson(int $encodeOptions = 0): string
109
    {
110 1
        return json_encode($this->toArray(), $encodeOptions) ?: '';
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116 2
    public function jsonSerialize(): mixed
117
    {
118 2
        return $this->toArray();
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124 3
    public function get(string $key, mixed $default = null): mixed
125
    {
126 3
        return Arr::get($this->payload, $key, $default);
127
    }
128
}
129