Total Complexity | 5 |
Total Lines | 74 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
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() |
||
82 | } |
||
83 | } |