Completed
Push — master ( 5fe85f...c42889 )
by Mike
02:51
created

Builder::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
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 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
    /**
53
     * @SuppressWarnings(PHPMD.ShortVariable)
54
     */
55 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...
56
    {
57
        $tag = str_slug(get_class($this->model));
58
        $key = "{$tag}_{$id}_" . implode('_', $columns);
59
60
        return $this->cache([$tag])
61
            ->rememberForever($key, function () use ($id, $columns) {
62
                return parent::find($id, $columns);
63
            });
64
    }
65
66 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...
67
    {
68
        $tag = str_slug(get_class($this->model));
69
        $key = "{$tag}_" . implode('_', $columns);
70
71
        return $this->cache([$tag])
72
            ->rememberForever($key, function () use ($id, $columns) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
Unused Code introduced by
The import $id is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
73
                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

73
                return parent::/** @scrutinizer ignore-call */ count($columns);
Loading history...
74
            });
75
    }
76
77 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...
78
    {
79
        $tag = str_slug(get_class($this->model));
80
        $key = "{$tag}_" . implode('_', $columns);
81
82
        return $this->cache([$tag])
83
            ->rememberForever($key, function () use ($id, $columns) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
Unused Code introduced by
The import $id is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
84
                return parent::first($columns);
85
            });
86
    }
87
88
    public function get($columns = ['*'])
89
    {
90
        $tag = str_slug(get_class($this->model));
91
        $key = "{$tag}_" . implode('_', $columns);
92
        $key .= collect($this->query->wheres)->reduce(function ($carry, $where) {
93
            $value = $where['value'] ?? implode('_', $where['values']) ?? '';
94
95
            return "{$carry}-{$where['column']}_{$value}";
96
        });
97
        $key .= '-' . implode('-', collect($this->eagerLoad)->keys()->toArray());
98
99
        return $this->cache([$tag])
100
            ->rememberForever($key, function () use ($columns) {
101
                return parent::get($columns);
102
            });
103
    }
104
}
105