Passed
Push — master ( 5b852a...062ac3 )
by Iman
05:12
created

CreateMenuForNewModule::createParentMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator;
4
5
class CreateMenuForNewModule
6
{
7
    public function execute($ctrl, $name, $icon)
8
    {
9
        $parentMenuId = $this->createParentMenu($name, $icon);
10
        $this->createSubMenus($ctrl, $name, $parentMenuId);
11
    }
12
13
    /**
14
     * @param $name
15
     * @param $icon
16
     * @return mixed
17
     */
18
    private function createParentMenu($name, $icon)
19
    {
20
        $menu_sort = DB::table('cms_menus')->where('parent_id', 0)->max('sorting') + 1;
0 ignored issues
show
Bug introduced by
The type crocodicstudio\crudboost...ules\ModuleGenerator\DB was not found. Did you mean DB? If so, make sure to prefix the type with \.
Loading history...
21
        $menu_id = DB::table('cms_menus')->insertGetId([
22
            'created_at' => YmdHis(),
23
            'name' => $name,
24
            'icon' => $icon,
25
            'path' => '#',
26
            'type' => 'URL External',
27
            'is_active' => 1,
28
            'cms_privileges' => auth('cbAdmin')->user()->id_cms_privileges,
0 ignored issues
show
Bug introduced by
Accessing id_cms_privileges on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
29
            'sorting' => $menu_sort,
30
            'parent_id' => 0,
31
        ]);
32
33
        return $menu_id;
34
    }
35
36
    /**
37
     * @param $arr
38
     * @param $name
39
     * @param $ctrl
40
     */
41
    private function createAddLinkSubmenu($arr , $name, $ctrl)
42
    {
43
        DB::table('cms_menus')->insert([
44
                'name' => cbTrans('text_default_add_new_module', ['module' => $name]),
45
                'icon' => 'fa fa-plus',
46
                'path' => $ctrl.'GetAdd',
47
                'sorting' => 1,
48
            ] + $arr);
49
    }
50
51
    /**
52
     * @param $arr
53
     * @param $name
54
     * @param $ctrl
55
     */
56
    private function createIndexLinkSubMenu($arr, $name, $ctrl)
57
    {
58
        DB::table('cms_menus')->insert([
59
                'name' => cbTrans('text_default_list_module', ['module' => $name]),
60
                'icon' => 'fa fa-bars',
61
                'path' => $ctrl.'GetIndex',
62
                'sorting' => 2,
63
            ] + $arr);
64
    }
65
66
    /**
67
     * @param $ctrl
68
     * @param $name
69
     * @param $parentMenuId
70
     */
71
    private function createSubMenus($ctrl, $name, $parentMenuId)
72
    {
73
        $arr = [
74
            'created_at' => YmdHis(),
75
            'type' => 'Route',
76
            'is_active' => 1,
77
            'cms_privileges' => auth('cbAdmin')->user()->id_cms_privileges,
0 ignored issues
show
Bug introduced by
Accessing id_cms_privileges on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
78
            'parent_id' => $parentMenuId,
79
        ];
80
81
        $this->createAddLinkSubmenu($arr, $name, $ctrl);
82
        $this->createIndexLinkSubMenu($arr, $name, $ctrl);
83
    }
84
}