Passed
Push — master ( 054b32...1c77af )
by Ron
07:39 queued 12s
created

SettingsController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Test Coverage

Coverage 37.16%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 105
dl 0
loc 265
ccs 42
cts 113
cp 0.3716
rs 10
c 1
b 0
f 0
wmc 20

13 Methods

Rating   Name   Duplication   Size   Complexity  
A configuration() 0 11 1
A submitLogo() 0 20 1
A logoSettings() 0 3 1
A __construct() 0 3 1
A delBackup() 0 5 1
A submitConfiguration() 0 25 3
A backupsIndex() 0 3 1
A submitEmailSettings() 0 49 3
A runBackup() 0 5 1
A downloadBackup() 0 8 2
A getBackups() 0 19 3
A emailSettings() 0 14 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
use Illuminate\Support\Facades\Storage;
17
use Illuminate\Support\Facades\Artisan;
18
19
class SettingsController extends Controller
20
{
21 24
    public function __construct()
22
    {
23 24
        $this->middleware(['auth', 'can:is_installer']);
24 24
    }
25
26
    //  Bring up the change logo form
27 2
    public function logoSettings()
28
    {
29 2
        return view('installer.logoSettings');
30
    }
31
    //  Submit the new company logo
32 2
    public function submitLogo(Request $request)
33
    {
34 2
        $request->validate([
35 2
            'file' => 'mimes:jpeg,bmp,png,jpg,gif'
36
        ]);
37
38 2
        $file     = $request->file;
39 2
        $fileName = $file->getClientOriginalName();
40 2
        $file->storeAs('img', $fileName, 'public');
41
42 2
        Settings::firstOrCreate(
43 2
            ['key'   => 'app.logo'],
44 2
            ['key'   => 'app.logo', 'value' => '/storage/img/' . $fileName]
45 2
        )->update(['value' => '/storage/img/' . $fileName]);
46
47 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id);
48 2
        Log::debug('Submitted Data - ', $request->toArray());
49 2
        Log::notice('A new company logo has been uploaded by User ID-' . Auth::user()->user_id);
50
51 2
        return response()->json(['url' => '/storage/img/' . $fileName]);
52
    }
53
54 2
    public function configuration()
55
    {
56 2
        $settings = collect([
57 2
            'url'      => config('app.url'),
58 2
            'timezone' => config('app.timezone'),
59 2
            'filesize' => config('filesystems.paths.max_size'),
60
        ]);
61
62 2
        return view('installer.configuration', [
63 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...
64 2
            'settings'     => $settings,
65
        ]);
66
    }
67
68 2
    public function submitConfiguration(Request $request)
69
    {
70 2
        $request->validate([
71 2
            'timezone' => 'required',
72
            'filesize' => 'required',
73
            //  TODO - add additinal validation to make sure proper information is passed in
74
        ]);
75
76
        //  Update the site timezone
77 2
        if (config('app.timezone') !== $request->timezone) {
78 2
            Settings::firstOrCreate(
79 2
                ['key'   => 'app.timezone'],
80 2
                ['key'   => 'app.timezone', 'value' => $request->timezone]
81 2
            )->update(['value' => $request->timezone]);
82
        }
83
        //  Update the maximum file upload size
84 2
        if (config('filesystems.paths.max_size') !== $request->filesize) {
85 2
            Settings::firstOrCreate(
86 2
                ['key'   => 'filesystems.paths.max_size'],
87 2
                ['key'   => 'filesystems.paths.max_size', 'value' => $request->filesize]
88 2
            )->update(['value' => $request->filesize]);
89
        }
90 2
        $request->session()->flash('success', 'Configuration Updated');
91
92 2
        return response()->json(['success' => true]);
93
    }
94
95
    //  System backups index page
96
    public function backupsIndex()
97
    {
98
        return view('installer.backupsIndex');
99
    }
100
101
    //  Retrieve the list of backups
102
    public function getBackups()
103
    {
104
        $backups = Storage::disk('backup')->files();
105
        $buArray = [];
106
107
        //  Filter list to include timestamp
108
        foreach($backups as $backup)
109
        {
110
            $parts = pathinfo($backup);
111
            if($parts['extension'] === 'zip')
112
            {
113
                $buArray[] = [
114
                    'name' => $backup,
115
                    'date' => Carbon::createFromTimestamp(Storage::disk('backup')->lastModified($backup))->format('M d, Y h:m a'),
116
                ];
117
            }
118
        }
119
120
        return $buArray;
121
    }
122
123
    //  Delete a backup
124
    public function delBackup($name)
125
    {
126
        Storage::disk('backup')->delete($name);
127
128
        return response()->json(['success' => true]);
129
    }
130
131
    //  Download a backup
132
    public function downloadBackup($name)
133
    {
134
        if(Storage::disk('backup')->exists($name))
135
        {
136
            return Storage::disk('backup')->download($name);
137
        }
138
139
        return view('err.badFile');
140
    }
141
142
    //  Create a new backup
143
    public function runBackup()
144
    {
145
        Artisan::call('tb-backup:run');
146
147
        return response()->json(['success' => true]);
148
    }
149
150
151
152
153
154
155
    //  Email Settings form
156
    public function emailSettings()
