Completed
Push — misc ( 5d788f...8599dc )
by Tony
02:45
created

DashboardController::update()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.9713
cc 3
eloc 15
nc 3
nop 2
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 = Dashboard::AllAvailable($request->user()->user_id)->get();
0 ignored issues
show
Bug introduced by
The method AllAvailable() does not exist on App\Models\Dashboard. Did you maybe mean scopeAllAvailable()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
29
        return $dashboards;
30
    }
31
32
    /**
33
     * Show the form for creating a new resource.
34
     *
35
     * @return \Illuminate\Http\Response
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
    public function store(Request $request)
49
    {
50
        $validation = Validator::make($request->all(), [
51
            'name' => 'required|max:255',
52
            'access' => 'required',
53
        ]);
54
        if($validation->passes())
55
        {
56
            $dashboard = new dashboard;
57
            $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...
58
            $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...
59
            if ($request->user()->dashboards()->save($dashboard))
60
            {
61
                if (is_numeric($request->copy_from))
62
                {
63
                    $duplicate_widgets = Dashboard::find($request->copy_from)->widgets()->get();
64
                    foreach ($duplicate_widgets as $tmp_widget)
65
                    {
66
                        $new_widget               = $tmp_widget->replicate();
67
                        $new_widget->user_id      = $request->user()->user_id;
68
                        $new_widget->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 __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...
69
                        unset($new_widget->user_widget_id);
70
                        $new_widget->save();
71
                    }
72
                }
73
                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...
74
            }
75
            else {
76
                return $this->response->errorInternal();
77
            }
78
        }
79
        else {
80
            $errors = $validation->errors();
81
            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...
82
        }
83
    }
84
85
    /**
86
     * Display the specified resource.
87
     *
88
     * @param  int  $id
89
     * @return \Illuminate\Http\Response
90
     */
91
    public function show(Request $request, $id)
92
    {
93
        $dashboard = Dashboard::find($id);
94
        $widgets   = $dashboard->widgets()->get();
95
        // morph the data as required
96
        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...
97
        }
98
99
        return array('dashboard' => $dashboard, 'widgets' => $widgets);
100
    }
101
102
    /**
103
     * Show the form for editing the specified resource.
104
     *
105
     * @param  int  $id
106
     * @return \Illuminate\Http\Response
107
     */
108
    public function edit($id)
0 ignored issues
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...
109
    {
110
        //
111
    }
112
113
    /**
114
     * Update the specified resource in storage.
115
     *
116
     * @param  \Illuminate\Http\Request  $request
117
     * @param  int  $id
118
     * @return \Illuminate\Http\Response
119
     */
120
    public function update(Request $request, $id)
121
    {
122
        $validation = Validator::make($request->all(), [
123
            'name' => 'required|max:255',
124
            'access' => 'required',
125
        ]);
126
        if($validation->passes())
127
        {
128
            $dashboard = Dashboard::find($id);
129
            $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...
130
            $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...
131
            if ($dashboard->save())
132
            {
133
                return $this->response->array(array('statusText' => 'OK'));
134
            }
135
            else {
136
                return $this->response->errorInternal();
137
            }
138
        }
139
        else {
140
            $errors = $validation->errors();
141
            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...
142
        }
143
    }
144
145
    /**
146
     * Remove the specified resource from storage.
147
     *
148
     * @param  int  $id
149
     * @return \Illuminate\Http\Response
150
     */
151
    public function destroy(Request $request, $id)
152
    {
153
        if (Dashboard::where('user_id', $request->user()->user_id)->where('dashboard_id', $id)->delete())
154
        {
155 View Code Duplication
            if (UsersWidgets::where('dashboard_id', $id)->delete() >= 0)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
            {
157
                return $this->response->array(array('statusText' => 'OK'));
158
            }
159
            else {
160
                return $this->response->errorInternal();
161
            }
162
        }
163
        else {
164
            return $this->response->errorInternal();
165
        }
166
    }
167
168
    public function clear($id)
169
    {
170
        if (Dashboard::find($id)->widgets()->delete() >= 0)
171
        {
172
            return $this->response->array(array('statusText' => 'OK'));
173
        }
174
        else {
175
            return $this->response->errorInternal();
176
        }
177
    }
178
179
}
180