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

AccessToken::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

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 2
nc 2
nop 0
crap 6
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\MorphTo;
11
12
class AccessToken extends Model
13
{
14
    use HasFactory;
15
16
    protected $table = 'access_tokens';
17
18
    /**
19
     * @var string[]
20
     */
21
    protected $fillable = [
22
        'token',
23
        'user_id',
24
        'user_type',
25
        'title',
26
        'expiration',
27
        'last_use',
28
    ];
29
30
    /**
31
     * @var string[]
32
     */
33
    protected $dates = [
34
        'expiration',
35
        'last_use',
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
        'title' => 'string',
51
        'last_use' => '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
     * Проверка валидности токена по дате
73
     * @return bool
74
     */
75
    public function isValid(): bool
76
    {
77
        return is_null($this->expiration) || $this->expiration > Carbon::now();
78
    }
79
}
80