Passed
Pull Request — master (#28)
by Mike
02:21
created

CachedBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 51.09 %

Importance

Changes 0
Metric Value
dl 47
loc 92
rs 10
c 0
b 0
f 0
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A min() 5 5 1
A pluck() 0 11 2
A find() 5 5 1
A max() 5 5 1
A avg() 0 5 1
A count() 5 5 1
A first() 5 5 1
A get() 5 5 1
A sum() 5 5 1
A cursor() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace GeneaLabs\LaravelModelCaching;
2
3
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4
use GeneaLabs\LaravelModelCaching\Traits\CacheKeyable;
5
use GeneaLabs\LaravelModelCaching\Traits\CacheTagable;
6
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
7
8
class CachedBuilder extends EloquentBuilder
9
{
10
    use Cachable;
11
    use CacheKeyable;
12
    use CacheTagable;
13
14
    public function avg($column)
15
    {
16
        return $this->cache($this->makeCacheTags())
17
            ->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
18
                return parent::avg($column);
19
            });
20
    }
21
22 View Code Duplication
    public function count($columns = ['*'])
23
    {
24
        return $this->cache($this->makeCacheTags())
25
            ->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
26
                return parent::count($columns);
27
            });
28
    }
29
30
    public function cursor()
31
    {
32
        return $this->cache($this->makeCacheTags())
33
            ->rememberForever($this->makeCacheKey() . "-cursor", function () {
34
                return collect(parent::cursor());
35
            });
36
    }
37
38
    /**
39
     * @SuppressWarnings(PHPMD.ShortVariable)
40
     */
41 View Code Duplication
    public function find($id, $columns = ['*'])
42
    {
43
        return $this->cache($this->makeCacheTags())
44
            ->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
45
                return parent::find($id, $columns);
46
            });
47
    }
48
49 View Code Duplication
    public function first($columns = ['*'])
50
    {
51
        return $this->cache($this->makeCacheTags())
52
            ->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
53
                return parent::first($columns);
54
            });
55
    }
56
57 View Code Duplication
    public function get($columns = ['*'])
58
    {
59
        return $this->cache($this->makeCacheTags())
60
            ->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
61
                return parent::get($columns);
62
            });
63
    }
64
65 View Code Duplication
    public function max($column)
66
    {
67
        return $this->cache($this->makeCacheTags())
68
            ->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
69
                return parent::max($column);
70
            });
71
    }
72
73 View Code Duplication
    public function min($column)
74
    {
75
        return $this->cache($this->makeCacheTags())
76
            ->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
77
                return parent::min($column);
78
            });
79
    }
80
81
    public function pluck($column, $key = null)
82
    {
83
        $cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
84
85
        if ($key) {
86
            $cacheKey .= "_{$key}";
87
        }
88
89
        return $this->cache($this->makeCacheTags())
90
            ->rememberForever($cacheKey, function () use ($column, $key) {
91
                return parent::pluck($column, $key);
92
            });
93
    }
94
95 View Code Duplication
    public function sum($column)
96
    {
97
        return $this->cache($this->makeCacheTags())
98
            ->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
99
                return parent::sum($column);
100
            });
101
    }
102
}
103