Completed
Push — master ( a19a47...15bbc5 )
by Arjay
15:59
created

MenuItemsController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 13

Importance

Changes 0
Metric Value
dl 0
loc 182
rs 10
c 0
b 0
f 0
wmc 15
lcom 2
cbo 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 5 1
A __construct() 0 7 1
A create() 0 9 1
A edit() 0 8 2
A update() 0 20 2
A destroy() 0 10 2
A publish() 0 11 2
A articles() 0 4 1
A extensions() 0 11 2
A store() 0 14 1
1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use App\Http\Requests;
6
use Baum\MoveNotPossibleException;
7
use Exception;
8
use Illuminate\Http\Request;
9
use Yajra\CMS\DataTables\ArticlesDataTable;
10
use Yajra\CMS\DataTables\MenuItemsDataTable;
11
use Yajra\CMS\Entities\Extension;
12
use Yajra\CMS\Entities\Menu;
13
use Yajra\CMS\Entities\Navigation;
14
use Yajra\CMS\Http\Requests\MenuItemsFormRequest;
15
use Yajra\CMS\Repositories\Extension\Repository;
16
17
class MenuItemsController extends Controller
18
{
19
    /**
20
     * Controller specific permission ability map.
21
     *
22
     * @var array
23
     */
24
    protected $customPermissionMap = [
25
        'publish'  => 'update',
26
        'articles' => 'view',
27
        'types'    => 'view',
28
    ];
29
30
    /**
31
     * @var \Yajra\CMS\Repositories\Extension\Repository
32
     */
33
    protected $extensions;
34
35
    /**
36
     * MenuItemsController constructor.
37
     *
38
     * @param \Yajra\CMS\Repositories\Extension\Repository $extensions
39
     */
40
    public function __construct(Repository $extensions)
41
    {
42
        $this->authorizePermissionResource('menu');
43
        $this->extensions = $extensions;
44
45
        view()->share('extensions', $this->extensions->all()->where('type', 'menu'));
0 ignored issues
show
Bug introduced by
The method share 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...
46
    }
47
48
    /**
49
     * @param \Yajra\CMS\Entities\Navigation $navigation
50
     * @param \Yajra\CMS\DataTables\MenuItemsDataTable $dataTable
51
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
52
     */
53
    public function index(Navigation $navigation, MenuItemsDataTable $dataTable)
54
    {
55
        return $dataTable->forNavigation($navigation)
56
                         ->render('administrator.navigation.menu.index', compact('navigation'));
57
    }
58
59
    /**
60
     * @param \Yajra\CMS\Entities\Navigation $navigation
61
     * @param \Yajra\CMS\Entities\Menu $menu
62
     * @return mixed
63
     */
64
    public function create(Navigation $navigation, Menu $menu)
65
    {
66
        $menu->extension_id = old('extension_id', Extension::MENU_INTERNAL);
67
        $menu->published    = true;
68
        $menu->setHighestOrderNumber();
69
        $menu->load('extension');
70
71
        return view('administrator.navigation.menu.create', compact('navigation', 'menu'));
72
    }
73
74
    /**
75
     * @param \Yajra\CMS\Entities\Navigation $navigation
76
     * @param \Yajra\CMS\Http\Requests\MenuItemsFormRequest $request
77
     * @return mixed
78
     */
79
    public function store(Navigation $navigation, MenuItemsFormRequest $request)
80
    {
81
        $menu = new Menu();
82
        $menu->fill($request->all());
83
        $menu->published     = $request->get('published', false);
84
        $menu->authenticated = $request->get('authenticated', false);
85
        $navigation->menus()->save($menu);
86
87
        $menu->permissions()->sync($request->get('permissions', []));
88
89
        flash()->success(trans('cms::menu.store.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::menu.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...
90
91
        return redirect()->route('administrator.navigation.menu.index', $navigation->id);
0 ignored issues
show
Documentation introduced by
$navigation->id is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
    }
93
94
    /**
95
     * @param \Yajra\CMS\Entities\Navigation $navigation
96
     * @param \Yajra\CMS\Entities\Menu $menu
97
     * @return mixed
98
     */
99
    public function edit(Navigation $navigation, Menu $menu)
100
    {
101
        if ($menu->isRoot()) {
102
            abort(404);
103
        }
104
105
        return view('administrator.navigation.menu.edit', compact('navigation', 'menu'));
106
    }
107
108
    /**
109
     * @param \Yajra\CMS\Entities\Navigation $navigation
110
     * @param \Yajra\CMS\Entities\Menu $menu
111
     * @param \Yajra\CMS\Http\Requests\MenuItemsFormRequest $request
112
     * @return mixed
113
     */
114
    public function update(Navigation $navigation, Menu $menu, MenuItemsFormRequest $request)
115
    {
116
        try {
117
            $menu->makeChildOf(Menu::query()->findOrFail($request->get('parent_id')));
118
        } catch (MoveNotPossibleException $e) {
119
            flash()->error(trans('cms::menu.update.move_error'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::menu.update.move_error') targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, Laracasts\Flash\FlashNotifier::error() 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 back();
122
        }
123
        $menu->fill($request->all());
124
        $menu->published     = $request->get('published', false);
125
        $menu->authenticated = $request->get('authenticated', false);
126
        $navigation->menus()->save($menu);
127
128
        $menu->permissions()->sync($request->get('permissions', []));
129
130
        flash()->success(trans('cms::menu.update.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::menu.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...
131
132
        return redirect()->route('administrator.navigation.menu.index', $navigation->id);
0 ignored issues
show
Documentation introduced by
$navigation->id is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
133
    }
134
135
    /**
136
     * @param \Yajra\CMS\Entities\Navigation $navigation
137
     * @param \Yajra\CMS\Entities\Menu $menu
138
     * @return string
139
     * @throws \Exception
140
     */
141
    public function destroy(Navigation $navigation, Menu $menu)
142
    {
143
        if ($menu->isRoot()) {
144
            abort(404);
145
        }
146
147
        $navigation->menus()->findOrFail($menu->id)->delete();
148
149
        return $this->notifySuccess(trans('cms::menu.destroy.success'));
150
    }
151
152
    /**
153
     * Publish/Unpublish a menu.
154
     *
155
     * @param \Yajra\CMS\Entities\Navigation $navigation
156
     * @param \Yajra\CMS\Entities\Menu $menu
157
     * @return \Illuminate\Http\JsonResponse
158
     */
159
    public function publish(Navigation $navigation, Menu $menu)
160
    {
161
        /** @var \Yajra\CMS\Entities\Menu $menu */
162
        $menu = $navigation->menus()->findOrFail($menu->id);
163
        $menu->togglePublishedState();
164
165
        return $this->notifySuccess(sprintf(
166
            'Menu successfully %s!',
167
            $menu->published ? 'published' : 'unpublished'
168
        ));
169
    }
170
171
    /**
172
     * Get articles list.
173
     *
174
     * @param \Yajra\CMS\DataTables\ArticlesDataTable $dataTable
175
     * @return \Illuminate\Http\JsonResponse
176
     */
177
    public function articles(ArticlesDataTable $dataTable)
178
    {
179
        return $dataTable->ajax();
180
    }
181
182
    /**
183
     * @param \Illuminate\Http\Request $request
184
     * @param \Yajra\CMS\Entities\Menu $menu
185
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
186
     */
187
    public function extensions(Request $request, Menu $menu)
188
    {
189
        $extension = $this->extensions->findOrFail($request->query('key'));
0 ignored issues
show
Documentation introduced by
$request->query('key') is of type string|array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
190
        $template  = $extension->param('template');
191
192
        if (view()->exists($template)) {
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...
193
            return view($template, compact('menu'));
194
        }
195
196
        return '';
197
    }
198
}
199