TrackSpamQuery::__construct()   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 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Sfneal\Honeypot\Queries;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Cache;
8
use Sfneal\Caching\Traits\Cacheable;
9
use Sfneal\Honeypot\Models\TrackSpam;
10
use Sfneal\Queries\Query;
11
12
class TrackSpamQuery extends Query
13
{
14
    use Cacheable;
15
16
    /**
17
     * @var int
18
     */
19
    private $limit;
20
21
    /**
22
     * TrackSpamQuery constructor.
23
     *
24
     * @param  int  $limit
25
     */
26
    public function __construct(int $limit = 200)
27
    {
28
        $this->limit = $limit;
29
    }
30
31
    /**
32
     * Retrieve a Query builder.
33
     *
34
     * @return Builder
35
     */
36
    protected function builder(): Builder
37
    {
38
        return TrackSpam::query();
39
    }
40
41
    /**
42
     * Execute a DB query.
43
     *
44
     * @return Collection
45
     */
46
    public function execute(): Collection
47
    {
48
        return $this->builder()
49
            ->limit($this->limit)
50
            ->get();
51
    }
52
53
    /**
54
     * Retrieve the Query cache key.
55
     *
56
     * @return string
57
     */
58
    public function cacheKey(): string
59
    {
60
        return TrackSpam::getTableName().':recent:'.$this->limit;
61
    }
62
63
    /**
64
     * Invalidate the Query Cache for this Query.
65
     *
66
     * @return self
67
     */
68
    public function invalidateCache()
69
    {
70
        // todo: fix this
71
        Cache::forget($this->cacheKey());
72
73
        return $this;
74
    }
75
}
76