OtpModel   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 21
c 1
b 0
f 1
dl 0
loc 73
rs 10

3 Methods

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