Passed
Push — master ( 062ac3...61cc62 )
by Iman
04:02
created

CreateMenuForNewModule::table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 = $this->table()->where('parent_id', 0)->max('sorting') + 1;
21
        $menu_id = $this->table()->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
        $this->table()->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
        $this->table()->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
85
    /**
86
     * @return mixed
87
     */
88
    private function table()
89
    {
90
        return \DB::table('cms_menus');
91
    }
92
}