Completed
Pull Request — develop (#76)
by Neil
07:56
created

DashboardController::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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
    public function __construct() {
18
19
    }
20
21
    /**
22
     * Display a listing of all authorized devices
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26
    public function index(Request $request)
27
    {
28
        $dashboards = User::find($request->user()->user_id)->dashboards()->get();
29
        // morph the data as required
30
        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...
31
        }
32
33
        return $dashboards;
34
    }
35
36
    /**
37
     * Show the form for creating a new resource.
38
     *
39
     * @return \Illuminate\Http\Response
40
     */
41
    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...
42
    {
43
        //
44
    }
45
46
    /**
47
     * Store a newly created resource in storage.
48
     *
49
     * @param  \Illuminate\Http\Request  $request
50
     * @return \Illuminate\Http\Response
51
     */
52
    public function store(Request $request)
53
    {
54
        $validation = Validator::make($request->all(), [
55
            'name' => 'required|max:255',
56
            'access' => 'required',
57
        ]);
58
        if($validation->passes())
59
        {
60
            $dashboard = new dashboard;
61
            $dashboard->user_id        = $request->user()->user_id;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<App\Models\Dashboard>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
62
            $dashboard->dashboard_name = $request->name;
1 ignored issue
show
Documentation introduced by
The property dashboard_name does not exist on object<App\Models\Dashboard>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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...
63
            $dashboard->access         = $request->access;
1 ignored issue
show
Documentation introduced by
The property access does not exist on object<App\Models\Dashboard>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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...
64
            if ($dashboard->save())
65
            {
66
                if (is_numeric($request->copy_from))
67
                {
68
                    $duplicate_widgets = Dashboard::find($request->copy_from)->widgets()->get();
69
                    foreach ($duplicate_widgets as $new_widget)
70
                    {
71
                        UsersWidgets::create([  'user_id'      => $request->user()->user_id,
72
                                                'widget_id'    => $new_widget->widget_id,
73
                                                'col'          => $new_widget->col,
74
                                                'row'          => $new_widget->row,
75
                                                'size_y'       => $new_widget->size_y,
76
                                                'size_x'       => $new_widget->size_x,
77
                                                'title'        => $new_widget->title,
78
                                                'refresh'      => $new_widget->refresh,
79
                                                'settings'     => $new_widget->settings,
80
                                                'dashboard_id' => $dashboard->dashboard_id,
0 ignored issues
show
Documentation introduced by
The property dashboard_id does not exist on object<App\Models\Dashboard>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
                                            ]);
82
                    }
83
                }
84
                return $this->response->array(array('statusText' => 'OK', 'dashboard_id' => $dashboard->dashboard_id));
0 ignored issues
show
Documentation introduced by
The property dashboard_id does not exist on object<App\Models\Dashboard>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
85
            }
86
            else {
87
                return $this->response->errorInternal();
88
            }
89
        }
90
        else {
91
            $errors = $validation->errors();
92
            return response()->json($errors,422);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json($errors, 422); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Api\Controllers\DashboardController::store of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
93
        }
94
    }
95
96
    /**
97
     * Display the specified resource.
98
     *
99
     * @param  int  $id
100
     * @return \Illuminate\Http\Response
101
     */
102
    public function show(Request $request, $id)
103
    {
104
        $dashboard = Dashboard::find($id);
105
        $widgets   = Dashboard::find($id)->widgets()->get();
106
        // morph the data as required
107
        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...
108
        }
109
110
        return array('dashboard' => $dashboard, 'widgets' => $widgets);
111
    }
112
113
    /**
114
     * Show the form for editing the specified resource.
115
     *
116
     * @param  int  $id
117
     * @return \Illuminate\Http\Response
118
     */
119
    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...
120
    {
121
        //
122
    }
123
124
    /**
125
     * Update the specified resource in storage.
126
     *
127
     * @param  \Illuminate\Http\Request  $request
128
     * @param  int  $id
129
     * @return \Illuminate\Http\Response
130
     */
131
    public function update(Request $request, $id)
132
    {
133
        $validation = Validator::make($request->all(), [
134
            'name' => 'required|max:255',
135
            'access' => 'required',
136
        ]);
137
        if($validation->passes())
138
        {
139
            $dashboard = Dashboard::find($id);
140
            $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...
141
            $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...
142
            if ($dashboard->save())
143
            {
144
                return $this->response->array(array('statusText' => 'OK'));
145
            }
146
            else {
147
                return $this->response->errorInternal();
148
            }
149
        }
150
        else {
151
            $errors = $validation->errors();
152
            return response()->json($errors,422);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json($errors, 422); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Api\Controllers\DashboardController::update of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
153
        }
154
    }
155
156
    /**
157
     * Remove the specified resource from storage.
158
     *
159
     * @param  int  $id
160
     * @return \Illuminate\Http\Response
161
     */
162
    public function destroy($id)
163
    {
164
        if (Dashboard::destroy($id))
165
        {
166
            if (UsersWidgets::where('dashboard_id', $id)->delete() >= 0)
167
            {
168
                return $this->response->array(array('statusText' => 'OK'));
169
            }
170
            else {
171
                return $this->response->errorInternal();
172
            }
173
        }
174
        else {
175
            return $this->response->errorInternal();
176
        }
177
    }
178
179
    public function clear($id)
180
    {
181
        if (UsersWidgets::where('dashboard_id', $id)->delete() >= 0)
182
        {
183
            return $this->response->array(array('statusText' => 'OK'));
184
        }
185
        else {
186
            return $this->response->errorInternal();
187
        }
188
    }
189
190
}
191