DatabaseOtpsRepository::table()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace DanielRobert\Otp\Storage;
4
5
use DateTimeInterface;
6
use Illuminate\Support\Facades\DB;
7
use DanielRobert\Otp\Contracts\ClearableRepository;
8
use DanielRobert\Otp\Contracts\PrunableRepository;
9
10
class DatabaseOtpsRepository implements ClearableRepository, PrunableRepository
11
{
12
13
    /**
14
     * The database connection name that should be used.
15
     *
16
     * @var string
17
     */
18
    protected $connection;
19
20
    /**
21
     * Create a new database repository.
22
     *
23
     * @param  string  $connection
24
     * @return void
25
     */
26
    public function __construct(string $connection)
27
    {
28
        $this->connection = $connection;
29
    }
30
31
    /**
32
     * Prune all of the entries older than the given date.
33
     *
34
     * @param  \DateTimeInterface  $before
35
     * @return int
36
     */
37
    public function prune(DateTimeInterface $before)
38
    {
39
        $query = $this->table('otps')->where('expired', true)->orWhere('created_at', '<', $before);
40
41
        $totalDeleted = 0;
42
43
        do {
44
            $deleted = $query->take(100)->delete();
45
46
            $totalDeleted += $deleted;
47
        } while ($deleted !== 0);
48
49
        return $totalDeleted;
50
    }
51
52
    /**
53
     * Clear all the entries.
54
     *
55
     * @return void
56
     */
57
    public function clear()
58
    {
59
        $this->table('otps')->delete();
60
    }
61
62
    /**
63
     * Get a query builder instance for the given table.
64
     *
65
     * @param  string  $table
66
     * @return \Illuminate\Database\Query\Builder
67
     */
68
    protected function table($table)
69
    {
70
        return DB::connection($this->connection)->table($table);
71
    }
72
}
73