Completed
Push — master ( 74da27...ac625a )
by wen
12:26
created

Admin::getMenus()   D

Complexity

Conditions 10
Paths 16

Size

Total Lines 38
Code Lines 27

Duplication

Lines 28
Ratio 73.68 %

Importance

Changes 0
Metric Value
dl 28
loc 38
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 27
nc 16
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace Sco\Admin;
5
6
use Auth;
7
use Route;
8
use Illuminate\Foundation\Application;
9
use Sco\Admin\Contracts\Admin as AdminContract;
10
use Illuminate\Config\Repository as ConfigRepository;
11
12
class Admin implements AdminContract
13
{
14
    /**
15
     * @var \Illuminate\Foundation\Application
16
     */
17
    protected $app;
18
19
    /**
20
     * @var \Illuminate\Config\Repository
21
     */
22
    protected $config;
23
24
    public function __construct(Application $app)
25
    {
26
        $this->app = $app;
27
        $this->config = new ConfigRepository(
28
            $this->app['config']->get('admin', [])
29
        );
30
    }
31
32
    public function getMenus($list = null)
33
    {
34
        $list = $list ?: $this->config->get('menus');
35
        $menus = collect();
36
        foreach ($list as $key => $items) {
37
            if (is_string($items)) {
38
                if (Route::has($items)) {
39 View Code Duplication
                    if (Auth::user()->can($items)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
                        $menus->push([
41
                            'title' => $key,
42
                            'url'   => route($items, [], false),
43
                            'child' => [],
44
                        ]);
45
                    }
46 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
                    $config = $this->getConfig($items);
48
                    if ($config && $config->getAttribute('permissions.view')) {
49
                        $menus->push([
50
                            'title' => $config->getAttribute('title'),
51
                            'url'   => route('admin.model.index', ['model' => $items], false),
52
                            'child' => [],
53
                        ]);
54
                    }
55
                }
56 View Code Duplication
            } elseif (is_array($items)) { // child
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
                $childs = $this->getMenus($items);
58
                if ($childs->isNotEmpty()) {
59
                    $menu = [
60
                        'title' => $key,
61
                        'url'   => '/#',
62
                        'child' => $childs,
63
                    ];
64
                    $menus->push($menu);
65
                }
66
            }
67
        }
68
        return $menus;
69
    }
70
71
    public function getConfig($name)
72
    {
73
        return $this->app['admin.config.factory']->make($name);
74
    }
75
}
76