Passed
Push — dev5 ( 455ff1...2be149 )
by Ron
22:55
created

SettingsController::submitConfiguration()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 8
nop 1
dl 0
loc 34
ccs 0
cts 15
cp 0
crap 20
rs 9.584
c 0
b 0
f 0
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 12
    public function __construct()
20
    {
21 12
        $this->middleware(['auth', 'can:is_installer']);
22 12
    }
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
            ['key'   => 'app.logo', 'value' => '/storage/img/' . $fileName]
43
        )->update(['value' => '/storage/img/' . $fileName]);
44 2
45 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id);
46 2
        Log::debug('Submitted Data - ', $request->toArray());
47
        Log::notice('A new company logo has been uploaded by User ID-' . Auth::user()->user_id);
48 2
49
        return response()->json(['url' => '/storage/img/' . $fileName]);
50
    }
51
52
    public function configuration()
53
    {
54
        $settings = collect([
55
            'url'      => config('app.url'),
56
            'timezone' => config('app.timezone'),
57
            'filesize' => config('filesystems.paths.max_size'),
58
        ]);
59
60
        return view('installer.configuration', [
61
            '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
            'settings'     => $settings,
63
        ]);
64
    }
65
66
    public function submitConfiguration(Request $request)
67
    {
68
        $request->validate([
69
            '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
        if(config('app.url') !== $request->url)
77
        {
78
            Settings::firstOrCreate(
79
                ['key'   => 'app.url'],
80
                ['key'   => 'app.url', 'value' => $request->url]
81
            )->update(['value' => $request->url]);
82
        }
83
        //  Update the site timezone
84
        if (config('app.timezone') !== $request->timezone) {
85
            Settings::firstOrCreate(
86
                ['key'   => 'app.timezone'],
87
                ['key'   => 'app.timezone', 'value' => $request->timezone]
88
            )->update(['value' => $request->timezone]);
89
        }
90
        //  Update the maximum file upload size
91
        if (config('filesystems.paths.max_size') !== $request->filesize) {
92
            Settings::firstOrCreate(
93
                ['key'   => 'filesystems.paths.max_size'],
94
                ['key'   => 'filesystems.paths.max_size', 'value' => $request->filesize]
95
            )->update(['value' => $request->filesize]);
96
        }
97
        $request->session()->flash('success', 'Configuration Updated');
98
99
        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
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
115
        return view('installer.emailSettings');
116
    }
117
118
    //  Send a test email
119
    public function sendTestEmail(Request $request)
120
    {
121
        //  to be added
122
    }
123
124
    //  Submit the test email form
125
    public function submitEmailSettings(Request $request)
126
    {
127
        $request->validate([
128
            'host'       => 'required',
129
            'port'       => 'required|numeric',
130
            'encryption' => 'required',
131
            'username'   => 'required'
132
        ]);
133
134
        //  Update each setting
135
        Settings::where('key', 'mail.host')->update(['value' => $request->host]);
136
        Settings::where('key', 'mail.port')->update(['value' => $request->port]);
137
        Settings::where('key', 'mail.encryption')->update(['value' => $request->encryption]);
138
        Settings::where('key', 'mail.username')->update(['value' => $request->username]);
139
        if(!empty($request->password))
140
        {
141
            Settings::where('key', 'mail.password')->update(['value' => $request->password]);
142
        }
143
144
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
145
        Log::debug('Submitted Data - ', $request->toArray());
146
        Log::notice('Email Settings have been changed by User ID-'.Auth::user()->user_id);
147
        return redirect()->back()->with('success', 'Tech Bench Successfully Updated');
148
    }
149
}
150