Completed
Push — develop ( a4058d...a8d9bd )
by Tony
15:30
created

DashboardController::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use Validator;
6
use App\Models\User;
7
use App\Models\Dashboard;
8
use App\Models\UsersWidgets;
9
use Dingo\Api\Routing\Helpers;
10
use Illuminate\Http\Request;
11
12
class DashboardController extends Controller
13
{
14
15
    use Helpers;
16
17 3
    public function __construct() {
18
19 3
    }
20
21
    /**
22
     * Display a listing of all authorized devices
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26 3
    public function index(Request $request)
27
    {
28 3
        $dashboards = Dashboard::allAvailable($request->user())->get();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Query\Builder, but not in App\Models\Dashboard.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
29 3
        return $dashboards;
30
    }
31
32
    /**
33
     * Show the form for creating a new resource.
34
     *
35
     * @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...
36
     */
37
    public function create(Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request 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...
38
    {
39
        //
40
    }
41
42
    /**
43
     * Store a newly created resource in storage.
44
     *
45
     * @param  \Illuminate\Http\Request  $request
46
     * @return \Illuminate\Http\Response
47
     */
48 1
    public function store(Request $request)
49
    {
50 1
        $validation = Validator::make($request->all(), [
51 1
            'name' => 'required|max:255',
52 1
            'access' => 'required',
53 1
        ]);
54 1
        if($validation->passes())
55 1
        {
56 1
            $dashboard = new Dashboard;
57 1
            $dashboard->dashboard_name = $request->name;
1 ignored issue
show
Bug introduced by
The property name does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
58 1
            $dashboard->access         = $request->access;
1 ignored issue
show
Bug introduced by
The property access does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
59 1
            if ($request->user()->dashboards()->save($dashboard))
60 1
            {
61 1
                if (is_numeric($request->copy_from))
62 1
                {
63
                    $duplicate_widgets = Dashboard::find($request->copy_from)->widgets()->get();
64
                    foreach ($duplicate_widgets as $tmp_widget)
65
                    {
66
                        /** @var UsersWidgets $tmp_widget */
67
                        $new_widget               = $tmp_widget->replicate();
68
                        $new_widget->user_id      = $request->user()->user_id;
69
                        $new_widget->dashboard_id = $dashboard->dashboard_id;
70
                        unset($new_widget->user_widget_id);
71
                        $new_widget->save();
72
                    }
73
                }
74 1
                return $this->response->array(array('statusText' => 'OK', 'dashboard_id' => $dashboard->dashboard_id));
75
            }
76
            else {
77
                return $this->response->errorInternal();
78
            }
79
        }
80
        else {
81
            $errors = $validation->errors();
82
            return response()->json($errors,422);
83
        }
84
    }
85
86
    /**
87
     * Display the specified resource.
88
     *
89
     * @param  int  $id
90
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

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...
91
     */
92 2
    public function show(Request $request, $id)
93
    {
94 2
        $dashboard = Dashboard::find($id);
95 2
        $widgets   = $dashboard->widgets()->get();
96
        // morph the data as required
97 2
        if ($request->query('displayFormat') == 'human') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
98
        }
99
100 2
        return array('dashboard' => $dashboard, 'widgets' => $widgets);
101
    }
102
103
    /**
104
     * Show the form for editing the specified resource.
105
     *
106
     * @param  int  $id
107
     * @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...
108
     */
109
    public function edit($id)
1 ignored issue
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...
110
    {
111
        //
112
    }
113
114
    /**
115
     * Update the specified resource in storage.
116
     *
117
     * @param  \Illuminate\Http\Request  $request
118
     * @param  int  $id
119
     * @return \Illuminate\Http\Response
120
     */
121
    public function update(Request $request, $id)
122
    {
123
        $validation = Validator::make($request->all(), [
124
            'name' => 'required|max:255',
125
            'access' => 'required',
126
        ]);
127
        if($validation->passes())
128
        {
129
            $dashboard = Dashboard::find($id);
130
            $dashboard->dashboard_name = $request->name;
1 ignored issue
show
Bug introduced by
The property name does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
131
            $dashboard->access         = $request->access;
1 ignored issue
show
Bug introduced by
The property access does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
132
            if ($dashboard->save())
133
            {
134
                return $this->response->array(array('statusText' => 'OK'));
135
            }
136
            else {
137
                return $this->response->errorInternal();
138
            }
139
        }
140
        else {
141
            $errors = $validation->errors();
142
            return response()->json($errors,422);
143
        }
144
    }
145
146
    /**
147
     * Remove the specified resource from storage.
148
     *
149
     * @param  int  $id
150
     * @return \Illuminate\Http\Response
151
     */
152
    public function destroy(Request $request, $id)
153
    {
154
        if (Dashboard::where('user_id', $request->user()->user_id)->where('dashboard_id', $id)->delete())
155
        {
156
            if (UsersWidgets::where('dashboard_id', $id)->delete() >= 0)
157
            {
158
                return $this->response->array(array('statusText' => 'OK'));
159
            }
160
            else {
161
                return $this->response->errorInternal();
162
            }
163
        }
164
        else {
165
            return $this->response->errorInternal();
166
        }
167
    }
168
169
    public function clear($id)
170
    {
171
        if (Dashboard::find($id)->widgets()->delete() >= 0)
172
        {
173
            return $this->response->array(array('statusText' => 'OK'));
174
        }
175
        else {
176
            return $this->response->errorInternal();
177
        }
178
    }
179
180
}
181