AccessToken::user()   A
last analyzed

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