Completed
Push — master ( e5ba92...7a1e6c )
by Arjay
13:54
created

WidgetsController::parameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 1
b 0
f 1
nc 2
nop 2
dl 12
loc 12
rs 9.4285
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 View Code Duplication
    public function create(Widget $widget)
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...
56
    {
57
        $widget->extension_id = old('extension_id', Extension::WIDGET_WYSIWYG);
58
        $widget->template     = old('template', 'widgets.wysiwyg.default');
59
60
        return view('administrator.widgets.create', compact('widget'));
61
    }
62
63
    /**
64
     * Store a newly created widget.
65
     *
66
     * @param \Yajra\CMS\Http\Requests\WidgetFormRequest $request
67
     * @return \Illuminate\Http\RedirectResponse
68
     */
69 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...
70
    {
71
        $widget = new Widget;
72
        $widget->fill($request->all());
73
        $widget->published     = $request->get('published', false);
74
        $widget->authenticated = $request->get('authenticated', false);
75
        $widget->show_title    = $request->get('show_title', false);
76
        $widget->save();
77
78
        $widget->syncPermissions($request->get('permissions', []));
79
        $widget->syncMenuAssignment($request->get('menu', []), $request->get('assignment', Widget::ALL_PAGES));
80
81
        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<Symfony\Component...on\TranslatorInterface>; 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...
82
83
        return redirect()->route('administrator.widgets.index');
84
    }
85
86
    /**
87
     * Show and edit selected widget.
88
     *
89
     * @param \Yajra\CMS\Entities\Widget $widget
90
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
91
     */
92
    public function edit(Widget $widget)
93
    {
94
        $widget->type     = old('type', $widget->type);
95
        $widget->template = old('template', $widget->template);
96
97
        return view('administrator.widgets.edit', compact('widget'));
98
    }
99
100
    /**
101
     * Update selected widget.
102
     *
103
     * @param \Yajra\CMS\Entities\Widget $widget
104
     * @param \Yajra\CMS\Http\Requests\WidgetFormRequest $request
105
     * @return \Illuminate\Http\RedirectResponse
106
     */
107 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...
108
    {
109
        $widget->fill($request->all());
110
        $widget->published     = $request->get('published', false);
111
        $widget->authenticated = $request->get('authenticated', false);
112
        $widget->show_title    = $request->get('show_title', false);
113
        $widget->save();
114
115
        $widget->syncPermissions($request->get('permissions', []));
116
        $widget->syncMenuAssignment($request->get('menu', []), $request->get('assignment', Widget::ALL_PAGES));
117
118
        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<Symfony\Component...on\TranslatorInterface>; 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...
119
120
        return redirect()->route('administrator.widgets.index');
121
    }
122
123
    /**
124
     * Remove selected widget.
125
     *
126
     * @param \Yajra\CMS\Entities\Widget $widget
127
     * @return \Illuminate\Http\JsonResponse
128
     * @throws \Exception
129
     */
130
    public function destroy(Widget $widget)
131
    {
132
        $widget->delete();
133
134
        return $this->notifySuccess(trans('cms::widget.destroy.success'));
135
    }
136
137
    /**
138
     * Publish/Unpublish a widget.
139
     *
140
     * @param \Yajra\CMS\Entities\Widget $widget
141
     * @return \Illuminate\Http\JsonResponse
142
     */
143
    public function publish(Widget $widget)
144
    {
145
        $widget->published = ! $widget->published;
146
        $widget->save();
147
148
        return $this->notifySuccess(trans('cms::widget.update.publish', [
149
                'task' => $widget->published ? 'published' : 'unpublished',
150
            ])
151
        );
152
    }
153
154
    /**
155
     * Get all widget types.
156
     *
157
     * @param string $type
158
     * @return string
159
     */
160
    public function templates($type)
161
    {
162
        $data      = [];
163
        $extension = $this->repository->findOrFail($type);
164
        foreach ($extension->param('templates') as $template) {
165
            $data[] = ['key' => $template['path'], 'value' => $template['description']];
166
        }
167
168
        return response()->json([
169
            'template' => $data[0]['key'],
170
            'selected' => $type,
171
            'data'     => $data,
172
        ], 200);
173
    }
174
175
    /**
176
     * Get widget custom parameter form if any.
177
     *
178
     * @param int $id
179
     * @param int $widget
180
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
181
     */
182 View Code Duplication
    public function parameters($id, $widget)
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...
183
    {
184
        $widget    = Widget::query()->findOrNew($widget);
185
        $extension = $this->repository->findOrFail($id);
186
        $formView  = $extension->param('form');
187
188
        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...
189
            return view($formView, compact('widget'));
190
        }
191
192
        return view('widgets.partials.none');
193
    }
194
}
195