157
    {
158
        $settings = collect([
159
            'host'       => config('mail.host'),
160
            'port'       => config('mail.port'),
161
            'encryption' => config('mail.encryption'),
162
            'username'   => config('mail.username'),
163
            'fromEmail'  =>config('mail.from.address'),
164
            'fromName'   => config('mail.from.name'),
165
        ]);
166
167
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
168
        return view('installer.emailSettings', [
169
            'settings' => $settings,
170
        ]);
171
    }
172
173
    //  Send a test email
174
    public function sendTestEmail(Request $request)
175
    {
176
        //  TODO - fix this and make it work
177
        return response()->json(['success' => false]);
178
        // $request->validate([
179
        //     'host'       => 'required',
180
        //     'port'       => 'required|numeric',
181
        //     'encryption' => 'required',
182
        //     'username'   => 'required',
183
        //     'fromEmail'  => 'required',
184
        //     'fromName'   => 'required',
185
        // ]);
186
187
        // //  Update each setting
188
        // Settings::firstOrCreate(
189
        //     ['key'   => 'mail.host'],
190
        //     ['key'   => 'mail.host', 'value' => $request->host]
191
        // )->update(['value' => $request->host]);
192
        // Settings::firstOrCreate(
193
        //     ['key'   => 'mail.port'],
194
        //     ['key'   => 'mail.port', 'value' => $request->port]
195
        // )->update(['value' => $request->port]);
196
        // Settings::firstOrCreate(
197
        //     ['key'   => 'mail.encryption'],
198
        //     ['key'   => 'mail.encryption', 'value' => $request->encryption]
199
        // )->update(['value' => $request->encryption]);
200
        // Settings::firstOrCreate(
201
        //     ['key'   => 'mail.username'],
202
        //     ['key'   => 'mail.username', 'value' => $request->username]
203
        // )->update(['value' => $request->username]);
204
        // Settings::firstOrCreate(
205
        //     ['key'   => 'mail.from.address'],
206
        //     ['key'   => 'mail.from.address', 'value' => $request->fromEmail]
207
        // )->update(['value' => $request->fromEmail]);
208
        // Settings::firstOrCreate(
209
        //     ['key'   => 'mail.from.name'],
210
        //     ['key'   => 'mail.from.name', 'value' => $request->fromName]
211
        // )->update(['value' => $request->fromName]);
212
        // //  Only update the password if it has changed
213
        // if (!empty($request->password) && $request->password !== 'NULL') {
214
        //     // Settings::where('key', 'mail.password')->update(['value' => $request->password]);
215
        //     Settings::firstOrCreate(
216
        //         ['key'   => 'mail.password'],
217
        //         ['key'   => 'mail.password', 'value' => $request->password]
218
        //     )->update(['value' => $request->password]);
219
        // }
220
221
        // try
222
        // {
223
        //     Mail::to(Auth::user())->send(new TestEmail($request));
224
        // }
225
        // catch (\Exception $e)
226
        // {
227
        //     // return $e->getMessage();
228
        //     return response()->json(['success' => false, 'message' => $e->getMessage()]);
229
        // }
230
231
        // return response()->json(['success' => true, 'message' => 'Email successfully sent to '.Auth::user()->email]);
232
    }
233
234
    //  Submit the test email form
235
    public function submitEmailSettings(Request $request)
236
    {
237
        $request->validate([
238
            'host'       => 'required',
239
            'port'       => 'required|numeric',
240
            'encryption' => 'required',
241
            'fromEmail'  => 'required',
242
            'fromName'   => 'required',
243
        ]);
244
245
        //  Update each setting
246
        Settings::firstOrCreate(
247
            ['key'   => 'mail.host'],
248
            ['key'   => 'mail.host', 'value' => $request->host]
249
        )->update(['value' => $request->host]);
250
        Settings::firstOrCreate(
251
            ['key'   => 'mail.port'],
252
            ['key'   => 'mail.port', 'value' => $request->port]
253
        )->update(['value' => $request->port]);
254
        Settings::firstOrCreate(
255
            ['key'   => 'mail.encryption'],
256
            ['key'   => 'mail.encryption', 'value' => $request->encryption]
257
        )->update(['value' => $request->encryption]);
258
        Settings::firstOrCreate(
259
            ['key'   => 'mail.username'],
260
            ['key'   => 'mail.username', 'value' => $request->username]
261
        )->update(['value' => $request->username]);
262
        Settings::firstOrCreate(
263
            ['key'   => 'mail.from.address'],
264
            ['key'   => 'mail.from.address', 'value' => $request->fromEmail]
265
        )->update(['value' => $request->fromEmail]);
266
        Settings::firstOrCreate(
267
            ['key'   => 'mail.from.name'],
268
            ['key'   => 'mail.from.name', 'value' => $request->fromName]
269
        )->update(['value' => $request->fromName]);
270
        //  Only update the password if it has changed
271
        if(!empty($request->password) && $request->password !== 'NULL')
272
        {
273
            // Settings::where('key', 'mail.password')->update(['value' => $request->password]);
274
            Settings::firstOrCreate(
275
                ['key'   => 'mail.password'],
276
                ['key'   => 'mail.password', 'value' => $request->password]
277
            )->update(['value' => $request->password]);
278
        }
279
280
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
281
        Log::debug('Submitted Data - ', $request->toArray());
282
        Log::notice('Email Settings have been changed by User ID-'.Auth::user()->user_id);
283
        return response()->json(['success' => true]);
284
    }
285
}
286