Completed
Push — settings ( bea8d4...96499a )
by Tony
05:40
created

DashboardWidgetController::get_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 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
    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)
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...
27
    {
28
        //
29
    }
30
31
    /**
32
     * Show the form for creating a new resource.
33
     *
34
     * @return \Illuminate\Http\Response
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 View Code Duplication
        if ($user_widget->save())
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...
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
73
     */
74
    public function show(Request $request, $id)
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...
75
    {
76
        $widget  = Widgets::find($id);
77
        $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
        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
86
     */
87
    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...
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 View Code Duplication
        if ($users_widgets->save())
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...
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
        {
133
            return $this->response->array(array('statusText' => 'OK'));
134
        }
135
        else {
136
            return $this->response->errorInternal();
137
        }
138
    }
139
140
    public function get_content($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...
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