Passed
Push — master ( c2fbb6...5d6416 )
by Mike
02:47
created

Builder::cursor()   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 0
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 cacheResults(Relation $relation, array $models, string $name) : array
24
    // {
25
    //     $parentIds = implode('_', collect($models)->pluck('id')->toArray());
26
    //     $parentName = str_slug(get_class($relation->getParent()));
27
    //     $childName = str_slug(get_class($relation->getRelated()));
28
    //
29
    //     $cachedResults = $this->cache([$parentName, $childName])->rememberForever(
30
    //         "{$parentName}_{$parentIds}-{$childName}s",
31
    //         function () use ($relation, $models, $name) {
32
    //             return $relation->match(
33
    //                $relation->initRelation($models, $name),
34
    //                $relation->getEager(),
35
    //                $name
36
    //            );
37
    //         }
38
    //     );
39
    //
40
    //     return $cachedResults;
41
    // }
42
    //
43
    // protected function eagerLoadRelation(array $models, $name, Closure $constraints)
44
    // {
45
    //     $relation = $this->getRelation($name);
46
    //     $relation->addEagerConstraints($models);
47
    //     $constraints($relation);
48
    //
49
    //     return $this->cacheResults($relation, $models, $name);
50
    // }
51
52
    protected function getCacheKey(array $columns = ['*'], $ids = null) : string
53
    {
54
        $key = str_slug(get_class($this->model));
55
56
        if ($ids) {
57
            $key .= '_' . (is_array($ids)
58
                ? implode('_', $ids)
59
                : $ids);
60
        }
61
62
        if ($columns !== ['*']) {
63
            $key .= '_' . implode('_', $columns);
64
        }
65
66
        $key .= collect($this->query->wheres)->reduce(function ($carry, $where) {
67
            $value = $where['value'] ?? implode('_', $where['values']) ?? '';
68
69
            return "{$carry}-{$where['column']}_{$value}";
70
        });
71
72
        if (collect($this->eagerLoad)->isNotEmpty()) {
73
            $key .= '-' . implode('-', collect($this->eagerLoad)->keys()->toArray());
74
        }
75
76
        if ($this->query->offset) {
77
            $key .= "-offset_{$this->query->offset}";
78
        }
79
80
        if ($this->query->limit) {
81
            $key .= "-limit_{$this->query->limit}";
82
        }
83
84
        return $key;
85
    }
86
87
    protected function getCacheTags() : array
88
    {
89
        return collect($this->eagerLoad)->keys()
90
            ->map(function ($name) {
91
                return str_slug(get_class(
92
                    $this->model
93
                        ->{$name}()
94
                        ->getQuery()
95
                        ->model
96
                ));
97
            })
98
            ->prepend(str_slug(get_class($this->model)))
99
            ->values()
100
            ->toArray();
101
    }
102
103 View Code Duplication
    public function avg($column)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        $tags = [str_slug(get_class($this->model))];
106
        $key = str_slug(get_class($this->model)) ."-avg_{$column}";
107
108
        return $this->cache($tags)
109
            ->rememberForever($key, function () use ($column) {
110
                return parent::avg($column);
1 ignored issue
show
Bug introduced by
The method avg() does not exist on Illuminate\Database\Eloquent\Builder. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Builder such as GeneaLabs\LaravelModelCaching\Builder. ( Ignorable by Annotation )

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

110
                return parent::/** @scrutinizer ignore-call */ avg($column);
Loading history...
111
            });
112
    }
113
114 View Code Duplication
    public function count($columns = ['*'])
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        $tags = [str_slug(get_class($this->model))];
117
        $key = str_slug(get_class($this->model)) ."-count";
118
119
        return $this->cache($tags)
120
            ->rememberForever($key, function () use ($columns) {
121
                return parent::count($columns);
1 ignored issue
show
Bug introduced by
The method count() does not exist on Illuminate\Database\Eloquent\Builder. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Builder such as GeneaLabs\LaravelModelCaching\Builder. ( Ignorable by Annotation )

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

121
                return parent::/** @scrutinizer ignore-call */ count($columns);
Loading history...
122
            });
