SettingsController::store()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 1
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (C) 2016 Tony Murray <[email protected]>
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
/**
18
 * SettingsController.php
19
 *
20
 * @package    LibreNMS
21
 * @author     Tony Murray <[email protected]>
22
 * @copyright  2016 Tony Murray
23
 * @license    @license http://opensource.org/licenses/GPL-3.0 GNU Public License v3 or later
24
 */
25
26
namespace App\Http\Controllers;
27
28
use Illuminate\Http\Request;
29
use Input;
30
use Settings;
31
32
class SettingsController extends Controller
33
{
34
    /**
35
     * Display a listing of the resource.
36
     *
37
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
38
     */
39
    public function index()
40
    {
41
        return view('settings.list');
42
    }
43
44
    /**
45
     * Show the form for creating a new resource.
46
     *
47
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
48
     */
49
    public function create()
50
    {
51
        //
52
    }
53
54
    /**
55
     * Store a newly created resource in storage.
56
     *
57
     * @param  \Illuminate\Http\Request $request
58
     * @return \Illuminate\Http\Response
59
     */
60
    public function store(Request $request)
61
    {
62
        $key = Input::get('key');
63
        if (Settings::isReadOnly($key)) {
64
            return response('Read only setting', 422);
65
        }
66
67
        $type = Input::get('type');
68
        $value = Input::get('value');
69
70
        if ($type == 'settings-value') {
71
            Settings::set($key, $value);
72
            return response('OK', 200);
73
        } elseif ($type == 'settings-array') {
74
            $current = Settings::get($key);
75
76
            // remove entries with missing indexes
77
            $delete = array_diff_key($current, $value);
78
            foreach (array_keys($delete) as $index) {
79
                Settings::forget($key.'.'.$index);
80
            }
81
82
            Settings::set($key, $value);
83
            return response('OK', 200);
84
        }
85
86
        return response('Invalid Data', 422);
87
    }
88
89
    /**
90
     * Display the specified resource.
91
     *
92
     * @param  int $id
93
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
94
     */
95
    public function show($id)
96
    {
97
        return view('settings.list', ['section' => $id]);
98
    }
99
100
    /**
101
     * Show the form for editing the specified resource.
102
     *
103
     * @param  int $id
104
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
105
     */
106
    public function edit($id)
107
    {
108
        return view('settings.list', ['section' => $id]);
109
    }
110
111
    /**
112
     * Update the specified resource in storage.
113
     *
114
     * @param  \Illuminate\Http\Request $request
115
     * @param  int $id
116
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
117
     */
118
    public function update(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
        //
121
    }
122
123
    /**
124
     * Remove the specified resource from storage.
125
     *
126
     * @param  int $id
127
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
128
     */
129
    public function destroy($id)
130
    {
131
        Settings::forget($id);
132
    }
133
}
134