Total Complexity | 25 |
Total Lines | 160 |
Duplicated Lines | 48.13 % |
Changes | 0 |
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; |
||
9 | class CachedBuilder extends EloquentBuilder |
||
10 | { |
||
11 | use Cachable; |
||
12 | |||
13 | protected $isCachable = true; |
||
14 | |||
15 | View Code Duplication | public function avg($column) |
|
16 | { |
||
17 | if (! $this->isCachable) { |
||
18 | return parent::avg($column); |
||
|
|||
19 | } |
||
20 | |||
21 | return $this->cache($this->makeCacheTags()) |
||
22 | ->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) { |
||
23 | return parent::avg($column); |
||
24 | }); |
||
25 | } |
||
26 | |||
27 | View Code Duplication | public function count($columns = ['*']) |
|
28 | { |
||
29 | if (! $this->isCachable) { |
||
30 | return parent::count($columns); |
||
1 ignored issue
–
show
|
|||
31 | } |
||
32 | |||
33 | return $this->cache($this->makeCacheTags()) |
||
34 | ->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) { |
||
35 | return parent::count($columns); |
||
36 | }); |
||
37 | } |
||
38 | |||
39 | public function cursor() |
||
40 | { |
||
41 | if (! $this->isCachable) { |
||
42 | return collect(parent::cursor()); |
||
43 | } |
||
44 | |||
45 | return $this->cache($this->makeCacheTags()) |
||
46 | ->rememberForever($this->makeCacheKey() . "-cursor", function () { |
||
47 | return collect(parent::cursor()); |
||
48 | }); |
||
49 | } |
||
50 | |||
51 | public function delete() |
||
52 | { |
||
53 | $this->cache($this->makeCacheTags()) |
||
54 | ->flush(); |
||
55 | |||
56 | return parent::delete(); |
||
57 | } |
||
58 | |||
59 | public function disableCache() |
||
60 | { |
||
61 | $this->isCachable = false; |
||
62 | |||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @SuppressWarnings(PHPMD.ShortVariable) |
||
68 | */ |
||
69 | public function find($id, $columns = ['*']) |
||
70 | { |
||
71 | if (! $this->isCachable) { |
||
72 | return parent::find($id, $columns); |
||
73 | } |
||
74 | |||
75 | return $this->cache($this->makeCacheTags()) |
||
76 | ->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) { |
||
77 | return parent::find($id, $columns); |
||
78 | }); |
||
79 | } |
||
80 | |||
81 | View Code Duplication | public function first($columns = ['*']) |
|
82 | { |
||
83 | if (! $this->isCachable) { |
||
84 | return parent::first($columns); |
||
85 | } |
||
86 | |||
87 | return $this->cache($this->makeCacheTags()) |
||
88 | ->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) { |
||
89 | return parent::first($columns); |
||
90 | }); |
||
91 | } |
||
92 | |||
93 | View Code Duplication | public function get($columns = ['*']) |
|
94 | { |
||
95 | if (! $this->isCachable) { |
||
96 | return parent::get($columns); |
||
97 | } |
||
98 | |||
99 | return $this->cache($this->makeCacheTags()) |
||
100 | ->rememberForever($this->makeCacheKey($columns), function () use ($columns) { |
||
101 | return parent::get($columns); |
||
102 | }); |
||
103 | } |
||
104 | |||
105 | View Code Duplication | public function max($column) |
|
106 | { |
||
107 | if (! $this->isCachable) { |
||
108 | return parent::max($column); |
||
109 | } |
||
110 | |||
111 | return $this->cache($this->makeCacheTags()) |
||
112 | ->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) { |
||
113 | return parent::max($column); |
||
114 | }); |
||
115 | } |
||
116 | |||
117 | View Code Duplication | public function min($column) |
|
126 | }); |
||
127 | } |
||
128 | |||
129 | public function pluck($column, $key = null) |
||
144 | }); |
||
145 | } |
||
146 | |||
147 | View Code Duplication | public function sum($column) |
|
148 | { |
||
149 | if (! $this->isCachable) { |
||
150 | return parent::sum($column); |
||
151 | } |
||
152 | |||
153 | return $this->cache($this->makeCacheTags()) |
||
154 | ->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) { |
||
155 | return parent::sum($column); |
||
156 | }); |
||
157 | } |
||
158 | |||
159 | protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string |
||
160 | { |
||
161 | return (new CacheKey($this->eagerLoad, $this->model, $this->query)) |
||
162 | ->make($columns, $idColumn); |
||
163 | } |
||
164 | |||
165 | protected function makeCacheTags() : array |
||
169 | } |
||
170 | } |
||
171 |