Completed
Push — develop ( bed248...1eb861 )
by Tony
8s
created

DashboardWidgetController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 18.75%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
c 4
b 0
f 0
lcom 2
cbo 6
dl 0
loc 135
ccs 9
cts 48
cp 0.1875
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A index() 0 4 1
A create() 0 4 1
A store() 0 20 2
A show() 0 6 1
A edit() 0 4 1
A update() 0 23 3
A destroy() 0 10 2
A get_content() 0 5 1
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use App\Models\Dashboard;
6
use App\Models\UsersWidgets;
7
use App\Models\Widgets;
8
use Dingo\Api\Http;
9
use Dingo\Api\Routing\Helpers;
10
use Illuminate\Http\Request;
11
12
class DashboardWidgetController extends Controller
13
{
14
15
    use Helpers;
16
17 1
    public function __construct() {
18
19 1
    }
20
21
    /**
22
     * Display a listing of all authorized devices
23
     *
24
     * @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...
25
     */
26
    public function index(Request $request)
27
    {
28
        //
29
    }
30
31
    /**
32
     * Show the form for creating a new resource.
33
     *
34
     * @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...
35
     */
36
    public function create()
37
    {
38
        //
39
    }
40
41
    /**
42
     * Store a newly created resource in storage.
43
     *
44
     * @param  \Illuminate\Http\Request  $request
45
     * @return \Illuminate\Http\Response
46
     */
47
    public function store(Request $request)
48
    {
49
        $row                         = Dashboard::find($request->dashboard_id)->widgets()->max('row') + 1;
50
        $user_widget                 = new UsersWidgets;
51
        $user_widget->user_id        = $request->user()->user_id;
52
        $user_widget->widget_id      = $request->widget_id;
1 ignored issue
show
Bug introduced by
The property widget_id 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...
53
        $user_widget->col            = $request->col;
1 ignored issue
show
Bug introduced by
The property col 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...
54
        $user_widget->row            = $row;
55
        $user_widget->size_x         = $request->size_x;
1 ignored issue
show
Bug introduced by
The property size_x 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...
56
        $user_widget->size_y         = $request->size_y;
1 ignored issue
show
Bug introduced by
The property size_y 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...
57
        $user_widget->title          = $request->title;
1 ignored issue
show
Bug introduced by
The property title 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
        $user_widget->dashboard_id   = $request->dashboard_id;
1 ignored issue
show
Bug introduced by
The property dashboard_id 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 ($user_widget->save())
60
        {
61
            return $this->response->array(array('statusText' => 'OK', 'user_widget_id' => $user_widget->user_widget_id));
62
        }
63
        else {
64
            return $this->response->errorInternal();
65
        }
66
    }
67
68
    /**
69
     * Display the specified resource.
70
     *
71
     * @param  int  $id
72
     * @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...
73
     */
74 1
    public function show(Request $request, $id)
75
    {
76 1
        $widget  = Widgets::find($id);
77 1
        $content = $this->api->be(auth()->user())->get('/api/dashboard-widget/'.$id.'/content');
0 ignored issues
show
Bug introduced by
The method user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78 1
        return array('widget' => $widget, 'content' => $content);
79
    }
80
81
    /**
82
     * Show the form for editing the specified resource.
83
     *
84
     * @param  int  $id
85
     * @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...
86
     */
87
    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...
88
    {
89
        //
90
    }
91
92
    /**
93
     * Update the specified resource in storage.
94
     *
95
     * @param  \Illuminate\Http\Request  $request
96
     * @param  int  $id
97
     * @return \Illuminate\Http\Response
98
     */
99
    public function update(Request $request, $id)
100
    {
101
        if ($request->input('settings'))
102
        {
103
            $users_widgets           = UsersWidgets::find($id);
104
            $users_widgets->settings = json_encode($request->input('settings'));
105
        }
106
        else {
107
            $users_widgets         = UsersWidgets::find($id);
108
            $users_widgets->col    = $request->input('x');
109
            $users_widgets->row    = $request->input('y');
110
            $users_widgets->size_x = $request->input('width');
111
            $users_widgets->size_y = $request->input('height');
112
        }
113
114
        if ($users_widgets->save())
115
        {
116
            return $this->response->array(array('statusText' => 'OK'));
117
        }
118
        else {
119
            return $this->response->errorInternal();
120
        }
121
    }
122
123
    /**
124
     * Remove the specified resource from storage.
125
     *
126
     * @param  int  $id
127
     * @return \Illuminate\Http\Response
128
     */
129
    public function destroy($id)
130
    {
131
        if (UsersWidgets::destroy($id))
132 1
        {
133
            return $this->response->array(array('statusText' => 'OK'));
134 1
        }
135 1
        else {
136
            return $this->response->errorInternal();
137
        }
138
    }
139
140
    public function get_content($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...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
141
    {
142
        $content[] = '<i class="fa fa-spinner fa-pulse fa-5x fa-fw margin-bottom"></i><span class="sr-only">Loading...</span>';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$content was never initialized. Although not strictly required by PHP, it is generally a good practice to add $content = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
143
        return $this->response->array(array('statusText' => 'OK', 'content' => $content));
144
    }
145
146
}
147