Completed
Push — develop ( aeac19...744b64 )
by Neil
9s
created

SettingsController::store()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 29
ccs 0
cts 17
cp 0
rs 8.439
cc 5
eloc 17
nc 5
nop 1
crap 30
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
     * Constructor
36
     */
37
    public function __construct(Request $request)
38
    {
39
        $this->middleware('auth');
40
    }
41
42
    /**
43
     * Display a listing of the resource.
44
     *
45
     * @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...
46
     */
47
    public function index()
48
    {
49
        return view('settings.list');
50
    }
51
52
    /**
53
     * Show the form for creating a new resource.
54
     *
55
     * @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...
56
     */
57
    public function create()
58
    {
59
        //
60
    }
61
62
    /**
63
     * Store a newly created resource in storage.
64
     *
65
     * @param  \Illuminate\Http\Request $request
66
     * @return \Illuminate\Http\Response
67
     */
68
    public function store(Request $request)
69
    {
70
        $key = Input::get('key');
71
        if (Settings::isReadOnly($key)) {
72
            return response('Read only setting', 422);
73
        }
74
75
        $type = Input::get('type');
76
        $value = Input::get('value');
77
78
        if ($type == 'settings-value') {
79
            Settings::set($key, $value);
80
            return response('OK', 200);
81
        }
82
        elseif ($type == 'settings-array') {
83
            $current = Settings::get($key);
84
85
            // remove entries with missing indexes
86
            $delete = array_diff_key($current, $value);
87
            foreach (array_keys($delete) as $index) {
88
                Settings::forget($key.'.'.$index);
89
            }
90
91
            Settings::set($key, $value);
92
            return response('OK', 200);
93
        }
94
95
        return response('Invalid Data', 422);
96
    }
97
98
    /**
99
     * Display the specified resource.
100
     *
101
     * @param  int $id
102
     * @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...
103
     */
104
    public function show($id)
105
    {
106
        return view('settings.list', ['section' => $id]);
107
    }
108
109
    /**
110
     * Show the form for editing the specified resource.
111
     *
112
     * @param  int $id
113
     * @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...
114
     */
115
    public function edit($id)
116
    {
117
        return view('settings.list', ['section' => $id]);
118
    }
119
120
    /**
121
     * Update the specified resource in storage.
122
     *
123
     * @param  \Illuminate\Http\Request $request
124
     * @param  int $id
125
     * @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...
126
     */
127
    public function update(Request $request, $id)
128
    {
129
        //
130
    }
131
132
    /**
133
     * Remove the specified resource from storage.
134
     *
135
     * @param  int $id
136
     * @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...
137
     */
138
    public function destroy($id)
139
    {
140
        Settings::forget($id);
141
    }
142
}
143