Passed
Push — main ( a8595a...1afaaf )
by Garbuz
03:24
created

Token   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 39
c 1
b 0
f 0
dl 0
loc 148
ccs 0
cts 35
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A loadAccessToken() 0 9 1
A load() 0 10 4
A isToken() 0 3 1
A isValid() 0 3 2
A loadTokenHeader() 0 4 1
A loadDefault() 0 8 1
A loadGlobalToken() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Garbuzivan\Laraveltokens;
6
7
use DateTime;
8
use Garbuzivan\Laraveltokens\Interfaces\ModelToken;
9
use Garbuzivan\Laraveltokens\Models\AccessToken;
10
use Garbuzivan\Laraveltokens\Models\GlobalToken;
11
use Illuminate\Contracts\Auth\Authenticatable;
12
13
class Token
14
{
15
    /**
16
     * @var string|null
17
     */
18
    public ?string $type = null;
19
20
    /**
21
     * @var int
22
     */
23
    public int $id = 0;
24
25
    /**
26
     * @var string
27
     */
28
    public string $token;
29
30
    /**
31
     * @var int
32
     */
33
    public int $user_id;
34
35
    /**
36
     * @var string|null
37
     */
38
    public ?string $user_type = null;
39
40
    /**
41
     * @var string|null
42
     */
43
    public ?string $title = null;
44
45
    /**
46
     * @var DateTime|null
47
     */
48
    public ?DateTime $expiration = null;
49
50
    /**
51
     * @var DateTime|null
52
     */
53
    public ?DateTime $last_use = null;
54
55
    /**
56
     * @var bool
57
     */
58
    public bool $is_valid = false;
59
60
    /**
61
     * @var Authenticatable|null
62
     */
63
    public ?Authenticatable $user = null;
64
65
    /**
66
     * @var array
67
     */
68
    public array $header = [];
69
70
    /**
71
     * @param ModelToken|null $modelToken
72
     *
73
     * @return $this
74
     */
75
    public function load(?ModelToken $modelToken = null): self
76
    {
77
        if (is_null($modelToken)) {
78
            return $this;
79
        } elseif ($modelToken instanceof AccessToken) {
80
            return $this->loadAccessToken($modelToken);
81
        } elseif ($modelToken instanceof GlobalToken) {
82
            return $this->loadGlobalToken($modelToken);
83
        }
84
        return $this;
85
    }
86
87
    /**
88
     * Информация полученная из токена
89
     *
90
     * @param array $header
91
     *
92
     * @return $this
93
     */
94
    public function loadTokenHeader(array $header): self
95
    {
96
        $this->header = $header;
97
        return $this;
98
    }
99
100
    /**
101
     * @param AccessToken $token
102
     *
103
     * @return $this
104
     */
105
    public function loadAccessToken(AccessToken $token): self
106
    {
107
        $this->type = AccessToken::class;
108
        $this->loadDefault($token);
109
        $this->user_id = $token->user_id;
110
        $this->user_type = $token->user_type;
111
        $this->user = $token->user;
112
        $this->is_valid = $token->isValid();
113
        return $this;
114
    }
115
116
    /**
117
     * @param GlobalToken $token
118
     *
119
     * @return $this
120
     */
121
    public function loadGlobalToken(GlobalToken $token): self
122
    {
123
        $this->type = GlobalToken::class;
124
        $this->loadDefault($token);
125
        return $this;
126
    }
127
128
    /**
129
     * @param GlobalToken $token
130
     *
131
     * @return void
132
     */
133
    public function loadDefault(ModelToken $token): void
134
    {
135
        $this->id = $token->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Garbuzivan\Laraveltokens\Interfaces\ModelToken suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
136
        $this->token = $token->token;
0 ignored issues
show
Bug introduced by
Accessing token on the interface Garbuzivan\Laraveltokens\Interfaces\ModelToken suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
137
        $this->title = $token->title;
0 ignored issues
show
Bug introduced by
Accessing title on the interface Garbuzivan\Laraveltokens\Interfaces\ModelToken suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
138
        $this->expiration = $token->expiration;
0 ignored issues
show
Bug introduced by
Accessing expiration on the interface Garbuzivan\Laraveltokens\Interfaces\ModelToken suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
139
        $this->last_use = $token->last_use;
0 ignored issues
show
Bug introduced by
Accessing last_use on the interface Garbuzivan\Laraveltokens\Interfaces\ModelToken suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
140
        $this->is_valid = $token->isValid();
0 ignored issues
show
Bug introduced by
The method isValid() does not exist on Garbuzivan\Laraveltokens\Interfaces\ModelToken. Since it exists in all sub-types, consider adding an abstract or default implementation to Garbuzivan\Laraveltokens\Interfaces\ModelToken. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
        /** @scrutinizer ignore-call */ 
141
        $this->is_valid = $token->isValid();
Loading history...
141
    }
142
143
    /**
144
     * Загружен ли токен
145
     *
146
     * @return bool
147
     */
148
    public function isToken(): bool
149
    {
150
        return !is_null($this->type);
151
    }
152
153
    /**
154
     * Проверка валидности токена по дате
155
     *
156
     * @return bool
157
     */
158
    public function isValid(): bool
159
    {
160
        return $this->isToken() && $this->is_valid;
161
    }
162
}
163