| Total Complexity | 41 |
| Total Lines | 342 |
| 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 | ]); |
||
| 126 | |||
| 127 | try { |
||
| 128 | $setting = $settings->find(1); |
||
| 129 | $setting->fill($request->input())->save(); |
||
| 130 | |||
| 131 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 132 | } catch (\Exception $ex) { |
||
| 133 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | public function settingsTemplate(Setting $settings) |
||
| 138 | { |
||
| 139 | try { |
||
| 140 | $set = $settings->find(1); |
||
| 141 | $template = new Template(); |
||
| 142 | //$templates = $template->lists('name', 'id')->toArray(); |
||
| 143 | return view('themes.default1.common.setting.template', compact('set', 'template')); |
||
| 144 | } catch (\Exception $ex) { |
||
| 145 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | public function postSettingsTemplate(Setting $settings, Request $request) |
||
| 150 | { |
||
| 151 | try { |
||
| 152 | $setting = $settings->find(1); |
||
| 153 | $setting->fill($request->input())->save(); |
||
| 154 | |||
| 155 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 156 | } catch (\Exception $ex) { |
||
| 157 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | public function settingsError(Setting $settings) |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | public function settingsActivity(Activity $activities) |
||
| 173 | { |
||
| 174 | try { |
||
| 175 | $activity = $activities->all(); |
||
| 176 | |||
| 177 | return view('themes.default1.common.Activity-Log', compact('activity')); |
||
| 178 | } catch (\Exception $ex) { |
||
| 179 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | |||
| 184 | public function settingsMail() |
||
| 185 | { |
||
| 186 | try { |
||
| 187 | return view('themes.default1.common.email-log'); |
||
| 188 | } catch (\Exception $ex) { |
||
| 189 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getActivity() |
||
| 194 | { |
||
| 195 | try { |
||
| 196 | $activity_log = Activity::select('id', 'log_name', 'description', 'subject_id', 'subject_type', 'causer_id', 'properties', 'created_at')->get(); |
||
| 197 | |||
| 198 | return\ DataTables::of($activity_log) |
||
| 199 | ->addColumn('checkbox', function ($model) { |
||
| 200 | return "<input type='checkbox' class='activity' value=".$model->id.' name=select[] id=check>'; |
||
| 201 | }) |
||
| 202 | ->addColumn('name', function ($model) { |
||
| 203 | return ucfirst($model->log_name); |
||
| 204 | }) |
||
| 205 | ->addColumn('description', function ($model) { |
||
| 206 | return ucfirst($model->description); |
||
| 207 | }) |
||
| 208 | ->addColumn('username', function ($model) { |
||
| 209 | $causer_id = $model->causer_id; |
||
| 210 | $names = User::where('id', $causer_id)->pluck('last_name', 'first_name'); |
||
| 211 | foreach ($names as $key => $value) { |
||
| 212 | $fullName = $key.' '.$value; |
||
| 213 | |||
| 214 | return $fullName; |
||
| 215 | } |
||
| 216 | }) |
||
| 217 | ->addColumn('role', function ($model) { |
||
| 218 | $causer_id = $model->causer_id; |
||
| 219 | $role = User::where('id', $causer_id)->pluck('role'); |
||
| 220 | |||
| 221 | return json_decode($role); |
||
| 222 | }) |
||
| 223 | ->addColumn('new', function ($model) { |
||
| 224 | $properties = ($model->properties); |
||
| 225 | $newEntry = $this->getNewEntry($properties, $model); |
||
| 226 | |||
| 227 | return $newEntry; |
||
| 228 | }) |
||
| 229 | ->addColumn('old', function ($model) { |
||
| 230 | $data = ($model->properties); |
||
| 231 | $oldEntry = $this->getOldEntry($data, $model); |
||
| 232 | |||
| 233 | return $oldEntry; |
||
| 234 | }) |
||
| 235 | ->addColumn('created_at', function ($model) { |
||
| 236 | $newDate = $this->getDate($model->created_at); |
||
| 237 | |||
| 238 | return $newDate; |
||
| 239 | }) |
||
| 240 | |||
| 241 | ->rawColumns(['checkbox', 'name', 'description', 'username', 'role', 'new', 'old', 'created_at']) |
||
| 242 | ->make(true); |
||
| 243 | } catch (\Exception $e) { |
||
| 244 | Bugsnag::notifyException($e); |
||
| 245 | |||
| 246 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | |||
| 251 | public function getMails() |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | public function destroy(Request $request) |
||
| 303 | { |
||
| 304 | try { |
||
| 305 | $ids = $request->input('select'); |
||
| 306 | if (!empty($ids)) { |
||
| 307 | foreach ($ids as $id) { |
||
| 308 | $activity = Activity::where('id', $id)->first(); |
||
| 309 | if ($activity) { |
||
| 310 | $activity->delete(); |
||
| 311 | } else { |
||
| 312 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 313 | <i class='fa fa-ban'></i> |
||
| 314 | |||
| 315 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */ \Lang::get('message.failed').' |
||
| 316 | |||
| 317 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 318 | './* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
||
| 319 | </div>'; |
||
| 320 | //echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 324 | <i class='fa fa-ban'></i> |
||
| 325 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.success').' |
||
| 326 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 327 | './* @scrutinizer ignore-type */ \Lang::get('message.deleted-successfully').' |
||
| 328 | </div>'; |
||
| 329 | } else { |
||
| 330 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 331 | <i class='fa fa-ban'></i> |
||
| 332 | <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 333 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 334 | './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').' |
||
| 335 | </div>'; |
||
| 336 | //echo \Lang::get('message.select-a-row'); |
||
| 337 | } |
||
| 338 | } catch (\Exception $e) { |
||
| 339 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 340 | <i class='fa fa-ban'></i> |
||
| 341 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 342 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 343 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 344 | '.$e->getMessage().' |
||
| 345 | </div>'; |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | public function postSettingsError(Setting $settings, Request $request) |
||
| 358 | } |
||
| 359 | } |
||
| 360 | } |
||
| 361 |