Completed
Push — master ( 1e6f86...3701fc )
by wen
02:30
created

Admin::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
4
namespace Sco\Admin;
5
6
use Auth;
7
use Illuminate\Support\Collection;
8
use Route;
9
use Illuminate\Foundation\Application;
10
use Sco\Admin\Config\ModelFactory;
11
use Illuminate\Config\Repository as ConfigRepository;
12
use Sco\Admin\Contracts\AdminInterface;
13
use Sco\Admin\Contracts\ConfigFactoryInterface;
14
15
class Admin implements AdminInterface
16
{
17
    /**
18
     * @var \Illuminate\Foundation\Application
19
     */
20
    protected $app;
21
22
    /**
23
     * @var \Illuminate\Config\Repository
24
     */
25
    protected $config;
26
27
    protected $models;
28
29
    public function __construct(Application $app)
30
    {
31
        $this->app    = $app;
32
        $this->config = new ConfigRepository(
33
            $this->app['config']->get('admin', [])
34
        );
35
36
        $this->models = new Collection();
37
    }
38
39
    public function getMenus($list = null)
40
    {
41
        $list  = $list ?: $this->config->get('menus');
42
        $menus = collect();
43
        foreach ($list as $key => $items) {
44
            if (is_string($items)) {
45
                if (Route::has($items)) {
46
                    if (Auth::user()->can($items)) {
47
                        $menus->push([
48
                            'title' => $key,
49
                            'url'   => route($items, [], false),
50
                            'child' => [],
51
                        ]);
52
                    }
53
                } else {
54
                    $config = $this->app[ConfigFactoryInterface::class]->make($items);
55
                    if ($config && $config->getPermissions()->isViewable()) {
56
                        $menus->push([
57
                            'title' => $config->getTitle(),
58
                            'url'   => route(
59
                                'admin.model.index',
60
                                ['model' => $items],
61
                                false
62
                            ),
63
                            'child' => [],
64
                        ]);
65
                    }
66
                }
67
            } elseif (is_array($items)) { // child
68
                $childs = $this->getMenus($items);
69
                if ($childs->isNotEmpty()) {
70
                    $menu = [
71
                        'title' => $key,
72
                        'url'   => '/#',
73
                        'child' => $childs,
74
                    ];
75
                    $menus->push($menu);
76
                }
77
            }
78
        }
79
        return $menus;
80
    }
81
}
82