WidgetsController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 185
Duplicated Lines 16.76 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
dl 31
loc 185
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 9

10 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 4 1
A __construct() 0 5 1
A store() 16 16 1
A edit() 0 7 1
A update() 15 15 1
A destroy() 0 6 1
A publish() 0 10 2
A templates() 0 14 2
A parameters() 0 12 2
A create() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use Yajra\CMS\DataTables\WidgetsDataTable;
6
use Yajra\CMS\Entities\Extension;
7
use Yajra\CMS\Entities\Widget;
8
use Yajra\CMS\Http\Requests\WidgetFormRequest;
9
use Yajra\CMS\Repositories\Extension\Repository;
10
11
class WidgetsController extends Controller
12
{
13
    /**
14
     * Controller specific permission ability map.
15
     *
16
     * @var array
17
     */
18
    protected $customPermissionMap = [
19
        'publish' => 'update',
20
    ];
21
22
    /**
23
     * @var \Yajra\CMS\Repositories\Extension\Repository
24
     */
25
    protected $repository;
26
27
    /**
28
     * WidgetsController constructor.
29
     *
30
     * @param \Yajra\CMS\Repositories\Extension\Repository $repository
31
     */
32
    public function __construct(Repository $repository)
33
    {
34
        $this->authorizePermissionResource('widget');
35
        $this->repository = $repository;
36
    }
37
38
    /**
39
     * Display list of widgets.
40
     *
41
     * @param \Yajra\CMS\DataTables\WidgetsDataTable $dataTable
42
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
43
     */
44
    public function index(WidgetsDataTable $dataTable)
45
    {
46
        return $dataTable->render('administrator.widgets.index');
47
    }
48
49
    /**
50
     * Show widget form.
51
     *
52
     * @param \Yajra\CMS\Entities\Widget $widget
53
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
54
     */
55
    public function create(Widget $widget)
56
    {
57
        $widget->extension_id = old('extension_id', Extension::WIDGET_WYSIWYG);
58
        $widget->template     = old('template', 'widgets.wysiwyg.raw');
59
        $widget->published    = old('published', true);
60
61
        return view('administrator.widgets.create', compact('widget'));
62
    }
63
64
    /**
65
     * Store a newly created widget.
66
     *
67
     * @param \Yajra\CMS\Http\Requests\WidgetFormRequest $request
68
     * @return \Illuminate\Http\RedirectResponse
69
     */
70 View Code Duplication
    public function store(WidgetFormRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
71
    {
72
        $widget = new Widget;
73
        $widget->fill($request->all());
74
        $widget->published     = $request->get('published', false);
75
        $widget->authenticated = $request->get('authenticated', false);
76
        $widget->show_title    = $request->get('show_title', false);
77
        $widget->save();
78
79
        $widget->syncPermissions($request->get('permissions', []));
80
        $widget->syncMenuAssignment($request->get('menu', []), $request->get('assignment', Widget::ALL_PAGES));
81
82
        flash()->success(trans('cms::widget.store.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::widget.store.success') targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, Laracasts\Flash\FlashNotifier::success() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
83
84
        return redirect()->route('administrator.widgets.index');
85
    }
86
87
    /**
88
     * Show and edit selected widget.
89
     *
90
     * @param \Yajra\CMS\Entities\Widget $widget
91
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
92
     */
93
    public function edit(Widget $widget)
94
    {
95
        $widget->type     = old('type', $widget->type);
96
        $widget->template = old('template', $widget->template);
97
98
        return view('administrator.widgets.edit', compact('widget'));
99
    }
100
101
    /**
102
     * Update selected widget.
103
     *
104
     * @param \Yajra\CMS\Entities\Widget $widget
105
     * @param \Yajra\CMS\Http\Requests\WidgetFormRequest $request
106
     * @return \Illuminate\Http\RedirectResponse
107
     */
108 View Code Duplication
    public function update(Widget $widget, WidgetFormRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
109
    {
110
        $widget->fill($request->all());
111
        $widget->published     = $request->get('published', false);
112
        $widget->authenticated = $request->get('authenticated', false);
113
        $widget->show_title    = $request->get('show_title', false);
114
        $widget->save();
115
116
        $widget->syncPermissions($request->get('permissions', []));
117
        $widget->syncMenuAssignment($request->get('menu', []), $request->get('assignment', Widget::ALL_PAGES));
118
119
        flash()->success(trans('cms::widget.update.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::widget.update.success') targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, Laracasts\Flash\FlashNotifier::success() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
120
121
        return redirect()->route('administrator.widgets.index');
122
    }
123
124
    /**
125
     * Remove selected widget.
126
     *
127
     * @param \Yajra\CMS\Entities\Widget $widget
128
     * @return \Illuminate\Http\JsonResponse
129
     * @throws \Exception
130
     */
131
    public function destroy(Widget $widget)
132
    {
133
        $widget->delete();
134
135
        return $this->notifySuccess(trans('cms::widget.destroy.success'));
136
    }
137
138
    /**
139
     * Publish/Unpublish a widget.
140
     *
141
     * @param \Yajra\CMS\Entities\Widget $widget
142
     * @return \Illuminate\Http\JsonResponse
143
     */
144
    public function publish(Widget $widget)
145
    {
146
        $widget->published = ! $widget->published;
147
        $widget->save();
148
149
        return $this->notifySuccess(trans('cms::widget.update.publish', [
150
                'task' => $widget->published ? 'published' : 'unpublished',
151
            ])
152
        );
153
    }
154
155
    /**
156
     * Get all widget types.
157
     *
158
     * @param string $type
159
     * @return string
160
     */
161
    public function templates($type)
162
    {
163
        $data      = [];
164
        $extension = $this->repository->findOrFail($type);
165
        foreach ($extension->param('templates') as $template) {
166
            $data[] = ['key' => $template['path'], 'value' => $template['description']];
167
        }
168
169
        return response()->json([
170
            'template' => $data[0]['key'],
171
            'selected' => $type,
172
            'data'     => $data,
173
        ], 200);
174
    }
175
176
    /**
177
     * Get widget custom parameter form if any.
178
     *
179
     * @param int $id
180
     * @param int $widget
181
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
182
     */
183
    public function parameters($id, $widget)
184
    {
185
        $widget    = Widget::withoutGlobalScope('menu_assignment')->findOrNew($widget);
186
        $extension = $this->repository->findOrFail($id);
187
        $formView  = $extension->param('form');
188
189
        if (view()->exists($formView)) {
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

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...
190
            return view($formView, compact('widget'));
191
        }
192
193
        return view('widgets.partials.none');
194
    }
195
}
196