Test Setup Failed
Pull Request — develop (#200)
by Tony
04:29
created

SettingsController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * SettingsController.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2017 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Api\Controllers;
27
28
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Api\Controllers\Controller.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
29
use Dingo\Api\Routing\Helpers;
30
use Illuminate\Http\Request;
31
use Settings;
32
33
/**
34
 * Settings resource representation.
35
 *
36
 * @Resource("Settings", uri="/api/settings")
37
 */
38
class SettingsController extends Controller
39
{
40
    use Helpers;
41
42
    /**
43
     * List all settings
44
     *
45
     * @Get("/")
46
     * @Versions({"v1"})
47
     * @Transaction({
48
     *      @Request("/"),
49
     *      @Response(200, body={"multi":{"dimensional","json"},"array":"values"})
50
     * })
51
     */
52
    public function index()
53
    {
54
        return Settings::all();
55
    }
56
57
    /**
58
     * Save a setting or array of settings
59
     *
60
     * @Post("/{setting}")
61
     * @Versions({"v1"})
62
     * @Transaction({
63
     *      @Request({"setting":"snmp.community", "value":{"public","private"}}),
64
     *      @Response(200)
65
     * })
66
     * @Parameters({
67
     *      @Parameter("setting", type="string", required=true, description="The setting path, separated by periods"),
68
     *      @Parameter("value", type="string", required=true, description="The value to set")
69
     * })
70
     */
71
    public function store(Request $request)
72
    {
73
        // TODO move to validation
74
        if (!$request->user()->isAdmin()) {
75
            return $this->response->errorForbidden('Only Admins can change settings');
76
        }
77
78
        Settings::set($request->setting, $request->value);
79
        return $this->response->accepted();
80
    }
81
82
    /**
83
     * Retrieve a setting
84
     *
85
     * @Get("/{setting}")
86
     * @Versions({"v1"})
87
     * @Transaction({
88
     *      @Request("/snmp.community"),
89
     *      @Response(200, body={"public","private"})
90
     * })
91
     * @Parameters({
92
     *      @Parameter("setting", type="string", required=true, description="The setting path, separated by periods")
93
     * })
94
     */
95
    public function show($id)
96
    {
97
        return Settings::get($id);
98
    }
99
100
    /**
101
     * Update the specified resource in storage.
102
     *
103
     * @param  \Illuminate\Http\Request $request
104
     * @param  int $id
105
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\Dingo\Api\Http\Response?

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...
106
     */
107
    public function update(Request $request, $id)
108
    {
109
        if (!$request->user()->isAdmin()) {
110
            return $this->response->errorForbidden('Only Admins can change settings');
111
        }
112
113
        if (!isset($request->value)) {
114
            return $this->response->errorBadRequest('Missing value');
115
        }
116
117
        Settings::set($id, $request->value);
118
        return $this->response->accepted();
119
    }
120
121
    /**
122
     * Unset a setting
123
     *
124
     * @Delete("/{setting}")
125
     * @Versions({"v1"})
126
     * @Transaction({
127
     *      @Request("/snmp.community"),
128
     *      @Response(200)
129
     * })
130
     * @Parameters({
131
     *      @Parameter("setting", type="string", required=true, description="The setting path, separated by periods")
132
     * })
133
     */
134
    public function destroy(Request $request, $id)
135
    {
136
        if (!$request->user()->isAdmin()) {
137
            return $this->response->errorForbidden('Only Admins can change settings');
138
        }
139
140
        Settings::forget($id);
141
        return $this->response->accepted();
142
    }
143
}
144