Test Setup Failed
Pull Request — develop (#200)
by Tony
07:41
created

SettingsController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
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
class SettingsController extends Controller
34
{
35
    use Helpers;
36
37
    /**
38
     * Display a listing of the resource.
39
     *
40
     * @return array|\Illuminate\Http\Response
41
     */
42
    public function index()
43
    {
44
        return Settings::all();
45
    }
46
47
    /**
48
     * Show the form for creating a new resource.
49
     *
50
     * @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...
51
     */
52
    public function create()
53
    {
54
        //
55
    }
56
57
    /**
58
     * Store a newly created resource in storage.
59
     *
60
     * @param  \Illuminate\Http\Request $request
61
     * @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...
62
     */
63
    public function store(Request $request)
64
    {
65
        // TODO move to validation
66
        if (!$request->user()->isAdmin()) {
67
            return $this->response->errorForbidden('Only Admins can change settings');
68
        }
69
70
        Settings::set($request->setting, $request->value);
71
        return $this->response->accepted();
72
    }
73
74
    /**
75
     * Display the specified resource.
76
     *
77
     * @param  int $id
78
     * @return \Illuminate\Http\Response
79
     */
80
    public function show($id)
81
    {
82
        return Settings::get($id);
83
    }
84
85
    /**
86
     * Show the form for editing the specified resource.
87
     *
88
     * @param  int $id
89
     * @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...
90
     */
91
    public function edit($id)
92
    {
93
        //
94
    }
95
96
    /**
97
     * Update the specified resource in storage.
98
     *
99
     * @param  \Illuminate\Http\Request $request
100
     * @param  int $id
101
     * @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...
102
     */
103
    public function update(Request $request, $id)
104
    {
105
        if (!$request->user()->isAdmin()) {
106
            return $this->response->errorForbidden('Only Admins can change settings');
107
        }
108
109
        if (!isset($request->value)) {
110
            return $this->response->errorBadRequest('Missing value');
111
        }
112
113
        Settings::set($id, $request->value);
114
        return $this->response->accepted();
115
    }
116
117
    /**
118
     * Remove the specified resource from storage.
119
     *
120
     * @param Request $request
121
     * @param  int $id
122
     * @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...
123
     */
124
    public function destroy(Request $request, $id)
125
    {
126
        if (!$request->user()->isAdmin()) {
127
            return $this->response->errorForbidden('Only Admins can change settings');
128
        }
129
130
        Settings::forget($id);
131
        return $this->response->accepted();
132
    }
133
}
134