RakshakSettingsController::updateCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinkstudeo\Rakshak\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Cache;
7
use Thinkstudeo\Rakshak\RakshakSetting;
8
9
class RakshakSettingsController extends Controller
10
{
11
    /**
12
     * Show the form for editing the specified resource.
13
     *
14
     * @param  User $user
0 ignored issues
show
Bug introduced by
The type Thinkstudeo\Rakshak\Http\Controllers\User 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...
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function edit()
18
    {
19
        $setting = RakshakSetting::first();
20
        $this->authorize('update', $setting);
21
22
        return view('rakshak::settings.edit', compact('setting'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('rakshak::se...t', compact('setting')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
23
    }
24
25
    /**
26
     * Update the specified resource in storage.
27
     *
28
     * @param  \Illuminate\Http\Request  $request
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function update(Request $request)
32
    {
33
        $setting = RakshakSetting::first();
34
        $this->authorize('update', $setting);
35
36
        $validated = $request->validate([
37
            'enable_2fa' => ['required', 'boolean'],
38
            'channel_2fa' => ['required', 'in:email,sms'],
39
            'control_level_2fa' => ['required', 'in:admin,user'],
40
        ]);
41
42
        $setting = tap($setting)->update($validated);
43
        $this->updateCache($setting);
44
45
        if ($request->expectsJson()) {
46
            return response(['message' => 'Rakshak settings updated successfully!', 'record' => $setting], 200);
47
        }
48
49
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...updated successfully.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
50
            ->back()
51
            ->with('status', 'success')
52
            ->with('message', 'Rakshak settings updated successfully.');
53
    }
54
55
    /**
56
     * Update the values for the cache keys.
57
     *
58
     * @param RakshakSetting $setting
59
     * @return void
60
     */
61
    protected function updateCache($setting)
62
    {
63
        Cache::forever('rakshak.enable_2fa', $setting->enable_2fa);
64
        Cache::forever('rakshak.channel_2fa', $setting->channel_2fa);
65
        Cache::forever('rakshak.control_level_2fa', $setting->control_level_2fa);
66
    }
67
}
68