Complex classes like BaseRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class BaseRepository implements RepositoryContract |
||
23 | { |
||
24 | /** |
||
25 | * The IoC container instance. |
||
26 | * |
||
27 | * @var \Illuminate\Contracts\Container\Container |
||
28 | */ |
||
29 | protected $container; |
||
30 | |||
31 | /** |
||
32 | * The repository model. |
||
33 | * |
||
34 | * @var object |
||
35 | */ |
||
36 | protected $model; |
||
37 | |||
38 | /** |
||
39 | * The repository identifier. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $repositoryId; |
||
44 | |||
45 | /** |
||
46 | * Indicate if the repository cache is enabled. |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $cacheEnabled = true; |
||
51 | |||
52 | /** |
||
53 | * Indicate if the repository cache clear is enabled. |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $cacheClearEnabled = true; |
||
58 | |||
59 | /** |
||
60 | * Execute given callback and cache result set. |
||
61 | * |
||
62 | * @param string $class |
||
63 | * @param string $method |
||
64 | * @param string $hash |
||
65 | * @param \Closure $closure |
||
66 | * |
||
67 | * @return mixed |
||
68 | */ |
||
69 | protected function executeCallback($class, $method, $hash, Closure $closure) |
||
70 | { |
||
71 | $cacheKey = $class.'@'.$method.'.'.$hash; |
||
72 | $config = $this->getContainer('config')->get('rinvex.repository.cache'); |
||
73 | |||
74 | if ($this->isCacheableMethod($config, $method)) { |
||
75 | if (method_exists($this->getContainer('cache')->getStore(), 'tags')) { |
||
76 | return $config['lifetime'] === -1 |
||
77 | ? $this->getContainer('cache')->tags($this->getRepositoryId())->rememberForever($cacheKey, $closure) |
||
78 | : $this->getContainer('cache')->tags($this->getRepositoryId())->remember($cacheKey, $config['lifetime'], $closure); |
||
79 | } |
||
80 | |||
81 | // Store cache keys by mimicking cache tags |
||
82 | $this->storeCacheKeys($class, $method, $hash, $config['keys_file']); |
||
83 | |||
84 | return $config['lifetime'] === -1 |
||
85 | ? $this->getContainer('cache')->rememberForever($cacheKey, $closure) |
||
86 | : $this->getContainer('cache')->remember($cacheKey, $config['lifetime'], $closure); |
||
87 | } |
||
88 | |||
89 | return call_user_func($closure); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Set the IoC container instance. |
||
94 | * |
||
95 | * @param \Illuminate\Contracts\Container\Container $container |
||
96 | * |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function setContainer(Container $container) |
||
100 | { |
||
101 | $this->container = $container; |
||
102 | |||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Return the IoC container instance or any of it's services. |
||
108 | * |
||
109 | * @param string|null $service |
||
110 | * |
||
111 | * @return mixed |
||
112 | */ |
||
113 | public function getContainer($service = null) |
||
114 | { |
||
115 | return is_null($service) ? ($this->container ?: app()) : ($this->container[$service] ?: app($service)); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Set the repository identifier. |
||
120 | * |
||
121 | * @param string $repositoryId |
||
122 | * |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function setRepositoryId($repositoryId) |
||
131 | |||
132 | /** |
||
133 | * Get the repository identifier. |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | public function getRepositoryId() |
||
141 | |||
142 | /** |
||
143 | * Enable repository cache. |
||
144 | * |
||
145 | * @param bool $status |
||
146 | * |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function enableCache($status = true) |
||
155 | |||
156 | /** |
||
157 | * Determine if repository cache is enabled. |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function isCacheEnabled() |
||
165 | |||
166 | /** |
||
167 | * Enable repository cache clear. |
||
168 | * |
||
169 | * @param bool $status |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function enableCacheClear($status = true) |
||
179 | |||
180 | /** |
||
181 | * Determine if repository cache clear is enabled. |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function isCacheClearEnabled() |
||
189 | |||
190 | /** |
||
191 | * Forget the repository cache. |
||
192 | * |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function forgetCache() |
||
214 | |||
215 | /** |
||
216 | * Set the relationships that should be eager loaded. |
||
217 | * |
||
218 | * @param mixed $relations |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function with($relations) |
||
228 | |||
229 | /** |
||
230 | * Add an "order by" clause to the repository. |
||
231 | * |
||
232 | * @param string $column |
||
233 | * @param string $direction |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function orderBy($column, $direction = 'asc') |
||
243 | |||
244 | /** |
||
245 | * Register a new global scope. |
||
246 | * |
||
247 | * @param \Illuminate\Database\Eloquent\Scope|\Closure|string $scope |
||
248 | * @param \Closure|null $implementation |
||
249 | * |
||
250 | * @throws \InvalidArgumentException |
||
251 | * |
||
252 | * @return mixed |
||
253 | */ |
||
254 | public function addGlobalScope($scope, Closure $implementation = null) |
||
258 | |||
259 | /** |
||
260 | * Remove all or passed registered global scopes. |
||
261 | * |
||
262 | * @param array|null $scopes |
||
263 | * |
||
264 | * @return $this |
||
265 | */ |
||
266 | public function withoutGlobalScopes(array $scopes = null) |
||
272 | |||
273 | /** |
||
274 | * Dynamically pass missing static methods to the model. |
||
275 | * |
||
276 | * @param $method |
||
277 | * @param $parameters |
||
278 | * |
||
279 | * @return mixed |
||
280 | */ |
||
281 | public static function __callStatic($method, $parameters) |
||
285 | |||
286 | /** |
||
287 | * Dynamically pass missing methods to the model. |
||
288 | * |
||
289 | * @param string $method |
||
290 | * @param array $parameters |
||
291 | * |
||
292 | * @return mixed |
||
293 | */ |
||
294 | public function __call($method, $parameters) |
||
300 | |||
301 | /** |
||
302 | * Store cache keys by mimicking cache tags. |
||
303 | * |
||
304 | * @param string $class |
||
305 | * @param string $method |
||
306 | * @param string $hash |
||
307 | * @param string $file |
||
308 | * |
||
309 | * @return void |
||
310 | */ |
||
311 | protected function storeCacheKeys($class, $method, $hash, $file) |
||
320 | |||
321 | /** |
||
322 | * Flush cache keys by mimicking cache tags. |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | protected function flushCacheKeys() |
||
344 | |||
345 | /** |
||
346 | * Get cache keys file. |
||
347 | * |
||
348 | * @param string $file |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | protected function getCacheKeys($file) |
||
356 | |||
357 | /** |
||
358 | * Determine if repository method is cacheable. |
||
359 | * |
||
360 | * @param array $config |
||
361 | * @param string $method |
||
362 | * |
||
363 | * @return bool |
||
364 | */ |
||
365 | protected function isCacheableMethod($config, $method) |
||
372 | } |
||
373 |