Passed
Push — dev5 ( fee765...622739 )
by Ron
07:50
created

SettingsController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Test Coverage

Coverage 48.96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 95
dl 0
loc 225
ccs 47
cts 96
cp 0.4896
rs 10
c 1
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A logoSettings() 0 3 1
A __construct() 0 3 1
A configuration() 0 11 1
A submitConfiguration() 0 34 4
A submitLogo() 0 20 1
A submitEmailSettings() 0 50 3
A emailSettings() 0 15 1
A sendTestEmail() 0 4 1
1
<?php
2
3
namespace App\Http\Controllers\Installer;
4
5
use App\User;
6
use App\Settings;
7
use Carbon\Carbon;
8
use App\Mail\TestEmail;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Mail;
13
use App\Http\Controllers\Controller;
14
use Illuminate\Support\Facades\Route;
15
use Illuminate\Support\Facades\Config;
16
17
class SettingsController extends Controller
18
{
19 24
    public function __construct()
20
    {
21 24
        $this->middleware(['auth', 'can:is_installer']);
22 24
    }
23
24
    //  Bring up the change logo form
25 2
    public function logoSettings()
26
    {
27 2
        return view('installer.logoSettings');
28
    }
29
    //  Submit the new company logo
30 2
    public function submitLogo(Request $request)
31
    {
32 2
        $request->validate([
33 2
            'file' => 'mimes:jpeg,bmp,png,jpg,gif'
34
        ]);
35
36 2
        $file     = $request->file;
37 2
        $fileName = $file->getClientOriginalName();
38 2
        $file->storeAs('img', $fileName, 'public');
39
40 2
        Settings::firstOrCreate(
41 2
            ['key'   => 'app.logo'],
42 2
            ['key'   => 'app.logo', 'value' => '/storage/img/' . $fileName]
43 2
        )->update(['value' => '/storage/img/' . $fileName]);
44
45 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id);
46 2
        Log::debug('Submitted Data - ', $request->toArray());
47 2
        Log::notice('A new company logo has been uploaded by User ID-' . Auth::user()->user_id);
48
49 2
        return response()->json(['url' => '/storage/img/' . $fileName]);
50
    }
51
52 2
    public function configuration()
53
    {
54 2
        $settings = collect([
55 2
            'url'      => config('app.url'),
56 2
            'timezone' => config('app.timezone'),
57 2
            'filesize' => config('filesystems.paths.max_size'),
58
        ]);
59
60 2
        return view('installer.configuration', [
61 2
            'timzone_list' => \Timezonelist::toArray(),
1 ignored issue
show
Bug introduced by
The type Timezonelist was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
62 2
            'settings'     => $settings,
63
        ]);
64
    }
65
66 2
    public function submitConfiguration(Request $request)
67
    {
68 2
        $request->validate([
69 2
            'url'      => 'required',
70
            'timezone' => 'required',
71
            'filesize' => 'required',
72
            //  TODO - add additinal validation to make sure proper information is passed in
73
        ]);
74
75
        //  Update the site URL
76 2
        if(config('app.url') !== $request->url)
77
        {
78 2
            Settings::firstOrCreate(
79 2
                ['key'   => 'app.url'],
80 2
                ['key'   => 'app.url', 'value' => $request->url]
81 2
            )->update(['value' => $request->url]);
82
        }
83
        //  Update the site timezone
84 2
        if (config('app.timezone') !== $request->timezone) {
85 2
            Settings::firstOrCreate(
86 2
                ['key'   => 'app.timezone'],
87 2
                ['key'   => 'app.timezone', 'value' => $request->timezone]
88 2
            )->update(['value' => $request->timezone]);
89
        }
90
        //  Update the maximum file upload size
91 2
        if (config('filesystems.paths.max_size') !== $request->filesize) {
92 2
            Settings::firstOrCreate(
93 2
                ['key'   => 'filesystems.paths.max_size'],
94 2
                ['key'   => 'filesystems.paths.max_size', 'value' => $request->filesize]
95 2
            )->update(['value' => $request->filesize]);
96
        }
97 2
        $request->session()->flash('success', 'Configuration Updated');
98
99 2
        return response()->json(['success' => true]);
100
    }
