Completed
Push — master ( 3e0546...23bd67 )
by wen
03:50
created

AdminMenu::getRouteUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
cc 3
eloc 6
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Sco\Admin\Http\Middleware;
4
5
use Auth;
6
use Closure;
7
use Route;
8
use Sco\Admin\Models\Permission;
9
10
class AdminMenu
11
{
12
    /**
13
     * Handle an incoming request.
14
     *
15
     * @param \Illuminate\Http\Request $request
16
     * @param \Closure                 $next
17
     *
18
     * @return mixed
19
     */
20
    public function handle($request, Closure $next)
21
    {
22
        $request->attributes->set('admin.menu', $this->getAdminMenu());
23
        return $next($request);
24
    }
25
26
    protected function getAdminMenu()
27
    {
28
        return $this->checkMenuPermission((new Permission())->getMenuList());
29
    }
30
31
    private function checkMenuPermission($list)
32
    {
33
        $return = $list->filter(function ($permission, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
            if (Auth::user()->can($permission->name)) {
35
                if (!$permission->child->isEmpty()) {
36
                    $permission->child = $this->checkMenuPermission($permission->child);
37
                }
38
                $permission->url = $this->getRouteUrl($permission);
39
                return $permission;
40
            }
41
        });
42
43
        return $return;
44
    }
45
46
    private function getRouteUrl($permission)
47
    {
48
        if ($permission->name == '#') {
49
            return '';
50
        } else {
51
            return Route::has($permission->name) ? route($permission->name, [], false)
52
                : ('/' . str_replace('.', '/', $permission->name));
53
        }
54
    }
55
}
56