Total Complexity | 40 |
Total Lines | 322 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PaymentSettingsController 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 PaymentSettingsController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class PaymentSettingsController extends Controller |
||
11 | { |
||
12 | public function getPlugin() |
||
13 | { |
||
14 | $plugins = $this->fetchConfig(); |
||
15 | |||
16 | return \DataTables::of(new Collection($plugins)) |
||
17 | // ->searchColumns('name') |
||
18 | ->addColumn('name', function ($model) { |
||
19 | return ucfirst($model['name']); |
||
20 | }) |
||
21 | ->addColumn('description', function ($model) { |
||
22 | return $model['description']; |
||
23 | }) |
||
24 | ->addColumn('author', function ($model) { |
||
25 | return ucfirst($model['author']); |
||
26 | }) |
||
27 | ->addColumn('website', function ($model) { |
||
28 | return '<a href='.$model['website'].' target=_blank>'.$model['website'].'</a>'; |
||
29 | }) |
||
30 | ->addColumn('version', function ($model) { |
||
31 | return $model['version']; |
||
32 | }) |
||
33 | ->addColumn('action', function ($model) { |
||
34 | if (array_has($model, 'path')) { |
||
35 | if ($model['status'] == 0) { |
||
36 | $activate = '<form method="post" action='.url('plugin/status/'.$model['name']).'>'.'<input type="hidden" name="_token" value='.\Session::token().'>'.' |
||
37 | <button type="submit" class="btn btn-secondary btn-sm btn-xs"'.tooltip('Activate').'<i class="fa fa-tasks" style="color:white;"></i></button></form>'; |
||
38 | $settings = ' '; |
||
39 | } else { |
||
40 | $settings = '<a href='.url($model['settings']).' class="btn btn-secondary btn-sm btn-xs"'.tooltip('Settings').'<i class="nav-icon fa fa-fw fa-cogs" style="color:white;"></i></a>'; |
||
41 | $activate = '<form method="post" action='.url('plugin/status/'.$model['name']).'>'.'<input type="hidden" name="_token" value='.\Session::token().'>'.' |
||
42 | <button type="submit" class="btn btn-secondary btn-sm btn-xs"'.tooltip('Deactivate').'<i class="fa fa-tasks" style="color:white;"></i></button></form>'; |
||
43 | } |
||
44 | |||
45 | $delete = '<a href= id=delete'.$model['name'].' class="btn btn-secondary btn-sm" data-toggle=modal data-target=#del'.$model['name']."><span style='color:white'><i class='fa fa-trash' style='color:white;'></i></span></a>" |
||
46 | ."<div class='modal fade' id=del".$model['name']."> |
||
47 | <div class='modal-dialog'> |
||
48 | <div class=modal-content> |
||
49 | <div class=modal-header> |
||
50 | <h4 class=modal-title>Delete</h4> |
||
51 | </div> |
||
52 | <div class=modal-body> |
||
53 | <p>Are you Sure ?</p> |
||
54 | |||
55 | <div class=modal-footer justify-content-between> |
||
56 | <button type=button class='btn btn-default pull-left' data-dismiss=modal id=dismis>".\Lang::get('lang.close').'</button> |
||
57 | <form method="delete" action='.url('plugin/delete/'.$model['name']).'>'.'<input type="hidden" name="_token" value='.\Session::token().'>'.' |
||
58 | <button type="submit" class="btn btn-danger">Delete</button></form> |
||
59 | |||
60 | |||
61 | </div> |
||
62 | |||
63 | |||
64 | </div> |
||
65 | </div> |
||
66 | </div> |
||
67 | </div>'; |
||
68 | $action = $settings.$activate; |
||
69 | } else { |
||
70 | $action = ''; |
||
71 | } |
||
72 | |||
73 | return $action; |
||
74 | }) |
||
75 | ->rawColumns(['name', 'description', 'author', 'website', 'version', 'action']) |
||
76 | ->make(true); |
||
77 | } |
||
78 | |||
79 | public function fetchConfig() |
||
80 | { |
||
81 | $configs = $this->readConfigs(); |
||
82 | //dd($configs); |
||
83 | $plugs = new Plugin(); |
||
84 | $fields = []; |
||
85 | $attributes = []; |
||
86 | if ($configs != 'null') { |
||
87 | foreach ($configs as $key => $config) { |
||
88 | $fields[$key] = include $config; |
||
89 | } |
||
90 | } |
||
91 | if (count($fields) > 0) { |
||
92 | foreach ($fields as $key => $field) { |
||
93 | $plug = $plugs->where('name', $field['name'])->select('path', 'status')->orderBy('name')->get()->toArray(); |
||
94 | if ($plug) { |
||
95 | foreach ($plug as $i => $value) { |
||
96 | $attributes[$key]['path'] = $plug[$i]['path']; |
||
97 | $attributes[$key]['status'] = $plug[$i]['status']; |
||
98 | } |
||
99 | } else { |
||
100 | $attributes[$key]['path'] = $field['name']; |
||
101 | $attributes[$key]['status'] = 0; |
||
102 | } |
||
103 | $attributes[$key]['name'] = $field['name']; |
||
104 | $attributes[$key]['settings'] = $field['settings']; |
||
105 | $attributes[$key]['description'] = $field['description']; |
||
106 | $attributes[$key]['website'] = $field['website']; |
||
107 | $attributes[$key]['version'] = $field['version']; |
||
108 | $attributes[$key]['author'] = $field['author']; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | return $attributes; |
||
113 | } |
||
114 | |||
115 | public function readConfigs() |
||
154 | } |
||
155 | } |
||
156 | |||
157 | public function statusPlugin($slug) |
||
158 | { |
||
159 | $plugs = new Plugin(); |
||
160 | $plug = $plugs->where('name', $slug)->first(); |
||
161 | if (! $plug) { |
||
162 | $app = base_path().DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'app.php'; |
||
163 | $str = "\n'App\\Plugins\\$slug"."\\ServiceProvider',"; |
||
164 | $line_i_am_looking_for = 102; |
||
165 | $lines = file($app, FILE_IGNORE_NEW_LINES); |
||
166 | $lines[$line_i_am_looking_for] = $str; |
||
167 | file_put_contents($app, implode("\n", $lines)); |
||
168 | $plugs->create(['name' => $slug, 'path' => $slug, 'status' => 1]); |
||
169 | |||
170 | return redirect()->back()->with('success', 'Status has changed'); |
||
171 | } |
||
172 | $status = $plug->status; |
||
173 | if ($status == 0) { |
||
174 | $plug->status = 1; |
||
175 | |||
176 | $app = base_path().DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'app.php'; |
||
177 | $str = "\n'App\\Plugins\\$slug"."\\ServiceProvider',"; |
||
178 | $line_i_am_looking_for = 102; |
||
179 | $lines = file($app, FILE_IGNORE_NEW_LINES); |
||
180 | $lines[$line_i_am_looking_for] = $str; |
||
181 | file_put_contents($app, implode("\n", $lines)); |
||
182 | } |
||
183 | if ($status == 1) { |
||
184 | $plug->status = 0; |
||
185 | /* |
||
186 | * remove service provider from app.php |
||
187 | */ |
||
188 | $str = "\n'App\\Plugins\\$slug"."\\ServiceProvider',"; |
||
189 | $path_to_file = base_path().DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'app.php'; |
||
190 | |||
191 | $file_contents = file_get_contents($path_to_file); |
||
192 | $file_contents = str_replace($str, '//', $file_contents); |
||
193 | file_put_contents($path_to_file, $file_contents); |
||
194 | } |
||
195 | $plug->save(); |
||
196 | |||
197 | return redirect()->back()->with('success', 'Status has changed'); |
||
198 | } |
||
199 | |||
200 | public function postPlugins(Request $request) |
||
201 | { |
||
202 | $v = $this->validate($request, ['plugin' => 'required|mimes:application/zip,zip,Zip']); |
||
203 | $plug = new Plugin(); |
||
204 | $file = $request->file('plugin'); |
||
205 | //dd($file); |
||
206 | $destination = app_path().DIRECTORY_SEPARATOR.'Plugins'; |
||
207 | $zipfile = $file->getRealPath(); |
||
208 | /* |
||
209 | * get the file name and remove .zip |
||
210 | */ |
||
211 | $filename2 = $file->getClientOriginalName(); |
||
212 | $filename2 = str_replace('.zip', '', $filename2); |
||
213 | $filename1 = ucfirst($file->getClientOriginalName()); |
||
214 | $filename = str_replace('.zip', '', $filename1); |
||
215 | mkdir($destination.DIRECTORY_SEPARATOR.$filename); |
||
216 | /* |
||
217 | * extract the zip file using zipper |
||
218 | */ |
||
219 | \Zipper::make($zipfile)->folder($filename2)->extractTo($destination.DIRECTORY_SEPARATOR.$filename); |
||
220 | |||
221 | $file = app_path().DIRECTORY_SEPARATOR.'Plugins'.DIRECTORY_SEPARATOR.$filename; // Plugin file path |
||
222 | |||
223 | if (file_exists($file)) { |
||
224 | $seviceporvider = $file.DIRECTORY_SEPARATOR.'ServiceProvider.php'; |
||
225 | $config = $file.DIRECTORY_SEPARATOR.'config.php'; |
||
226 | if (file_exists($seviceporvider) && file_exists($config)) { |
||
227 | /* |
||
228 | * move to faveo config |
||
229 | */ |
||
230 | $faveoconfig = config_path().DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.$filename.'.php'; |
||
231 | if ($faveoconfig) { |
||
232 | //copy($config, $faveoconfig); |
||
233 | /* |
||
234 | * write provider list in app.php line 128 |
||
235 | */ |
||
236 | $app = base_path().DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'app.php'; |
||
237 | $str = "\n\n\t\t\t'App\\Plugins\\$filename"."\\ServiceProvider',"; |
||
238 | $line_i_am_looking_for = 102; |
||
239 | $lines = file($app, FILE_IGNORE_NEW_LINES); |
||
240 | $lines[$line_i_am_looking_for] = $str; |
||
241 | file_put_contents($app, implode("\n", $lines)); |
||
242 | $plug->create(['name' => $filename, 'path' => $filename, 'status' => 1]); |
||
243 | |||
244 | return redirect()->back()->with('success', 'Installed SuccessFully'); |
||
245 | } else { |
||
246 | /* |
||
247 | * delete if the plugin hasn't config.php and ServiceProvider.php |
||
248 | */ |
||
249 | $this->deleteDirectory($file); |
||
250 | |||
251 | return redirect()->back()->with('fails', 'Their is no '.$file); |
||
252 | } |
||
253 | } else { |
||
254 | /* |
||
255 | * delete if the plugin hasn't config.php and ServiceProvider.php |
||
256 | */ |
||
257 | $this->deleteDirectory($file); |
||
258 | |||
259 | return redirect()->back()->with('fails', 'Their is no <b>config.php or ServiceProvider.php</b> '.$file); |
||
260 | } |
||
261 | } else { |
||
262 | /* |
||
263 | * delete if the plugin Name is not equal to the folder name |
||
264 | */ |
||
265 | $this->deleteDirectory($file); |
||
266 | |||
267 | return redirect()->back()->with('fails', '<b>Plugin File Path is not exist</b> '.$file); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Delete the directory. |
||
273 | * |
||
274 | * @param type $dir |
||
275 | * |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function deleteDirectory($dir) |
||
279 | { |
||
280 | if (! file_exists($dir)) { |
||
281 | return true; |
||
282 | } |
||
283 | if (! is_dir($dir)) { |
||
284 | return unlink($dir); |
||
285 | } |
||
286 | foreach (scandir($dir) as $item) { |
||
287 | if ($item == '.' || $item == '..') { |
||
288 | continue; |
||
289 | } |
||
290 | chmod($dir.DIRECTORY_SEPARATOR.$item, 0777); |
||
291 | if (! $this->deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) { |
||
292 | return false; |
||
293 | } |
||
294 | } |
||
295 | chmod($dir, 0777); |
||
296 | |||
297 | return rmdir($dir); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Reading the Filedirectory. |
||
302 | * |
||
303 | * @return type |
||
304 | */ |
||
305 | public function readPlugins() |
||
306 | { |
||
307 | $dir = app_path().DIRECTORY_SEPARATOR.'Plugins'; |
||
308 | $plugins = array_diff(scandir($dir), ['.', '..']); |
||
309 | |||
310 | return $plugins; |
||
311 | } |
||
312 | |||
313 | public function deletePlugin($slug) |
||
332 | } |
||
333 | } |
||
334 |