Completed
Push — master ( 786b39...7594a2 )
by wen
03:20
created

AdminMenu::getAdminMenu()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 27
rs 6.7272
c 1
b 0
f 1
ccs 0
cts 9
cp 0
cc 7
eloc 19
nc 6
nop 1
crap 56
1
<?php
2
3
namespace Sco\Admin\Http\Middleware;
4
5
use Closure;
6
use Route;
7
use Sco\Admin\Config\Factory as ConfigFactory;
8
9
class AdminMenu
10
{
11
    protected $configFactory;
12
13
    public function __construct(ConfigFactory $configFactory)
14
    {
15
        $this->configFactory = $configFactory;
16
    }
17
18
    /**
19
     * Handle an incoming request.
20
     *
21
     * @param \Illuminate\Http\Request $request
22
     * @param \Closure                 $next
23
     *
24
     * @return mixed
25
     */
26
    public function handle($request, Closure $next)
27
    {
28
        $request->attributes->set('admin.menu',
29
            $this->getAdminMenu(config('admin.menus')));
30
        return $next($request);
31
    }
32
33
    protected function getAdminMenu($list)
34
    {
35
        $menus = collect();
36
        foreach ($list as $key => $items) {
37
            if (is_string($items)) {
38
                $config = $this->configFactory->make($items);
39
                if ($config && $config->getAttribute('permission')) {
40
                    $menus->push([
41
                        'title' => $config->getAttribute('title'),
42
                        'url'   => $this->getRouteUrl('admin.' . $items),
43
                        'child' => [],
44
                    ]);
45
                }
46
            } elseif (is_array($items)) { // child
47
                $childs = $this->getAdminMenu($items);
48
                if ($childs->isNotEmpty()) {
49
                    $menu = [
50
                        'title' => $key,
51
                        'url'   => '/#',
52
                        'child' => $childs,
53
                    ];
54
                    $menus->push($menu);
55
                }
56
            }
57
        }
58
        return $menus;
59
    }
60
61
    private function getRouteUrl($name)
62
    {
63
        return Route::has($name) ? route($name, [], false)
64
            : ('/' . str_replace('.', '/', $name));
65
66
    }
67
}
68