| Total Complexity | 41 |
| Total Lines | 350 |
| 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 |
||
| 14 | class SettingsController extends BaseSettingsController |
||
| 15 | { |
||
| 16 | public $apikey; |
||
| 17 | |||
| 18 | public function __construct() |
||
| 19 | { |
||
| 20 | $this->middleware('auth', ['except' => 'checkPaymentGateway']); |
||
| 21 | $this->middleware('admin', ['except' => 'checkPaymentGateway']); |
||
| 22 | |||
| 23 | $apikey = new ApiKey(); |
||
| 24 | $this->apikey = $apikey; |
||
| 25 | } |
||
| 26 | |||
| 27 | public function settings(Setting $settings) |
||
| 28 | { |
||
| 29 | if (!$settings->where('id', '1')->first()) { |
||
| 30 | $settings->create(['company' => '']); |
||
| 31 | } |
||
| 32 | |||
| 33 | return view('themes.default1.common.admin-settings'); |
||
| 34 | //return view('themes.default1.common.settings', compact('setting', 'template')); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function plugins() |
||
| 38 | { |
||
| 39 | return view('themes.default1.common.plugins'); |
||
| 40 | } |
||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | public function getKeys(ApiKey $apikeys) |
||
| 45 | |||
| 46 | { |
||
| 47 | try { |
||
| 48 | $model = $apikeys->find(1); |
||
| 49 | |||
| 50 | return view('themes.default1.common.apikey', compact('model')); |
||
| 51 | } catch (\Exception $ex) { |
||
| 52 | return redirect('/')->with('fails', $ex->getMessage()); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | public function postKeys(ApiKey $apikeys, Request $request) |
||
| 57 | { |
||
| 58 | try { |
||
| 59 | $keys = $apikeys->find(1); |
||
| 60 | $keys->fill($request->input())->save(); |
||
| 61 | |||
| 62 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 63 | } catch (\Exception $ex) { |
||
| 64 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | public static function checkPaymentGateway($currency) |
||
| 69 | { |
||
| 70 | try { |
||
| 71 | $plugins = new Plugin(); |
||
| 72 | $models = []; |
||
| 73 | $gateways = 'Razorpay'; |
||
| 74 | |||
| 75 | return $gateways; |
||
| 76 | } catch (\Exception $ex) { |
||
| 77 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | public function settingsSystem(Setting $settings) |
||
| 82 | { |
||
| 83 | try { |
||
| 84 | $set = $settings->find(1); |
||
| 85 | |||
| 86 | return view('themes.default1.common.setting.system', compact('set')); |
||
| 87 | } catch (\Exception $ex) { |
||
| 88 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | public function postSettingsSystem(Setting $settings, Request $request) |
||
| 93 | { |
||
| 94 | try { |
||
| 95 | $setting = $settings->find(1); |
||
| 96 | if ($request->hasFile('logo')) { |
||
| 97 | $name = $request->file('logo')->getClientOriginalName(); |
||
| 98 | $destinationPath = public_path('cart/img/logo'); |
||
| 99 | $request->file('logo')->move($destinationPath, $name); |
||
| 100 | $setting->logo = $name; |
||
| 101 | } |
||
| 102 | $setting->fill($request->except('password', 'logo'))->save(); |
||
| 103 | |||
| 104 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 105 | } catch (\Exception $ex) { |
||
| 106 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | public function settingsEmail(Setting $settings) |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | public function postSettingsEmail(Setting $settings, Request $request) |
||
| 122 | { |
||
| 123 | $this->validate($request, [ |
||
| 124 | 'email' => 'required', |
||
| 125 | 'password' => 'required', |
||
| 126 | 'driver' => 'required', |
||
| 127 | 'port' => 'required', |
||
| 128 | 'encryption'=> 'required', |
||
| 129 | ]); |
||
| 130 | |||
| 131 | try { |
||
| 132 | $setting = $settings->find(1); |
||
| 133 | $setting->fill($request->input())->save(); |
||
| 134 | |||
| 135 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 136 | } catch (\Exception $ex) { |
||
| 137 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | public function settingsTemplate(Setting $settings) |
||
| 142 | { |
||
| 143 | try { |
||
| 144 | $set = $settings->find(1); |
||
| 145 | $template = new Template(); |
||
| 146 | //$templates = $template->lists('name', 'id')->toArray(); |
||
| 147 | return view('themes.default1.common.setting.template', compact('set', 'template')); |
||
| 148 | } catch (\Exception $ex) { |
||
| 149 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | public function postSettingsTemplate(Setting $settings, Request $request) |
||
| 154 | { |
||
| 155 | try { |
||
| 156 | $setting = $settings->find(1); |
||
| 157 | $setting->fill($request->input())->save(); |
||
| 158 | |||
| 159 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 160 | } catch (\Exception $ex) { |
||
| 161 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | public function settingsError(Setting $settings) |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | public function settingsActivity(Activity $activities) |
||
| 177 | { |
||
| 178 | try { |
||
| 179 | $activity = $activities->all(); |
||
| 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) |
||
| 306 | { |
||
| 307 | try { |
||
| 308 | $ids = $request->input('select'); |
||
| 309 | if (!empty($ids)) { |
||
| 310 | foreach ($ids as $id) { |
||
| 311 | $activity = Activity::where('id', $id)->first(); |
||
| 312 | if ($activity) { |
||
| 313 | $activity->delete(); |
||
| 314 | } else { |
||
| 315 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 316 | <i class='fa fa-ban'></i> |
||
| 317 | |||
| 318 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 319 | /* @scrutinizer ignore-type */ \Lang::get('message.failed').' |
||
| 320 | |||
| 321 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 322 | './* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
||
| 323 | </div>'; |
||
| 324 | //echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
||
| 325 | } |
||
| 326 | } |
||
| 327 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 328 | <i class='fa fa-ban'></i> |
||
| 329 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> ' |
||
| 330 | ./* @scrutinizer ignore-type */\Lang::get('message.success').' |
||
| 331 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 332 | './* @scrutinizer ignore-type */ \Lang::get('message.deleted-successfully').' |
||
| 333 | </div>'; |
||
| 334 | } else { |
||
| 335 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 336 | <i class='fa fa-ban'></i> |
||
| 337 | <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert'). |
||
| 338 | '!</b> './* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 339 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 340 | './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').' |
||
| 341 | </div>'; |
||
| 342 | //echo \Lang::get('message.select-a-row'); |
||
| 343 | } |
||
| 344 | } catch (\Exception $e) { |
||
| 345 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 346 | <i class='fa fa-ban'></i> |
||
| 347 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 348 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 349 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 350 | '.$e->getMessage().' |
||
| 351 | </div>'; |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | public function postSettingsError(Setting $settings, Request $request) |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |
||
| 367 |