Passed
Push — master ( 3ade87...7c706f )
by Iman
04:22
created

MenuRepo::where()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\MenuModule;
4
5
use crocodicstudio\crudbooster\helpers\CRUDBooster;
6
use Illuminate\Support\Facades\DB;
7
8
class MenuRepo
9
{
10
    public static function sidebarMenu()
11
    {
12
       $conditions = [
13
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
14
            'parent_id' => 0,
15
            'is_active' => 1,
16
            'is_dashboard'=> 0
17
        ];
18
        $menuActive = self::where($conditions)->orderby('sorting', 'asc')->get();
19
20
        foreach ($menuActive as &$menu) {
21
22
            $url = self::menuUrl($menu);
23
24
            $menu->url = $url;
25
            $menu->url_path = trim(str_replace(url('/'), '', $url), "/");
26
            $conditions = [
27
                ['is_dashboard', '=', 0],
28
                ['is_active', '=', 1],
29
                ['parent_id', '=', $menu->id],
30
                ['cms_privileges', 'like', '%"'.CRUDBooster::myPrivilegeName().'"%'],
0 ignored issues
show
Bug introduced by
Are you sure crocodicstudio\crudboost...ster::myPrivilegeName() of type Illuminate\Session\Store...on\SessionManager|mixed can be used in concatenation? ( Ignorable by Annotation )

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

30
                ['cms_privileges', 'like', '%"'./** @scrutinizer ignore-type */ CRUDBooster::myPrivilegeName().'"%'],
Loading history...
31
            ];
32
            $child = self::where($conditions)->orderby('sorting', 'asc')->get();
33
34
            if (count($child)) {
35
                foreach ($child as &$c) {
36
                    $url = self::menuUrl($c);
37
                    $c->url = $url;
38
                    $c->url_path = trim(str_replace(url('/'), '', $url), "/");
39
                }
40
            }
41
            $menu->children = $child;
42
        }
43
44
        return $menuActive;
45
    }
46
47
    private static function table()
48
    {
49
        return DB::table('cms_menus');
50
    }
51
52
    private static function menuUrl($menu)
53
    {
54
        $menu->is_broken = false;
55
        $menuType = $menu->type;
56
        if ($menuType == MenuTypes::route) {
57
            return route($menu->path);
58
        }
59
60
        if ($menuType == MenuTypes::url) {
61
            return $menu->path;
62
        }
63
64
        if ($menuType == MenuTypes::ControllerMethod) {
65
            return action($menu->path);
66
        }
67
68
        if (in_array($menuType, [MenuTypes::Module, MenuTypes::Statistic])) {
69
            return CRUDBooster::adminPath($menu->path);
70
        }
71
72
        $menu->is_broken = true;
73
74
        return '#';
75
    }
76
77
    public static function sidebarDashboard()
78
    {
79
        $conditions = [
80
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
81
            'is_dashboard' => 1,
82
            'is_active' => 1,
83
        ];
84
        $menu = self::where($conditions)->first() ?: new \stdClass();
85
86
        $menu->url = self::menuUrl($menu);
87
88
        return $menu;
89
    }
90
91
    public static function fetchMenuWithChilds($status = 1)
92
    {
93
        $menus = self::fetchMenu(0, $status);
94
95
        foreach ($menus as $menu) {
96
            $child = self::fetchMenu($menu->id, $status);
97
            if (count($child)) {
98
                $menu->children = $child;
99
            }
100
        }
101
102
        return $menus;
103
    }
104
105
    public static function fetchMenu($parent, $status = 1)
106
    {
107
        $conditions = [
108
            'parent_id' => $parent,
109
            'is_active' => $status,
110
        ];
111
        return self::where($conditions)->orderby('sorting', 'asc')->get();
112
    }
113
114
    public static function getDashboard()
115
    {
116
        return self::where(['is_dashboard' => 1])->first();
117
    }
118
119
    private static function where($conditions)
120
    {
121
        return self::table()->where($conditions);
122
    }
123
}