101
102
103
104
105
106
107
108
109
110
111
    //  Email Settings form
112
    public function emailSettings()
113
    {
114
        $settings = collect([
115
            'host'       => config('mail.host'),
116
            'port'       => config('mail.port'),
117
            'encryption' => config('mail.encryption'),
118
            'username'   => config('mail.username'),
119
            // 'password'   => config('mail.password'),
120
            'fromEmail'  =>config('mail.from.address'),
121
            'fromName'   => config('mail.from.name'),
122
        ]);
123
124
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
125
        return view('installer.emailSettings', [
126
            'settings' => $settings,
127
        ]);
128
    }
129
130
    //  Send a test email
131
    public function sendTestEmail(Request $request)
132
    {
133
        //  TODO - fix this and make it work
134
        return response()->json(['success' => false]);
135
        // $request->validate([
136
        //     'host'       => 'required',
137
        //     'port'       => 'required|numeric',
138
        //     'encryption' => 'required',
139
        //     'username'   => 'required',
140
        //     'fromEmail'  => 'required',
141
        //     'fromName'   => 'required',
142
        // ]);
143
144
        // //  Update each setting
145
        // Settings::firstOrCreate(
146
        //     ['key'   => 'mail.host'],
147
        //     ['key'   => 'mail.host', 'value' => $request->host]
148
        // )->update(['value' => $request->host]);
149
        // Settings::firstOrCreate(
150
        //     ['key'   => 'mail.port'],
151
        //     ['key'   => 'mail.port', 'value' => $request->port]
152
        // )->update(['value' => $request->port]);
153
        // Settings::firstOrCreate(
154
        //     ['key'   => 'mail.encryption'],
155
        //     ['key'   => 'mail.encryption', 'value' => $request->encryption]
156
        // )->update(['value' => $request->encryption]);
157
        // Settings::firstOrCreate(
158
        //     ['key'   => 'mail.username'],
159
        //     ['key'   => 'mail.username', 'value' => $request->username]
160
        // )->update(['value' => $request->username]);
161
        // Settings::firstOrCreate(
162
        //     ['key'   => 'mail.from.address'],
163
        //     ['key'   => 'mail.from.address', 'value' => $request->fromEmail]
164
        // )->update(['value' => $request->fromEmail]);
165
        // Settings::firstOrCreate(
166
        //     ['key'   => 'mail.from.name'],
167
        //     ['key'   => 'mail.from.name', 'value' => $request->fromName]
168
        // )->update(['value' => $request->fromName]);
169
        // //  Only update the password if it has changed
170
        // if (!empty($request->password) && $request->password !== 'NULL') {
171
        //     // Settings::where('key', 'mail.password')->update(['value' => $request->password]);
172
        //     Settings::firstOrCreate(
173
        //         ['key'   => 'mail.password'],
174
        //         ['key'   => 'mail.password', 'value' => $request->password]
175
        //     )->update(['value' => $request->password]);
176
        // }
177
178
        // try
179
        // {
180
        //     Mail::to(Auth::user())->send(new TestEmail($request));
181
        // }
182
        // catch (\Exception $e)
183
        // {
184
        //     // return $e->getMessage();
185
        //     return response()->json(['success' => false, 'message' => $e->getMessage()]);
186
        // }
187
188
        // return response()->json(['success' => true, 'message' => 'Email successfully sent to '.Auth::user()->email]);
189
    }
190
191
    //  Submit the test email form
192
    public function submitEmailSettings(Request $request)