123
    }
124
125 View Code Duplication
    public function cursor()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $tags = [str_slug(get_class($this->model))];
128
        $key = str_slug(get_class($this->model)) ."-cursor";
129
130
        return $this->cache($tags)
131
            ->rememberForever($key, function () {
132
                return collect(parent::cursor());
133
            });
134
    }
135
136
    /**
137
     * @SuppressWarnings(PHPMD.ShortVariable)
138
     */
139 View Code Duplication
    public function find($id, $columns = ['*'])
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        $tags = $this->getCacheTags();
142
        $key = $this->getCacheKey($columns, $id);
143
144
        return $this->cache($tags)
145
            ->rememberForever($key, function () use ($id, $columns) {
146
                return parent::find($id, $columns);
147
            });
148
    }
149
150 View Code Duplication
    public function first($columns = ['*'])
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $tags = $this->getCacheTags();
153
        $key = $this->getCacheKey($columns) . '-first';
154
155
        return $this->cache($tags)
156
            ->rememberForever($key, function () use ($columns) {
157
                return parent::first($columns);
158
            });
159
    }
160
161 View Code Duplication
    public function get($columns = ['*'])
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163
        $tags = $this->getCacheTags();
164
        $key = $this->getCacheKey($columns);
165
166
        return $this->cache($tags)
167
            ->rememberForever($key, function () use ($columns) {
168
                return parent::get($columns);
169
            });
170
    }
171
172 View Code Duplication
    public function max($column)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        $tags = [str_slug(get_class($this->model))];
175
        $key = str_slug(get_class($this->model)) ."-max_{$column}";
176
177
        return $this->cache($tags)
178
            ->rememberForever($key, function () use ($column) {
179
                return parent::max($column);
1 ignored issue
show
Bug introduced by
The method max() does not exist on Illuminate\Database\Eloquent\Builder. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Builder such as GeneaLabs\LaravelModelCaching\Builder. ( Ignorable by Annotation )

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

179
                return parent::/** @scrutinizer ignore-call */ max($column);
Loading history...
180
            });
181
    }
182
183 View Code Duplication
    public function min($column)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
    {
185
        $tags = [str_slug(get_class($this->model))];
186
        $key = str_slug(get_class($this->model)) ."-min_{$column}";
187
188
        return $this->cache($tags)
189
            ->rememberForever($key, function () use ($column) {
190
                return parent::min($column);
1 ignored issue
show
Bug introduced by
The method min() does not exist on Illuminate\Database\Eloquent\Builder. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Builder such as GeneaLabs\LaravelModelCaching\Builder. ( Ignorable by Annotation )

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

190
                return parent::/** @scrutinizer ignore-call */ min($column);
Loading history...
191
            });
192
    }
193
194
    public function pluck($column, $key = null)
195
    {
196
        $tags = $this->getCacheTags();
197
        $cacheKey = $this->getCacheKey([$column]) . "-pluck_{$column}";
198
199
        if ($key) {
200
            $cacheKey .= "_{$key}";
201
        }
202
203
        return $this->cache($tags)
204
            ->rememberForever($cacheKey, function () use ($column, $key) {
205
                return parent::pluck($column, $key);
206
            });
207
    }
208
209 View Code Duplication
    public function sum($column)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
    {
211
        $tags = [str_slug(get_class($this->model))];
212
        $key = str_slug(get_class($this->model)) ."-sum_{$column}";
213
214
        return $this->cache($tags)
215
            ->rememberForever($key, function () use ($column) {
216
                return parent::sum($column);
1 ignored issue
show
Bug introduced by
The method sum() does not exist on Illuminate\Database\Eloquent\Builder. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Builder such as GeneaLabs\LaravelModelCaching\Builder. ( Ignorable by Annotation )

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

216
                return parent::/** @scrutinizer ignore-call */ sum($column);
Loading history...
217
            });
218
    }
219
}
220