Completed
Push — master ( 0cf675...db70b3 )
by Mike
12s
created

CachedBuilder::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 5
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php namespace GeneaLabs\LaravelModelCaching;
2
3
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
5
6
class CachedBuilder extends EloquentBuilder
7
{
8
    use Cachable;
9
10 View Code Duplication
    public function avg($column)
11
    {
12
        return $this->cache($this->makeCacheTags())
13
            ->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
14
                return parent::avg($column);
0 ignored issues
show
introduced by
The method avg() does not exist on Illuminate\Database\Eloquent\Builder. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
                return parent::/** @scrutinizer ignore-call */ avg($column);
Loading history...
15
            });
16
    }
17
18 View Code Duplication
    public function count($columns = ['*'])
19
    {
20
        return $this->cache($this->makeCacheTags())
21
            ->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
22
                return parent::count($columns);
1 ignored issue
show
introduced by
The method count() does not exist on Illuminate\Database\Eloquent\Builder. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
                return parent::/** @scrutinizer ignore-call */ count($columns);
Loading history...
23
            });
24
    }
25
26
    public function cursor()
27
    {
28
        return $this->cache($this->makeCacheTags())
29
            ->rememberForever($this->makeCacheKey() . "-cursor", function () {
30
                return collect(parent::cursor());
31
            });
32
    }
33
34
    public function delete()
35
    {
36
        $this->cache($this->makeCacheTags())
37
            ->flush();
38
39
        return parent::delete();
40
    }
41
42
    /**
43
     * @SuppressWarnings(PHPMD.ShortVariable)
44
     */
45 View Code Duplication
    public function find($id, $columns = ['*'])
46
    {
47
        return $this->cache($this->makeCacheTags())
48
            ->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
49
                return parent::find($id, $columns);
50
            });
51
    }
52
53 View Code Duplication
    public function first($columns = ['*'])
54
    {
55
        return $this->cache($this->makeCacheTags())
56
            ->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
57
                return parent::first($columns);
58
            });
59
    }
60
61 View Code Duplication
    public function get($columns = ['*'])
62
    {
63
        return $this->cache($this->makeCacheTags())
64
            ->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
65
                return parent::get($columns);
66
            });
67
    }
68
69 View Code Duplication
    public function max($column)
70
    {
71
        return $this->cache($this->makeCacheTags())
72
            ->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
73
                return parent::max($column);
0 ignored issues
show
introduced by
The method max() does not exist on Illuminate\Database\Eloquent\Builder. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
                return parent::/** @scrutinizer ignore-call */ max($column);
Loading history...
74
            });
75
    }
76
77 View Code Duplication
    public function min($column)
78
    {
79
        return $this->cache($this->makeCacheTags())
80
            ->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
81
                return parent::min($column);
0 ignored issues
show
introduced by
The method min() does not exist on Illuminate\Database\Eloquent\Builder. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
                return parent::/** @scrutinizer ignore-call */ min($column);
Loading history...
82
            });
83
    }
84
85
    public function pluck($column, $key = null)
86
    {
87
        $cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
88
89
        if ($key) {
90
            $cacheKey .= "_{$key}";
91
        }
92
93
        return $this->cache($this->makeCacheTags())
94
            ->rememberForever($cacheKey, function () use ($column, $key) {
95
                return parent::pluck($column, $key);
96
            });
97
    }
98
99
    public function sum($column)
100
    {
101
        return $this->cache($this->makeCacheTags())
102
            ->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
103
                return parent::sum($column);
0 ignored issues
show
introduced by
The method sum() does not exist on Illuminate\Database\Eloquent\Builder. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
                return parent::/** @scrutinizer ignore-call */ sum($column);
Loading history...
104
            });
105
    }
106
107
    protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
108
    {
109
        return (new CacheKey($this->eagerLoad, $this->model, $this->query))
110
            ->make($columns, $idColumn);
111
    }
112
113
    protected function makeCacheTags() : array
114
    {
115
        return (new CacheTags($this->eagerLoad, $this->model))
116
            ->make();
117
    }
118
}
119