Token::user()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Mayoz\Token;
4
5
use Illuminate\Support\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Token extends Model
9
{
10
    /**
11
     * The table associated with the model.
12
     *
13
     * @var string
14
     */
15
    protected $table = 'tokens';
16
17
    /**
18
     * The attributes that are mass assignable.
19
     *
20
     * @var array
21
     */
22
    protected $fillable = [
23
        'api_token',
24
        'expired_at',
25
    ];
26
27
    /**
28
     * The attributes that should be mutated to dates.
29
     *
30
     * @var array
31
     */
32
    protected $dates = ['expired_at'];
33
34
    /**
35
     * Set the expired timestamp value.
36
     *
37
     * @param  mixed  $value
38
     * @return void
39
     */
40
    public function setExpiredAtAttribute($value)
41
    {
42
        if (! is_null($value) && ! $value instanceof Carbon) {
43
            $value = Carbon::parse($value);
44
        }
45
46
        $this->attributes['expired_at'] = $value;
47
    }
48
49
    /**
50
     * Determine if the token is expired or not.
51
     *
52
     * @return bool
53
     */
54
    public function isExpired()
55
    {
56
        if (is_null($this->expired_at)) {
57
            return false;
58
        }
59
60
        return now()->greaterThan(new Carbon($this->expired_at));
61
    }
62
63
    /**
64
     * Determine if the token is not expired.
65
     *
66
     * @return bool
67
     */
68
    public function isNotExpired()
69
    {
70
        return ! $this->isExpired();
71
    }
72
73
    /**
74
     * Get the user that owns the token.
75
     *
76
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
77
     */
78
    public function user()
79
    {
80
        return $this->belongsTo(config('laravel-tokens.user'));
81
    }
82
}
83