Otp   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeValid() 0 11 1
A useNow() 0 5 1
1
<?php
2
3
4
namespace Alish\LaravelOtp\Models;
5
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Carbon;
8
9
/**
10
 * @method static valid(string $key)
11
 * @property string token
12
 * @property Carbon revoked_at
13
 * @property Carbon used_at
14
 * @property Carbon expires_at
15
 */
16
class Otp extends Model
17
{
18
    protected $table = 'otps';
19
20
    protected $casts = [
21
        'revoked_at' => 'datetime',
22
        'used_at' => 'datetime',
23
        'expires_at' => 'datetime',
24
    ];
25
26
    public function scopeValid($query, string $key)
27
    {
28
        return $query
29
            ->where('key', $key)
30
            ->whereNull('revoked_at')
31
            ->whereNull('used_at')
32
            ->where(function ($query) {
33
                $query->whereNull('expires_at')
34
                    ->orWhere('expires_at', '>', now());
35
            });
36
    }
37
38
    public function useNow(): bool
39
    {
40
        $this->used_at = now();
41
        return $this->save();
42
    }
43
}
44