MenuItemController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 48
dl 0
loc 99
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A edit() 0 19 2
A destroy() 0 16 2
A create() 0 17 1
A __construct() 0 3 1
A update() 0 11 1
A store() 0 11 1
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Controllers\Back\Menu;
4
5
use Thinktomorrow\Chief\Admin\Audit\Audit;
6
use Thinktomorrow\Chief\App\Http\Controllers\Controller;
7
use Thinktomorrow\Chief\App\Http\Requests\MenuRequest;
8
use Thinktomorrow\Chief\Site\Menu\Application\CreateMenuItem;
9
use Thinktomorrow\Chief\Site\Menu\Application\DeleteMenuItem;
10
use Thinktomorrow\Chief\Site\Menu\Application\UpdateMenuItem;
11
use Thinktomorrow\Chief\Site\Menu\Menu;
12
use Thinktomorrow\Chief\Site\Menu\MenuItem;
13
use Thinktomorrow\Chief\Site\Menu\Tree\PrepareMenuItemsForAdminSelect;
14
use Thinktomorrow\Chief\Site\Urls\UrlHelper;
15
16
class MenuItemController extends Controller
17
{
18
    private PrepareMenuItemsForAdminSelect $prepareMenuItemsForAdminSelect;
19
20
    public function __construct(PrepareMenuItemsForAdminSelect $prepareMenuItemsForAdminSelect)
21
    {
22
        $this->prepareMenuItemsForAdminSelect = $prepareMenuItemsForAdminSelect;
23
    }
24
25
    /**
26
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
27
     */
28
    public function create(string $menutype)
29
    {
30
        $this->authorize('create-page');
31
32
        $menuitem = new MenuItem();
33
        $menuitem->type = MenuItem::TYPE_INTERNAL;  // Default menu type
34
        $menuitem->menu_type = $menutype;
35
36
        $menuitems = $this->prepareMenuItemsForAdminSelect->prepare(
37
            Menu::tree($menutype, config('app.fallback_locale'))
0 ignored issues
show
Unused Code introduced by
The call to Thinktomorrow\Chief\Site\Menu\Menu::tree() has too many arguments starting with config('app.fallback_locale'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            Menu::/** @scrutinizer ignore-call */ 
38
                  tree($menutype, config('app.fallback_locale'))

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
38
        );
39
40
        return view('chief::admin.menu.create', [
41
            'pages' => UrlHelper::allOnlineModels(),
42
            'menuitem' => $menuitem,
43
            'ownerReference' => null,
44
            'parents' => $menuitems,
45
        ]);
46
    }
47
48
    public function store(MenuRequest $request)
49
    {
50
        $this->authorize('create-page');
51
52
        $menu = app(CreateMenuItem::class)->handle($request);
53
54
        Audit::activity()
55
            ->performedOn($menu)
56
            ->log('created');
57
58
        return redirect()->route('chief.back.menus.show', $menu->menu_type)->with('messages.success', $menu->label . ' is aangemaakt');
0 ignored issues
show
Bug introduced by
The property label does not seem to exist on Thinktomorrow\Chief\Site\Menu\MenuItem. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
59
    }
60
61
    /**
62
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
63
     */
64
    public function edit($id)
65
    {
66
        $this->authorize('update-page');
67
68
        $menuitem = MenuItem::findOrFail($id);
69
70
        $menuitems = $this->prepareMenuItemsForAdminSelect->prepare(
71
            Menu::tree(
72
                $menuitem->menuType(),
73
                config('app.fallback_locale')
0 ignored issues
show
Unused Code introduced by
The call to Thinktomorrow\Chief\Site\Menu\Menu::tree() has too many arguments starting with config('app.fallback_locale'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            Menu::/** @scrutinizer ignore-call */ 
74
                  tree(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
74
            ),
75
            $menuitem
76
        );
77
78
        return view('chief::admin.menu.edit', [
79
            'menuitem' => $menuitem,
80
            'pages' => UrlHelper::allOnlineModels(),
81
            'ownerReference' => $menuitem->owner ? $menuitem->owner->modelReference()->getShort() : null,
82
            'parents' => $menuitems,
83
        ]);
84
    }
85
86
    public function update(MenuRequest $request, $id)
87
    {
88
        $this->authorize('update-page');
89
90
        $menu = app(UpdateMenuItem::class)->handle($id, $request);
91
92
        Audit::activity()
93
            ->performedOn($menu)
94
            ->log('updated');
95
96
        return redirect()->route('chief.back.menus.show', $menu->menu_type)->with('messages.success', $menu->label . ' is aangepast');
0 ignored issues
show
Bug introduced by
The property label does not seem to exist on Thinktomorrow\Chief\Site\Menu\MenuItem. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
97
    }
98
99
    public function destroy($id)
100
    {
101
        $this->authorize('delete-page');
102
103
        $menuItem = app(DeleteMenuItem::class)->handle($id);
104
105
        if ($menuItem) {
0 ignored issues
show
introduced by
$menuItem is of type Thinktomorrow\Chief\Site\Menu\MenuItem, thus it always evaluated to true.
Loading history...
106
            $message = 'Het item werd verwijderd.';
107
108
            Audit::activity()
109
                ->performedOn($menuItem)
110
                ->log('deleted');
111
112
            return redirect()->route('chief.back.menus.show', $menuItem->menuType())->with('messages.warning', $message);
113
        } else {
114
            return redirect()->back()->with('messages.warning', 'Je menu item is niet verwijderd. Probeer opnieuw');
115
        }
116
    }
117
}
118