Completed
Push — development ( 5f50c9...3496bb )
by Ashutosh
11:44 queued 10s
created

SettingsController   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 41
eloc 185
dl 0
loc 350
rs 9.1199
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A settingsEmail() 0 8 2
A settings() 0 7 2
A settingsError() 0 8 2
A getMails() 0 48 2
A settingsSystem() 0 8 2
A postSettingsError() 0 9 2
A getActivity() 0 56 3
A checkPaymentGateway() 0 10 2
A postSettingsSystem() 0 15 3
A settingsActivity() 0 7 2
A postSettingsTemplate() 0 9 2
A getKeys() 0 9 2
A settingsTemplate() 0 9 2
A plugins() 0 3 1
A __construct() 0 7 1
A settingsMail() 0 6 2
A postSettingsEmail() 0 17 2
A postKeys() 0 9 2
A destroy() 0 46 5

How to fix   Complexity   

Complex Class

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
2
3
namespace App\Http\Controllers\Common;
4
5
use App\Model\Common\Setting;
6
use App\Model\Common\Template;
7
use App\Model\Plugin;
8
use App\User;
9
use Bugsnag;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Input;
12
use Spatie\Activitylog\Models\Activity;
13
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)
111
    {
112
        try {
113
            $set = $settings->find(1);
114
115
            return view('themes.default1.common.setting.email', compact('set'));
116
        } catch (\Exception $ex) {
117
            return redirect()->back()->with('fails', $ex->getMessage());
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)
166
    {
167
        try {
168
            $set = $settings->find(1);
169
170
            return view('themes.default1.common.setting.error-log', compact('set'));
171
        } catch (\Exception $ex) {
172
            return redirect()->back()->with('fails', $ex->getMessage());
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()
255
    {
256
        try {
257
            $email_log = \DB::table('email_log')->get();
258
259
            return\ DataTables::of($email_log)
260
             ->addColumn('checkbox', function ($model) {
261
                 return "<input type='checkbox' class='activity' value=".$model->id.' name=select[] id=check>';
262
             })
263
                           ->addColumn('date', function ($model) {
264
                               return ucfirst($model->date);
265
                           })
266
                             ->addColumn('from', function ($model) {
267
                                 $from = Markdown::convertToHtml(ucfirst($model->from));
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Common\Markdown was not found. Did you mean Markdown? If so, make sure to prefix the type with \.
Loading history...
268
269
                                 return $from;
270
                             })
271
                              ->addColumn('to', function ($model) {
272
                                  $to = Markdown::convertToHtml(ucfirst($model->to));
273
274
                                  return $to;
275
                              })
276
                             ->addColumn('cc', function ($model) {
277
                                 $cc = '--';
278
279
                                 return $cc;
280
                             })
281
282
                               ->addColumn('subject', function ($model) {
283
                                   return ucfirst($model->subject);
284
                               })
285
286
                              ->addColumn('headers', function ($model) {
287
                                  $headers = Markdown::convertToHtml(ucfirst($model->headers));
288
289
                                  return $headers;
290
                              })
291
                              ->addColumn('status', function ($model) {
292
                                  return ucfirst($model->status);
293
                              })
294
295
                            ->rawColumns(['checkbox', 'date', 'from', 'to', 'cc',
296
                                'bcc', 'subject', 'headers', 'status', ])
297
                            ->make(true);
298
        } catch (\Exception $e) {
299
            Bugsnag::notifyException($e);
300
301
            return redirect()->back()->with('fails', $e->getMessage());
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>&times;</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>&times;</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>&times;</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>&times;</button>
350
                            '.$e->getMessage().'
351
                    </div>';
352
        }
353
    }
354
355
    public function postSettingsError(Setting $settings, Request $request)
356
    {
357
        try {
358
            $setting = $settings->find(1);
359
            $setting->fill($request->input())->save();
360
361
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
362
        } catch (\Exception $ex) {
363
            return redirect()->back()->with('fails', $ex->getMessage());
364
        }
365
    }
366
}
367