| Total Complexity | 46 |
| Total Lines | 398 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SettingsController 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 SettingsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class SettingsController extends BaseSettingsController |
||
| 17 | { |
||
| 18 | public $apikey; |
||
| 19 | |||
| 20 | public function __construct() |
||
| 21 | { |
||
| 22 | $this->middleware('auth', ['except' => 'checkPaymentGateway']); |
||
| 23 | $this->middleware('admin', ['except' => 'checkPaymentGateway']); |
||
| 24 | |||
| 25 | $apikey = new ApiKey(); |
||
| 26 | $this->apikey = $apikey; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function settings(Setting $settings) |
||
| 30 | { |
||
| 31 | if (!$settings->where('id', '1')->first()) { |
||
| 32 | $settings->create(['company' => '']); |
||
| 33 | } |
||
| 34 | |||
| 35 | return view('themes.default1.common.admin-settings'); |
||
| 36 | //return view('themes.default1.common.settings', compact('setting', 'template')); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function plugins() |
||
| 40 | { |
||
| 41 | return view('themes.default1.common.plugins'); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function getKeys(ApiKey $apikeys) |
||
| 45 | { |
||
| 46 | try { |
||
| 47 | $model = $apikeys->find(1); |
||
| 48 | |||
| 49 | return view('themes.default1.common.apikey', compact('model')); |
||
| 50 | } catch (\Exception $ex) { |
||
| 51 | return redirect('/')->with('fails', $ex->getMessage()); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | public function postKeys(ApiKey $apikeys, Request $request) |
||
| 56 | { |
||
| 57 | try { |
||
| 58 | $keys = $apikeys->find(1); |
||
| 59 | $keys->fill($request->input())->save(); |
||
| 60 | |||
| 61 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 62 | } catch (\Exception $ex) { |
||
| 63 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | public static function checkPaymentGateway($currency) |
||
| 68 | { |
||
| 69 | try { |
||
| 70 | $plugins = new Plugin(); |
||
| 71 | $models = []; |
||
| 72 | $gateways = 'Razorpay'; |
||
| 73 | |||
| 74 | return $gateways; |
||
| 75 | } catch (\Exception $ex) { |
||
| 76 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | public function settingsSystem(Setting $settings) |
||
| 81 | { |
||
| 82 | try { |
||
| 83 | $set = $settings->find(1); |
||
| 84 | |||
| 85 | return view('themes.default1.common.setting.system', compact('set')); |
||
| 86 | } catch (\Exception $ex) { |
||
| 87 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | public function postSettingsSystem(Setting $settings, Request $request) |
||
| 92 | { |
||
| 93 | try { |
||
| 94 | $setting = $settings->find(1); |
||
| 95 | if ($request->hasFile('logo')) { |
||
| 96 | $name = $request->file('logo')->getClientOriginalName(); |
||
| 97 | $destinationPath = public_path('cart/img/logo'); |
||
| 98 | $request->file('logo')->move($destinationPath, $name); |
||
| 99 | $setting->logo = $name; |
||
| 100 | } |
||
| 101 | $setting->fill($request->except('password', 'logo'))->save(); |
||
| 102 | |||
| 103 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 104 | } catch (\Exception $ex) { |
||
| 105 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | public function settingsEmail(Setting $settings) |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | public function postSettingsEmail(Setting $settings, Request $request) |
||
| 121 | { |
||
| 122 | $this->validate($request, [ |
||
| 123 | 'email' => 'required', |
||
| 124 | 'password' => 'required', |
||
| 125 | 'driver' => 'required', |
||
| 126 | 'port' => 'required', |
||
| 127 | 'encryption'=> 'required', |
||
| 128 | ]); |
||
| 129 | |||
| 130 | try { |
||
| 131 | $setting = $settings->find(1); |
||
| 132 | $setting->fill($request->input())->save(); |
||
| 133 | |||
| 134 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 135 | } catch (\Exception $ex) { |
||
| 136 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | public function settingsTemplate(Setting $settings) |
||
| 141 | { |
||
| 142 | try { |
||
| 143 | $set = $settings->find(1); |
||
| 144 | $template = new Template(); |
||
| 145 | //$templates = $template->lists('name', 'id')->toArray(); |
||
| 146 | return view('themes.default1.common.setting.template', compact('set', 'template')); |
||
| 147 | } catch (\Exception $ex) { |
||
| 148 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | public function postSettingsTemplate(Setting $settings, Request $request) |
||
| 153 | { |
||
| 154 | try { |
||
| 155 | $setting = $settings->find(1); |
||
| 156 | $setting->fill($request->input())->save(); |
||
| 157 | |||
| 158 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 159 | } catch (\Exception $ex) { |
||
| 160 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | public function settingsError(Setting $settings) |
||
| 165 | { |
||
| 166 | try { |
||
| 167 | $set = $settings->find(1); |
||
| 168 | |||
| 169 | return view('themes.default1.common.setting.error-log', compact('set')); |
||
| 170 | } catch (\Exception $ex) { |
||
| 171 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | public function settingsActivity(Activity $activities) |
||
| 176 | { |
||
| 177 | try { |
||
| 178 | $activity = $activities->all(); |
||
| 179 | |||
| 180 | return view('themes.default1.common.Activity-Log', compact('activity')); |
||
| 181 | } catch (\Exception $ex) { |
||
| 182 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | public function settingsMail() |
||
| 187 | { |
||
| 188 | try { |
||
| 189 | return view('themes.default1.common.email-log'); |
||
| 190 | } catch (\Exception $ex) { |
||
| 191 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | public function getActivity() |
||
| 196 | { |
||
| 197 | try { |
||
| 198 | $activity_log = Activity::select('id', 'log_name', 'description', |
||
| 199 | 'subject_id', 'subject_type', 'causer_id', 'properties', 'created_at')->get(); |
||
| 200 | |||
| 201 | return\ DataTables::of($activity_log) |
||
| 202 | ->addColumn('checkbox', function ($model) { |
||
| 203 | return "<input type='checkbox' class='activity' value=".$model->id.' name=select[] id=check>'; |
||
| 204 | }) |
||
| 205 | ->addColumn('name', function ($model) { |
||
| 206 | return ucfirst($model->log_name); |
||
| 207 | }) |
||
| 208 | ->addColumn('description', function ($model) { |
||
| 209 | return ucfirst($model->description); |
||
| 210 | }) |
||
| 211 | ->addColumn('username', function ($model) { |
||
| 212 | $causer_id = $model->causer_id; |
||
| 213 | $names = User::where('id', $causer_id)->pluck('last_name', 'first_name'); |
||
| 214 | foreach ($names as $key => $value) { |
||
| 215 | $fullName = $key.' '.$value; |
||
| 216 | |||
| 217 | return $fullName; |
||
| 218 | } |
||
| 219 | }) |
||
| 220 | ->addColumn('role', function ($model) { |
||
| 221 | $causer_id = $model->causer_id; |
||
| 222 | $role = User::where('id', $causer_id)->pluck('role'); |
||
| 223 | |||
| 224 | return json_decode($role); |
||
| 225 | }) |
||
| 226 | ->addColumn('new', function ($model) { |
||
| 227 | $properties = ($model->properties); |
||
| 228 | $newEntry = $this->getNewEntry($properties, $model); |
||
| 229 | |||
| 230 | return $newEntry; |
||
| 231 | }) |
||
| 232 | ->addColumn('old', function ($model) { |
||
| 233 | $data = ($model->properties); |
||
| 234 | $oldEntry = $this->getOldEntry($data, $model); |
||
| 235 | |||
| 236 | return $oldEntry; |
||
| 237 | }) |
||
| 238 | ->addColumn('created_at', function ($model) { |
||
| 239 | $newDate = $this->getDate($model->created_at); |
||
| 240 | |||
| 241 | return $newDate; |
||
| 242 | }) |
||
| 243 | |||
| 244 | ->rawColumns(['checkbox', 'name', 'description', |
||
| 245 | 'username', 'role', 'new', 'old', 'created_at', ]) |
||
| 246 | ->make(true); |
||
| 247 | } catch (\Exception $e) { |
||
| 248 | Bugsnag::notifyException($e); |
||
| 249 | |||
| 250 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | public function getMails() |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | public function destroy(Request $request) |
||
| 351 | </div>'; |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | public function destroyEmail(Request $request) |
||
| 356 | { |
||
| 357 | try { |
||
| 358 | $ids = $request->input('select'); |
||
| 359 | if (!empty($ids)) { |
||
| 360 | foreach ($ids as $id) { |
||
| 361 | $email = \DB::table('email_log')->where('id', $id)->delete(); |
||
| 362 | if ($email) { |
||
| 363 | // $email->delete(); |
||
| 364 | } else { |
||
| 365 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 366 | <i class='fa fa-ban'></i> |
||
| 367 | |||
| 368 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 369 | /* @scrutinizer ignore-type */ \Lang::get('message.failed').' |
||
| 370 | |||
| 371 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 372 | './* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
||
| 373 | </div>'; |
||
| 374 | //echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 378 | <i class='fa fa-ban'></i> |
||
| 379 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> ' |
||
| 380 | ./* @scrutinizer ignore-type */\Lang::get('message.success').' |
||
| 381 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 382 | './* @scrutinizer ignore-type */ \Lang::get('message.deleted-successfully').' |
||
| 383 | </div>'; |
||
| 384 | } else { |
||
| 385 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 386 | <i class='fa fa-ban'></i> |
||
| 387 | <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert'). |
||
| 388 | '!</b> './* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 389 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 390 | './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').' |
||
| 391 | </div>'; |
||
| 392 | //echo \Lang::get('message.select-a-row'); |
||
| 393 | } |
||
| 394 | } catch (\Exception $e) { |
||
| 395 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 396 | <i class='fa fa-ban'></i> |
||
| 397 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 398 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 399 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 400 | '.$e->getMessage().' |
||
| 401 | </div>'; |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | public function postSettingsError(Setting $settings, Request $request) |
||
| 414 | } |
||
| 415 | } |
||
| 417 |