Passed
Push — master ( 051f8c...0effca )
by Daniel
02:41
created

OtpModel::expiredAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace DanielRobert\Otp\Models;
3
4
use Carbon\Carbon;
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Factories\HasFactory;
7
8
class OtpModel extends Model
9
{
10
    use HasFactory;
11
12
    /**
13
     * The attributes that are mass assignable.
14
     *
15
     * @var array<int, string>
16
     */
17
    protected $fillable = [
18
        'identifier', 
19
        'token',
20
        'validity',
21
        'expired',
22
        'no_times_generated',
23
        'generated_at',
24
    ];
25
26
    /**
27
     * The table name.
28
     *
29
     * @var string
30
     */
31
    protected $table = 'otps';
32
33
    /**
34
     * The attributes that should be cast.
35
     *
36
     * @var array<string, string>
37
     */
38
    protected $casts = [
39
        'generated_at' => 'datetime',
40
    ];
41
42
    /**
43
     * Checks if otp is expired.
44
     *
45
     * @return bool
46
     */
47
    public function isExpired() :bool
48
    {
49
        if ($this->expired) {
50
            return true;
51
        }
52
53
        $generatedTime = $this->generated_at->addMinutes($this->validity);
54
       
55
        if (strtotime($generatedTime) >= strtotime(Carbon::now()->toDateTimeString())) {
56
            return false;
57
        }
58
        $this->expired = true;
59
        $this->save();
60
61
        return true;
62
    }
63
64
    /**
65
     * Sets the expiry date
66
     *
67
     * @return object
68
     */
69
    public function expiredAt() :object
70
    {
71
        return $this->generated_at->addMinutes($this->validity);
72
    }
73
74
    /**
75
     * Get the current connection name for the model.
76
     *
77
     * @return string
78
     */
79
    public function getConnectionName()
80
    {
81
        return config('otp.storage.database.connection');
82
    }
83
}