SettingController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 86
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 9 1
A update() 0 21 4
A created() 0 6 1
A getUpdateFormRequest() 0 4 1
A getModel() 0 4 1
A getStoreFormRequest() 0 4 1
1
<?php
2
3
namespace Microboard\Http\Controllers;
4
5
use Illuminate\Auth\Access\AuthorizationException;
6
use Illuminate\Http\RedirectResponse;
7
use Illuminate\View\View;
8
use Microboard\Http\Requests\Setting\StoreFormRequest;
9
use Microboard\Http\Requests\Setting\UpdateFormRequest;
10
use Microboard\Models\Setting;
11
use Spatie\MediaLibrary\MediaCollections\Exceptions\FileDoesNotExist;
12
use Spatie\MediaLibrary\MediaCollections\Exceptions\FileIsTooBig;
13
14
class SettingController extends ResourceController
15
{
16
    /**
17
     * Display a listing of the resource.
18
     *
19
     * @return View
20
     * @throws AuthorizationException
21
     */
22
    public function index()
23
    {
24
        $this->authorize('viewAny', new Setting);
25
        return view('microboard::settings.index', [
26
            'groups' => cache()->remember('settings', 25000, function () {
27
                return Setting::all()->groupBy('group');
28
            })
29
        ]);
30
    }
31
32
    /**
33
     * Update the specified resource in storage.
34
     *
35
     * @param UpdateFormRequest $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
     * @param Setting $setting
0 ignored issues
show
Bug introduced by
There is no parameter named $setting. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
37
     * @return RedirectResponse
38
     * @throws AuthorizationException
39
     */
40
    public function update()
41
    {
42
        $request = resolve($this->getUpdateFormRequest());
43
        $this->authorize('update', new Setting);
44
45
        foreach ($request->get('value') as $id => $value) {
46
            $setting = tap(Setting::find($id))->update(compact('value'));
47
48
            if (in_array($setting->type, ['avatar', 'files'])) {
49
                if ($setting->type === 'avatar') {
50
                    $setting->getMedia()->each->delete();
51
                }
52
53
                addMediaTo($setting, 'default', "value.{$setting->id}");
54
            }
55
        }
56
57
        cache()->forget('settings');
58
59
        return redirect()->back();
60
    }
61
62
    /**
63
     * @param StoreFormRequest $request
64
     * @param Setting $model
65
     * @return RedirectResponse
66
     * @throws FileDoesNotExist
67
     * @throws FileIsTooBig
68
     */
69
    protected function created($request, $model)
70
    {
71
        addMediaTo($model, 'default', "value.{$model->id}");
72
73
        return redirect()->back();
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    protected function getUpdateFormRequest(): string
80
    {
81
        return UpdateFormRequest::class;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    protected function getModel(): string
88
    {
89
        return Setting::class;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    protected function getStoreFormRequest(): string
96
    {
97
        return StoreFormRequest::class;
98
    }
99
}
100