MenuManagerController::process()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 14
rs 10
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Settings;
4
5
use Illuminate\Http\Request;
6
use Uccello\Core\Http\Controllers\Core\Controller;
7
use Uccello\Core\Models\Domain;
8
use Uccello\Core\Models\Module;
9
use Uccello\Core\Models\Menu;
10
11
class MenuManagerController extends Controller
12
{
13
    /**
14
     * Default view name
15
     *
16
     * @var string
17
     */
18
    protected $viewName = 'menu-manager.main';
19
20
    /**
21
     * Check user permissions
22
     */
23
    protected function checkPermissions()
24
    {
25
        $this->middleware('uccello.permissions:admin');
26
    }
27
28
    /**
29
     * Display a settings dashboard
30
     *
31
     * @param \Uccello\Core\Models\Domain|null $domain
32
     * @param \Uccello\Core\Models\Module $module
33
     * @param \Illuminate\Http\Request $request
34
     * @return void
35
     */
36
    public function process(?Domain $domain, Module $module, Request $request)
37
    {
38
        // Pre-process
39
        $this->preProcess($domain, $module, $request);
40
41
        // Get main menu
42
        $mainMenu = $domain->mainMenu->data ?? null;
43
        $mainMenuJson = $this->addLinksAddedAfterMenuCreation($mainMenu, $this->domain->notAdminModules);
44
45
        // Get admin menu
46
        $adminMenu = $domain->adminMenu->data ?? null;
47
        $adminMenuJson = $this->addLinksAddedAfterMenuCreation($adminMenu, $this->domain->adminModules);
48
49
        return $this->autoView(compact('mainMenuJson', 'adminMenuJson'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->autoView(c...son', 'adminMenuJson')) returns the type Illuminate\View\View which is incompatible with the documented return type void.
Loading history...
50
    }
51
52
    /**
53
     * Store menu into database
54
     *
55
     * @param Domain|null $domain
56
     * @return \Uccello\Core\Models\Menu
57
     */
58
    public function store(?Domain $domain)
59
    {
60
        $menu = Menu::firstOrNew([
61
            'domain_id' => $domain->id,
62
            'user_id' => null,
63
            'type' => request('type'),
64
            ]);
65
        $menu->data = json_decode(request('structure'));
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Menu. 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...
Bug introduced by
It seems like request('structure') can also be of type array; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

65
        $menu->data = json_decode(/** @scrutinizer ignore-type */ request('structure'));
Loading history...
66
        $menu->save();
67
68
        return $menu;
69
    }
70
71
    /**
72
     * Reset domain menu into database
73
     *
74
     * @param Domain|null $domain
75
     * @return \Uccello\Core\Models\Menu
76
     */
77
    public function reset(?Domain $domain)
78
    {
79
        Menu::where('domain_id', $domain->id)->delete();
80
    }
81
82
    /**
83
     * Add to menu links, the links added after the creation of the menu
84
     * (e.g. new modules or modules activated after the creation)
85
     *
86
     * @param array $links
87
     * @param array $domainModules
88
     * @return array
89
     */
90
    protected function addLinksAddedAfterMenuCreation($links, $domainModules)
91
    {
92
        if (!empty($links)) {
93
            $addedModules = [ ];
94
            $this->getAddedModules($links, $addedModules);
95
96
            foreach ($domainModules as $module) {
97
                //TODO: Check needed capacity
98
                if (!auth()->user()->canRetrieve($this->domain, $module) || in_array($module->name, $addedModules)) {
99
                    continue;
100
                }
101
102
                foreach ($module->menuLinks as $link) {
103
                    $link->type = 'module';
104
                    $link->module = $module->name;
105
                    $link->color = 'grey';
106
                    $link->translation = uctrans($module->name, $module);
107
                    $links[ ] = $link;
108
                }
109
            }
110
        }
111
112
        return $links;
113
    }
114
115
    /**
116
     * Recursive function to get all modules present in the menu
117
     *
118
     * @param array $links
119
     * @param array $addedModules
120
     * @return void
121
     */
122
    protected function getAddedModules($links, &$addedModules)
123
    {
124
        foreach ($links as $link) {
125
            if (!empty($link->module) && !in_array($link->module, $addedModules)) {
126
                $addedModules[ ] = $link->module;
127
            }
128
129
            if (isset($link->children) && is_array($link->children)) {
130
                foreach ($link->children as $subLink) {
131
                    $this->getAddedModules($link->children, $addedModules);
132
                }
133
            }
134
        }
135
    }
136
}