| Total Complexity | 5 |
| Total Lines | 73 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 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() |
||
| 79 | } |
||
| 80 | } |
||
| 81 |