193
    {
194
        $request->validate([
195
            'host'       => 'required',
196
            'port'       => 'required|numeric',
197
            'encryption' => 'required',
198
            'username'   => 'required',
199
            'fromEmail'  => 'required',
200
            'fromName'   => 'required',
201
        ]);
202
203
        //  Update each setting
204
        Settings::firstOrCreate(
205
            ['key'   => 'mail.host'],
206
            ['key'   => 'mail.host', 'value' => $request->host]
207
        )->update(['value' => $request->host]);
208
        Settings::firstOrCreate(
209
            ['key'   => 'mail.port'],
210
            ['key'   => 'mail.port', 'value' => $request->port]
211
        )->update(['value' => $request->port]);
212
        Settings::firstOrCreate(
213
            ['key'   => 'mail.encryption'],
214
            ['key'   => 'mail.encryption', 'value' => $request->encryption]
215
        )->update(['value' => $request->encryption]);
216
        Settings::firstOrCreate(
217
            ['key'   => 'mail.username'],
218
            ['key'   => 'mail.username', 'value' => $request->username]
219
        )->update(['value' => $request->username]);
220
        Settings::firstOrCreate(
221
            ['key'   => 'mail.from.address'],
222
            ['key'   => 'mail.from.address', 'value' => $request->fromEmail]
223
        )->update(['value' => $request->fromEmail]);
224
        Settings::firstOrCreate(
225
            ['key'   => 'mail.from.name'],
226
            ['key'   => 'mail.from.name', 'value' => $request->fromName]
227
        )->update(['value' => $request->fromName]);
228
        //  Only update the password if it has changed
229
        if(!empty($request->password) && $request->password !== 'NULL')
230
        {
231
            // Settings::where('key', 'mail.password')->update(['value' => $request->password]);
232
            Settings::firstOrCreate(
233
                ['key'   => 'mail.password'],
234
                ['key'   => 'mail.password', 'value' => $request->password]
235
            )->update(['value' => $request->password]);
236
        }
237
238
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
239
        Log::debug('Submitted Data - ', $request->toArray());
240
        Log::notice('Email Settings have been changed by User ID-'.Auth::user()->user_id);
241
        return response()->json(['success' => true]);
242
    }
243
244
    // protected function updateEmailSettings($data)
245
    // {
246
    //     //  Update each setting
247
    //     Settings::firstOrCreate(
248
    //         ['key'   => 'mail.host'],
249
    //         ['key'   => 'mail.host', 'value' => $data->host]
250
    //     )->update(['value' => $data->host]);
251
    //     Settings::firstOrCreate(
252
    //         ['key'   => 'mail.port'],
253
    //         ['key'   => 'mail.port', 'value' => $data->port]
254
    //     )->update(['value' => $data->port]);
255
    //     Settings::firstOrCreate(
256
    //         ['key'   => 'mail.encryption'],
257
    //         ['key'   => 'mail.encryption', 'value' => $data->encryption]
258
    //     )->update(['value' => $data->encryption]);
259
    //     Settings::firstOrCreate(
260
    //         ['key'   => 'mail.username'],
261
    //         ['key'   => 'mail.username', 'value' => $data->username]
262
    //     )->update(['value' => $data->username]);
263
    //     Settings::firstOrCreate(
264
    //         ['key'   => 'mail.from.address'],
265
    //         ['key'   => 'mail.from.address', 'value' => $data->fromEmail]
266
    //     )->update(['value' => $data->fromEmail]);
267
    //     Settings::firstOrCreate(
268
    //         ['key'   => 'mail.from.name'],
269
    //         ['key'   => 'mail.from.name', 'value' => $data->fromName]
270
    //     )->update(['value' => $data->fromName]);
271
    //     //  Only update the password if it has changed
272
    //     if (!empty($data->password) && $data->password !== 'NULL') {
273
    //         // Settings::where('key', 'mail.password')->update(['value' => $request->password]);
274
    //         Settings::firstOrCreate(
275
    //             ['key'   => 'mail.password'],
276
    //             ['key'   => 'mail.password', 'value' => $data->password]
277
    //         )->update(['value' => $data->password]);
278
    //     }
279
280
    //     return true;
281
    // }
282
283
284
}
285