Completed
Push — master ( 74076e...6f8a5e )
by Mike
04:24
created

Builder::sum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php namespace GeneaLabs\LaravelModelCaching;
2
3
use Closure;
4
use Illuminate\Cache\TaggableStore;
5
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6
use Illuminate\Database\Eloquent\Relations\Pivot;
7
use Illuminate\Support\Collection;
8
use Illuminate\Database\Eloquent\Relations\Relation;
9
10
class Builder extends EloquentBuilder
11
{
12
    protected function cache(array $tags = [])
13
    {
14
        $cache = cache();
15
16
        if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
17
            $cache = $cache->tags($tags);
18
        }
19
20
        return $cache;
21
    }
22
23
    protected function getCacheKey(array $columns = ['*'], $idColumn = null) : string
24
    {
25
        $key = str_slug(get_class($this->model));
26
        $key .= $idColumn ? "_{$idColumn}" : '';
27
28
        if ($columns !== ['*']) {
29
            $key .= '_' . implode('_', $columns);
30
        }
31
32
        $key .= collect($this->query->wheres)->reduce(function ($carry, $where) {
33
            $value = $where['value'] ?? implode('_', $where['values']) ?? '';
34
35
            return "{$carry}-{$where['column']}_{$value}";
36
        });
37
38
        if (collect($this->eagerLoad)->isNotEmpty()) {
39
            $key .= '-' . implode('-', collect($this->eagerLoad)->keys()->toArray());
40
        }
41
42
        if ($this->query->offset) {
43
            $key .= "-offset_{$this->query->offset}";
44
        }
45
46
        if ($this->query->limit) {
47
            $key .= "-limit_{$this->query->limit}";
48
        }
49
50
        return $key;
51
    }
52
53
    protected function getCacheTags() : array
54
    {
55
        return collect($this->eagerLoad)->keys()
56
            ->map(function ($name) {
57
                return str_slug(get_class(
58
                    $this->model
59
                        ->{$name}()
60
                        ->getQuery()
61
                        ->model
62
                ));
63
            })
64
            ->prepend(str_slug(get_class($this->model)))
65
            ->values()
66
            ->toArray();
67
    }
68
69 View Code Duplication
    public function avg($column)
70
    {
71
        $tags = [str_slug(get_class($this->model))];
72
        $key = str_slug(get_class($this->model)) ."-avg_{$column}";
73
74
        return $this->cache($tags)
75
            ->rememberForever($key, function () use ($column) {
76
                return parent::avg($column);
77
            });
78
    }
79
80 View Code Duplication
    public function count($columns = ['*'])
81
    {
82
        $tags = [str_slug(get_class($this->model))];
83
        $key = str_slug(get_class($this->model)) ."-count";
84
85
        return $this->cache($tags)
86
            ->rememberForever($key, function () use ($columns) {
87
                return parent::count($columns);
88
            });
89
    }
90
91 View Code Duplication
    public function cursor()
92
    {
93
        $tags = [str_slug(get_class($this->model))];
94
        $key = str_slug(get_class($this->model)) ."-cursor";
95
96
        return $this->cache($tags)
97
            ->rememberForever($key, function () {
98
                return collect(parent::cursor());
99
            });
100
    }
101
102
    /**
103
     * @SuppressWarnings(PHPMD.ShortVariable)
104
     */
105 View Code Duplication
    public function find($id, $columns = ['*'])
106
    {
107
        $tags = $this->getCacheTags();
108
        $key = $this->getCacheKey($columns, $id);
109
110
        return $this->cache($tags)
111
            ->rememberForever($key, function () use ($id, $columns) {
112
                return parent::find($id, $columns);
113
            });
114
    }
115
116 View Code Duplication
    public function first($columns = ['*'])
117
    {
118
        $tags = $this->getCacheTags();
119
        $key = $this->getCacheKey($columns) . '-first';
120
121
        return $this->cache($tags)
122
            ->rememberForever($key, function () use ($columns) {
123
                return parent::first($columns);
124
            });
125
    }
126
127 View Code Duplication
    public function get($columns = ['*'])
128
    {
129
        $tags = $this->getCacheTags();
130
        $key = $this->getCacheKey($columns);
131
132
        return $this->cache($tags)
133
            ->rememberForever($key, function () use ($columns) {
134
                return parent::get($columns);
135
            });
136
    }
137
138 View Code Duplication
    public function max($column)
139
    {
140
        $tags = [str_slug(get_class($this->model))];
141
        $key = str_slug(get_class($this->model)) ."-max_{$column}";
142
143
        return $this->cache($tags)
144
            ->rememberForever($key, function () use ($column) {
145
                return parent::max($column);
146
            });
147
    }
148
149 View Code Duplication
    public function min($column)
150
    {
151
        $tags = [str_slug(get_class($this->model))];
152
        $key = str_slug(get_class($this->model)) ."-min_{$column}";
153
154
        return $this->cache($tags)
155
            ->rememberForever($key, function () use ($column) {
156
                return parent::min($column);
157
            });
158
    }
159
160
    public function pluck($column, $key = null)
161
    {
162
        $tags = $this->getCacheTags();
163
        $cacheKey = $this->getCacheKey([$column]) . "-pluck_{$column}";
164
165
        if ($key) {
166
            $cacheKey .= "_{$key}";
167
        }
168
169
        return $this->cache($tags)
170
            ->rememberForever($cacheKey, function () use ($column, $key) {
171
                return parent::pluck($column, $key);
172
            });
173
    }
174
175 View Code Duplication
    public function sum($column)
176
    {
177
        $tags = [str_slug(get_class($this->model))];
178
        $key = str_slug(get_class($this->model)) ."-sum_{$column}";
179
180
        return $this->cache($tags)
181
            ->rememberForever($key, function () use ($column) {
182
                return parent::sum($column);
183
            });
184
    }
185
}
186