Otp::scopeValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
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