Total Complexity | 82 |
Total Lines | 384 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CB 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.
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 CB, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class CB |
||
18 | { |
||
19 | |||
20 | public function getRoleByName($roleName) { |
||
21 | return $this->find("cb_roles",['name'=>$roleName]); |
||
22 | } |
||
23 | |||
24 | public function fcm() { |
||
25 | return new FCM(); |
||
26 | } |
||
27 | |||
28 | public function sidebar() { |
||
29 | return new SidebarMenus(); |
||
30 | } |
||
31 | |||
32 | public function session() { |
||
33 | return new UserSession(); |
||
34 | } |
||
35 | |||
36 | public function getDeveloperUrl($path = null) { |
||
37 | $path = ($path)?"/".trim($path,"/"):null; |
||
38 | return url(cbConfig("DEV_PATH")).$path; |
||
|
|||
39 | } |
||
40 | |||
41 | public function getProfileUrl() { |
||
42 | return $this->getAdminUrl()."/profile"; |
||
43 | } |
||
44 | |||
45 | public function getLogoutUrl() { |
||
46 | return $this->getAdminUrl()."/logout"; |
||
47 | } |
||
48 | |||
49 | public function getLoginUrl() { |
||
50 | return $this->getAdminUrl("login"); |
||
51 | } |
||
52 | |||
53 | public function getAdminUrl($path = null) { |
||
54 | $path = ($path)?"/".trim($path,"/"):null; |
||
55 | return url(cbConfig("ADMIN_PATH")).$path; |
||
56 | } |
||
57 | |||
58 | public function getAppName() { |
||
59 | return env("APP_NAME","CRUDBOOSTER"); |
||
60 | } |
||
61 | |||
62 | public function uploadBase64($value) |
||
63 | { |
||
64 | $fileData = base64_decode($value); |
||
65 | $mime_type = finfo_buffer(finfo_open(), $fileData, FILEINFO_MIME_TYPE); |
||
66 | if($mime_type) { |
||
67 | if($mime_type = explode('/', $mime_type)) { |
||
68 | $ext = $mime_type[1]; |
||
69 | if($ext) { |
||
70 | $filePath = 'uploads/'.date('Y-m'); |
||
71 | Storage::makeDirectory($filePath); |
||
72 | $filename = sha1(strRandom(5)).'.'.$ext; |
||
73 | if (Storage::put($filePath.'/'.$filename, $fileData)) { |
||
74 | self::resizeImage($filePath.'/'.$filename); |
||
75 | return $filePath.'/'.$filename; |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | return null; |
||
81 | } |
||
82 | |||
83 | public function uploadFile($name, $encrypt = true, $resize_width = 1024, $resize_height = null) |
||
84 | { |
||
85 | if (request()->hasFile($name)) { |
||
86 | |||
87 | $file = request()->file($name); |
||
88 | $ext = $file->getClientOriginalExtension(); |
||
89 | if(in_array($ext,cbConfig("UPLOAD_FILE_EXTENSION_ALLOWED"))) { |
||
90 | $filename = slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)); |
||
91 | $file_path = 'uploads/'.date('Y-m'); |
||
92 | |||
93 | //Create Directory Monthly |
||
94 | Storage::makeDirectory($file_path); |
||
95 | |||
96 | if ($encrypt == true) { |
||
97 | $filename = sha1(strRandom(5)).'.'.$ext; |
||
98 | } else { |
||
99 | $filename = slug($filename, '_').'.'.$ext; |
||
100 | } |
||
101 | |||
102 | if (Storage::putFileAs($file_path, $file, $filename)) { |
||
103 | if($resize_width || $resize_height) { |
||
104 | $this->resizeImage($file_path.'/'.$filename, $resize_width, $resize_height); |
||
105 | } |
||
106 | return $file_path.'/'.$filename; |
||
107 | } else { |
||
108 | throw new \Exception("Something went wrong, file can't upload!"); |
||
109 | } |
||
110 | }else{ |
||
111 | throw new \Exception("The file format is not allowed!"); |
||
112 | } |
||
113 | |||
114 | } else { |
||
115 | throw new \Exception("There is no file send to server!"); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | public function resizeImage($fullFilePath, $resize_width = null, $resize_height = null, $qty = 100, $thumbQty = 75) |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | public function update($table, $id, $data) |
||
162 | } |
||
163 | |||
164 | public function updateCompact($table, $id, $params) { |
||
170 | } |
||
171 | |||
172 | public function find($table, $id) |
||
185 | } |
||
186 | } |
||
187 | |||
188 | public function listAllTable() |
||
189 | { |
||
190 | return DB::connection()->getDoctrineSchemaManager()->listTableNames(); |
||
191 | } |
||
192 | |||
193 | public function findAll($table, $condition_array = []) |
||
194 | { |
||
195 | return DB::table($table)->where($condition_array)->get(); |
||
196 | } |
||
197 | |||
198 | public function redirectBack($message, $type = 'warning') |
||
199 | { |
||
200 | if (request()->ajax()) { |
||
201 | return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $_SERVER['HTTP_REFERER']]); |
||
202 | } else { |
||
203 | return redirect()->back()->withInput() |
||
204 | ->with(['message'=> $message, 'message_type'=> $type]); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | public function redirect($to, $message, $type = 'warning') |
||
209 | { |
||
210 | if (Request::ajax()) { |
||
211 | return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]); |
||
212 | } else { |
||
213 | return redirect($to)->with(['message' => $message, 'message_type' => $type]); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | |||
218 | public function getCurrentMethod() |
||
219 | { |
||
220 | $action = str_replace("App\Http\Controllers", "", Route::currentRouteAction()); |
||
221 | $atloc = strpos($action, '@') + 1; |
||
222 | $method = substr($action, $atloc); |
||
223 | |||
224 | return $method; |
||
225 | } |
||
226 | |||
227 | public function stringBetween($string, $start, $end) |
||
228 | { |
||
229 | $string = ' '.$string; |
||
230 | $ini = strpos($string, $start); |
||
231 | if ($ini == 0) { |
||
232 | return ''; |
||
233 | } |
||
234 | $ini += strlen($start); |
||
235 | $len = strpos($string, $end, $ini) - $ini; |
||
236 | |||
237 | return substr($string, $ini, $len); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param array $rules |
||
242 | * @param array $messages |
||
243 | * @throws CBValidationException |
||
244 | */ |
||
245 | public function validation($rules = [], $messages = []) |
||
261 | } |
||
262 | } |
||
263 | |||
264 | public function pk($table) |
||
267 | } |
||
268 | |||
269 | public function findPrimaryKey($table) |
||
270 | { |
||
271 | $pk = DB::getDoctrineSchemaManager()->listTableDetails($table)->getPrimaryKey(); |
||
272 | if(!$pk) { |
||
273 | return null; |
||
274 | } |
||
275 | return $pk->getColumns()[0]; |
||
276 | } |
||
277 | |||
278 | public function urlFilterColumn($key, $type, $value = '', $singleSorting = true) |
||
279 | { |
||
280 | $params = Request::all(); |
||
281 | $mainpath = trim(self::mainpath(), '/'); |
||
282 | $key = sanitizeXSS($key); |
||
283 | $type = sanitizeXSS($type); |
||
284 | $value = sanitizeXSS($value); |
||
285 | |||
286 | if ($params['filter_column'] && $singleSorting) { |
||
287 | foreach ($params['filter_column'] as $k => $filter) { |
||
288 | foreach ($filter as $t => $val) { |
||
289 | if ($t == 'sorting') { |
||
290 | unset($params['filter_column'][$k]['sorting']); |
||
291 | } |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | |||
296 | $params['filter_column'][$key][$type] = $value; |
||
297 | |||
298 | if (isset($params)) { |
||
299 | return $mainpath.'?'.http_build_query($params); |
||
300 | } else { |
||
301 | return $mainpath.'?filter_column['.$key.']['.$type.']='.$value; |
||
302 | } |
||
303 | } |
||
304 | |||
305 | |||
306 | public function getUrlParameters($exception = null) |
||
307 | { |
||
308 | $get = request()->all(); |
||
309 | $inputhtml = ''; |
||
310 | |||
311 | if ($get) { |
||
312 | if (is_array($exception)) { |
||
313 | foreach ($exception as $e) { |
||
314 | unset($get[$e]); |
||
315 | } |
||
316 | } |
||
317 | $string_parameters = http_build_query($get); |
||
318 | $string_parameters_array = explode('&', $string_parameters); |
||
319 | foreach ($string_parameters_array as $s) { |
||
320 | $part = explode('=', $s); |
||
321 | if(isset($part[0]) && isset($part[1])) { |
||
322 | $name = htmlspecialchars(urldecode($part[0])); |
||
323 | $name = strip_tags($name); |
||
324 | $value = htmlspecialchars(urldecode($part[1])); |
||
325 | $value = strip_tags($value); |
||
326 | if ($name) { |
||
327 | $inputhtml .= "<input type='hidden' name='$name' value='$value'/>\n"; |
||
328 | } |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | |||
333 | return $inputhtml; |
||
334 | } |
||
335 | |||
336 | |||
337 | public function routeGet($prefix, $controller) { |
||
338 | $alias = str_replace("@"," ", $controller); |
||
339 | $alias = ucwords($alias); |
||
340 | $alias = str_replace(" ","",$alias); |
||
341 | Route::get($prefix, ['uses' => $controller, 'as' => $alias]); |
||
342 | } |
||
343 | |||
344 | public function routePost($prefix, $controller) { |
||
349 | } |
||
350 | |||
351 | public function routeGroupBackend(callable $callback, $namespace = 'crocodicstudio\crudbooster\controllers') { |
||
357 | } |
||
358 | |||
359 | /* |
||
360 | | -------------------------------------------------------------------------------------------------------------- |
||
361 | | Alternate route for Laravel Route::controller |
||
362 | | -------------------------------------------------------------------------------------------------------------- |
||
363 | | $prefix = path of route |
||
364 | | $controller = controller name |
||
365 | | $namespace = namespace of controller (optional) |
||
366 | | |
||
367 | */ |
||
368 | public function routeController($prefix, $controller, $namespace = null) |
||
401 | |||
402 | } |
||
403 | } |
||
404 | } |
||
405 |