Completed
Push — master ( b0dccf...a37019 )
by Mike
10s
created

CachedBuilder::makeCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php namespace GeneaLabs\LaravelModelCaching;
2
3
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
5
6
/**
7
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
8
 */
9
class CachedBuilder extends EloquentBuilder
10
{
11
    use Cachable;
12
13
    public function avg($column)
14
    {
15
        if (! $this->isCachable) {
16
            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

16
            return parent::/** @scrutinizer ignore-call */ avg($column);
Loading history...
17
        }
18
19
        return $this->cache($this->makeCacheTags())
20
            ->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
21
                return parent::avg($column);
22
            });
23
    }
24
25
    public function count($columns = ['*'])
26
    {
27
        if (! $this->isCachable) {
28
            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

28
            return parent::/** @scrutinizer ignore-call */ count($columns);
Loading history...
29
        }
30
31
        return $this->cache($this->makeCacheTags())
32
            ->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
33
                return parent::count($columns);
34
            });
35
    }
36
37
    public function cursor()
38
    {
39
        if (! $this->isCachable) {
40
            return collect(parent::cursor());
41
        }
42
43
        return $this->cache($this->makeCacheTags())
44
            ->rememberForever($this->makeCacheKey() . "-cursor", function () {
45
                return collect(parent::cursor());
46
            });
47
    }
48
49
    public function delete()
50
    {
51
        $this->cache($this->makeCacheTags())
52
            ->flush();
53
54
        return parent::delete();
55
    }
56
57
    /**
58
     * @SuppressWarnings(PHPMD.ShortVariable)
59
     */
60
    public function find($id, $columns = ['*'])
61
    {
62
        if (! $this->isCachable) {
63
            return parent::find($id, $columns);
64
        }
65
66
        return $this->cache($this->makeCacheTags())
67
            ->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
68
                return parent::find($id, $columns);
69
            });
70
    }
71
72
    public function first($columns = ['*'])
73
    {
74
        if (! $this->isCachable) {
75
            return parent::first($columns);
76
        }
77
78
        return $this->cache($this->makeCacheTags())
79
            ->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
80
                return parent::first($columns);
81
            });
82
    }
83
84
    public function get($columns = ['*'])
85
    {
86
        if (! $this->isCachable) {
87
            return parent::get($columns);
88
        }
89
90
        return $this->cache($this->makeCacheTags())
91
            ->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
92
                return parent::get($columns);
93
            });
94
    }
95
96
    public function max($column)
97
    {
98
        if (! $this->isCachable) {
99
            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

99
            return parent::/** @scrutinizer ignore-call */ max($column);
Loading history...
100
        }
101
102
        return $this->cache($this->makeCacheTags())
103
            ->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
104
                return parent::max($column);
105
            });
106
    }
107
108
    public function min($column)
109
    {
110
        if (! $this->isCachable) {
111
            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

111
            return parent::/** @scrutinizer ignore-call */ min($column);
Loading history...
112
        }
113
114
        return $this->cache($this->makeCacheTags())
115
            ->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
116
                return parent::min($column);
117
            });
118
    }
119
120
    public function pluck($column, $key = null)
121
    {
122
        if (! $this->isCachable) {
123
            return parent::pluck($column, $key);
124
        }
125
126
        $cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
127
128
        if ($key) {
129
            $cacheKey .= "_{$key}";
130
        }
131
132
        return $this->cache($this->makeCacheTags())
133
            ->rememberForever($cacheKey, function () use ($column, $key) {
134
                return parent::pluck($column, $key);
135
            });
136
    }
137
138
    public function sum($column)
139
    {
140
        if (! $this->isCachable) {
141
            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

141
            return parent::/** @scrutinizer ignore-call */ sum($column);
Loading history...
142
        }
143
144
        return $this->cache($this->makeCacheTags())
145
            ->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
146
                return parent::sum($column);
147
            });
148
    }
149
}
150