RefreshToken   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 25
dl 0
loc 72
ccs 0
cts 6
cp 0
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A user() 0 3 1
A accessToken() 0 3 1
A isValid() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Garbuzivan\Laraveltokens\Models;
6
7
use Carbon\Carbon;
8
use Garbuzivan\Laraveltokens\Interfaces\ModelToken;
9
use Illuminate\Database\Eloquent\Factories\HasFactory;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12
use Illuminate\Database\Eloquent\Relations\MorphTo;
13
14
class RefreshToken extends Model implements ModelToken
15
{
16
    use HasFactory;
17
18
    protected $table = 'refresh_tokens';
19
20
    /**
21
     * @var string[]
22
     */
23
    protected $fillable = [
24
        'token',
25
        'user_id',
26
        'user_type',
27
        'access_token_id',
28
        'expiration',
29
    ];
30
31
    /**
32
     * @var string[]
33
     */
34
    protected $dates = [
35
        'expiration',
36
        'created_at',
37
        'updated_at',
38
    ];
39
40
    /**
41
     * The attributes that should be casted to native types.
42
     *
43
     * @var array
44
     */
45
    protected $casts = [
46
        'id' => 'integer',
47
        'token' => 'string',
48
        'user_id' => 'integer',
49
        'user_type' => 'string',
50
        'access_token_id' => 'integer',
51
        'expiration' => 'datetime',
52
    ];
53
54
    /**
55
     * Validation rules
56
     *
57
     * @var array
58
     */
59
    public static array $rules = [
60
        'token' => 'required',
61
    ];
62
63
    /**
64
     * @return MorphTo
65
     */
66
    public function user(): MorphTo
67
    {
68
        return $this->morphTo();
69
    }
70
71
    /**
72
     * @return BelongsTo
73
     */
74
    public function accessToken(): BelongsTo
75
    {
76
        return $this->belongsTo(AccessToken::class, 'access_token_id', 'id');
77
    }
78
79
    /**
80
     * Проверка валидности токена по дате
81
     * @return bool
82
     */
83
    public function isValid(): bool
84
    {
85
        return is_null($this->expiration) || $this->expiration > Carbon::now();
86
    }
87
}
88