Completed
Push — development ( 75522b...9443e0 )
by Ashutosh
09:07
created

SettingsController::readConfigs()   B

Complexity

Conditions 11
Paths 44

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 39
rs 7.3166
c 0
b 0
f 0
cc 11
nc 44
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use App\ApiKey;
6
use App\Http\Controllers\Controller;
7
use App\Model\Common\Setting;
8
use App\Model\Common\Template;
9
use App\Model\Plugin;
10
use App\User;
11
use Bugsnag;
12
use DateTime;
13
use DateTimeZone;
14
use Illuminate\Http\Request;
15
use Illuminate\Support\Collection;
16
use Illuminate\Support\Facades\Input;
17
use Spatie\Activitylog\Models\Activity;
18
19
class SettingsController extends BaseSettingsController
20
{
21
    public $apikey;
22
23
    public function __construct()
24
    {
25
        $this->middleware('auth', ['except' => 'checkPaymentGateway']);
26
        $this->middleware('admin', ['except' => 'checkPaymentGateway']);
27
28
        $apikey = new ApiKey();
29
        $this->apikey = $apikey;
30
    }
31
32
    public function settings(Setting $settings)
33
    {
34
        if (!$settings->where('id', '1')->first()) {
35
            $settings->create(['company' => '']);
36
        }
37
38
        return view('themes.default1.common.admin-settings');
39
        //return view('themes.default1.common.settings', compact('setting', 'template'));
40
    }
41
42
    public function plugins()
43
    {
44
        return view('themes.default1.common.plugins');
45
    }
46
47
    public function getKeys(ApiKey $apikeys)
48
    {
49
        try {
50
            $model = $apikeys->find(1);
51
            return view('themes.default1.common.apikey', compact('model'));
52
        } catch (\Exception $ex) {
53
            return redirect('/')->with('fails', $ex->getMessage());
54
        }
55
    }
56
57
    public function postKeys(ApiKey $apikeys, Request $request)
58
    {
59
        try {
60
            $keys = $apikeys->find(1);
61
            $keys->fill($request->input())->save();
62
63
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
64
        } catch (\Exception $ex) {
65
            return redirect()->back()->with('fails', $ex->getMessage());
66
        }
67
    }
68
69
70
    public static function checkPaymentGateway($currency)
71
    {
72
        try {
73
            $plugins = new Plugin();
74
            $models = [];
75
            $gateways = 'Razorpay';
76
            return $gateways;
77
        } catch (\Exception $ex) {
78
            return redirect()->back()->with('fails', $ex->getMessage());
79
        }
80
    }
81
82
    public function settingsSystem(Setting $settings)
83
    {
84
        try {
85
            $set = $settings->find(1);
86
87
            return view('themes.default1.common.setting.system', compact('set'));
88
        } catch (\Exception $ex) {
89
            return redirect()->back()->with('fails', $ex->getMessage());
90
        }
91
    }
92
93
    public function postSettingsSystem(Setting $settings, Request $request)
94
    {
95
        try {
96
            $setting = $settings->find(1);
97
            if ($request->hasFile('logo')) {
98
                $name = $request->file('logo')->getClientOriginalName();
99
                $destinationPath = public_path('cart/img/logo');
100
                $request->file('logo')->move($destinationPath, $name);
101
                $setting->logo = $name;
102
            }
103
            $setting->fill($request->except('password', 'logo'))->save();
104
105
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
106
        } catch (\Exception $ex) {
107
            return redirect()->back()->with('fails', $ex->getMessage());
108
        }
109
    }
110
111
    public function settingsEmail(Setting $settings)
112
    {
113
        try {
114
            $set = $settings->find(1);
115
116
            return view('themes.default1.common.setting.email', compact('set'));
117
        } catch (\Exception $ex) {
118
            return redirect()->back()->with('fails', $ex->getMessage());
119
        }
120
    }
121
122
    public function postSettingsEmail(Setting $settings, Request $request)
123
    {
124
        $this->validate($request, [
125
                'email'    => 'required',
126
                'password' => 'required',
127
            ]);
128
129
        try {
130
            $setting = $settings->find(1);
131
            $setting->fill($request->input())->save();
132
133
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
134
        } catch (\Exception $ex) {
135
            return redirect()->back()->with('fails', $ex->getMessage());
136
        }
137
    }
138
139
    public function settingsTemplate(Setting $settings)
140
    {
141
        try {
142
            $set = $settings->find(1);
143
            $template = new Template();
144
            //$templates = $template->lists('name', 'id')->toArray();
145
            return view('themes.default1.common.setting.template', compact('set', 'template'));
146
        } catch (\Exception $ex) {
147
            return redirect()->back()->with('fails', $ex->getMessage());
148
        }
149
    }
150
151
    public function postSettingsTemplate(Setting $settings, Request $request)
152
    {
153
        try {
154
            $setting = $settings->find(1);
155
            $setting->fill($request->input())->save();
156
157
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
158
        } catch (\Exception $ex) {
159
            return redirect()->back()->with('fails', $ex->getMessage());
160
        }
161
    }
162
163
    public function settingsError(Setting $settings)
164
    {
165
        try {
166
            $set = $settings->find(1);
167
168
            return view('themes.default1.common.setting.error-log', compact('set'));
169
        } catch (\Exception $ex) {
170
            return redirect()->back()->with('fails', $ex->getMessage());
171
        }
172
    }
173
174
    public function settingsActivity(Activity $activities)
175
    {
176
        try {
177
            $activity = $activities->all();
178
179
            return view('themes.default1.common.Activity-Log', compact('activity'));
180
        } catch (\Exception $ex) {
181
            return redirect()->back()->with('fails', $ex->getMessage());
182
        }
183
    }
184
185
    public function getActivity()
186
    {
187
        try {
188
            $activity_log = Activity::select('id', 'log_name', 'description', 'subject_id', 'subject_type', 'causer_id', 'properties', 'created_at')->get();
189
190
            return\ DataTables::of($activity_log)
191
             ->addColumn('checkbox', function ($model) {
192
                         return "<input type='checkbox' class='activity' value=".$model->id.' name=select[] id=check>';
193
                     })
194
                           ->addColumn('name', function ($model) {
195
                               return ucfirst($model->log_name);
196
                           })
197
                             ->addColumn('description', function ($model) {
198
                                 return ucfirst($model->description);
199
                             })
200
                          ->addColumn('username', function ($model) {
201
                                 $causer_id = $model->causer_id;
202
                                 $names = User::where('id', $causer_id)->pluck('last_name', 'first_name');
203
                                 foreach ($names as $key => $value) {
204
                                     $fullName = $key.' '.$value;
205
                                     return $fullName;
206
                                 }
207
                             })
208
                              ->addColumn('role', function ($model) {
209
                                  $causer_id = $model->causer_id;
210
                                  $role = User::where('id', $causer_id)->pluck('role');
211
                                  return json_decode($role);
212
                              })
213
                               ->addColumn('new', function ($model) {
214
                                    $properties = ($model->properties);
215
                                    $newEntry = $this->getNewEntry($properties,$model);
216
                                    return $newEntry;
217
                                    
218
                                })
219
                                ->addColumn('old', function ($model) {
220
                                    $data = ($model->properties);
221
                                    $oldEntry = $this->getOldEntry($data,$model);
222
                                    return $oldEntry;
223
                                })
224
                                ->addColumn('created_at', function ($model) {
225
                                    $newDate = $this->getDate($model->created_at);
226
                                    return $newDate;
227
                                })
228
229
                            ->rawColumns(['checkbox', 'name', 'description',   'username', 'role', 'new', 'old', 'created_at'])
230
                            ->make(true);
231
        } catch (\Exception $e) {
232
            Bugsnag::notifyException($e);
233
234
            return redirect()->back()->with('fails', $e->getMessage());
235
        }
236
    }
237
238
    
239
240
    public function destroy(Request $request)
241
    {
242
        try {
243
            $ids = $request->input('select');
244
            if (!empty($ids)) {
245
                foreach ($ids as $id) {
246
                    $activity = Activity::where('id', $id)->first();
247
                    if ($activity) {
248
                        $activity->delete();
249
                    } else {
250
                        echo "<div class='alert alert-danger alert-dismissable'>
251
                        <i class='fa fa-ban'></i>
252
253
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */     \Lang::get('message.failed').'
254
255
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
256
                            './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
257
                    </div>';
258
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
259
                    }
260
                }
261
                echo "<div class='alert alert-success alert-dismissable'>
262
                        <i class='fa fa-ban'></i>
263
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.success').'
264
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
265
                            './* @scrutinizer ignore-type */ \Lang::get('message.deleted-successfully').'
266
                    </div>';
267
            } else {
268
                echo "<div class='alert alert-danger alert-dismissable'>
269
                        <i class='fa fa-ban'></i>
270
                        <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.failed').'
271
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
272
                            './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').'
273
                    </div>';
274
                //echo \Lang::get('message.select-a-row');
275
            }
276
        } catch (\Exception $e) {
277
            echo "<div class='alert alert-danger alert-dismissable'>
278
                        <i class='fa fa-ban'></i>
279
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
280
                        /* @scrutinizer ignore-type */\Lang::get('message.failed').'
281
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
282
                            '.$e->getMessage().'
283
                    </div>';
284
        }
285
    }
286
287
  
288
289
    public function postSettingsError(Setting $settings, Request $request)
290
    {
291
        try {
292
            $setting = $settings->find(1);
293
            $setting->fill($request->input())->save();
294
295
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
296
        } catch (\Exception $ex) {
297
            return redirect()->back()->with('fails', $ex->getMessage());
298
        }
299
    }
300
}
301