Completed
Push — master ( 3f296a...337f79 )
by Adrian
11:40 queued 04:56
created

SidebarComposer::getSidebarItems()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 10
eloc 30
c 2
b 1
f 0
nc 6
nop 0
dl 0
loc 45
ccs 0
cts 35
cp 0
crap 110
rs 4.8196

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
namespace Anavel\Crud\View\Composers;
3
4
use Anavel\Crud\Contracts\Abstractor\ModelFactory as ModelAbstractorFactory;
5
use Route;
6
use Gate;
7
8
class SidebarComposer
9
{
10
    protected $modelFactory;
11
12
    public function __construct(ModelAbstractorFactory $modelFactory)
13
    {
14
        $this->modelFactory = $modelFactory;
15
    }
16
17
    public function compose($view)
18
    {
19
        $menuItems = $this->getSidebarItems();
20
        $view->with([
21
            'menuItems' => $menuItems,
22
            'headerName' => config('anavel-crud.name')
23
        ]);
24
    }
25
26
    /**
27
     * @return array
28
     */
29
    private function getSidebarItems()
30
    {
31
        $modelsGroups = config('anavel-crud.modelsGroups');
32
        $models = config('anavel-crud.models');
33
        $menuItems = [];
34
35
        if (!is_null($modelsGroups)) {
36
            foreach ($modelsGroups as $group => $items) {
0 ignored issues
show
Bug introduced by
The expression $modelsGroups of type object|integer|double|string|array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
37
                $menuItems[$group]['isActive'] = false;
38
                $menuItems[$group]['name'] = $group;
39
                $menuItems[$group]['items'] = [];
40
                foreach ($items as $itemName) {
41
                    try {
42
                        $item = $this->getModelItem($itemName);
43
                        $menuItems[$group]['items'][] = $item;
44
                        if (!$menuItems[$group]['isActive'] && $item['isActive']) {
45
                            $menuItems[$group]['isActive'] = true;
46
                        }
47
                        if (isset($models[$itemName])) {
48
                            unset($models[$itemName]);
49
                        }
50
                    } catch (\Exception $e) {
51
                        continue;
52
                    }
53
                }
54
            }
55
        }
56
57
        foreach ($models as $modelName => $model) {
58
            try {
59
                $item = $this->getModelItem($modelName);
60
            } catch (\Exception $e) {
61
                continue;
62
            }
63
            $menuItems[$modelName]['isActive'] =  $item['isActive'];
64
            $menuItems[$modelName]['name'] = $item['name'];
65
            $menuItems[$modelName]['items'][] = $item;
66
        }
67
68
        //Sort alphabetically de menu items
69
        usort($menuItems, function ($itemA, $itemB) {
70
            return strcmp($itemA['name'], $itemB['name']);
71
        });
72
        return $menuItems;
73
    }
74
75
    /**
76
     * @param $modelName
77
     * @return array
78
     * @throws \Exception
79
     */
80
    private function getModelItem($modelName)
81
    {
82
        $modelAbstractor = $this->modelFactory->getByName($modelName);
83
        if (array_key_exists('authorize', $config = $modelAbstractor->getConfig()) && $config['authorize'] === true) {
84
            if (Gate::denies('adminIndex', $modelAbstractor->getInstance())) {
85
                throw new \Exception('Access denied');
86
            }
87
        }
88
89
        $isActive = false;
90
        if (Route::current()->getParameter('model') === $modelAbstractor->getSlug()) {
91
            $isActive = true;
92
        }
93
94
        $item = [
95
            'route' => route('anavel-crud.model.index', $modelAbstractor->getSlug()),
96
            'name' => $modelAbstractor->getName(),
97
            'isActive' => $isActive
98
        ];
99
100
        return $item;
101
    }
102
}
103