Passed
Push — main ( 69451c...874877 )
by Garbuz
03:25
created

RefreshToken::accessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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