SidebarComposer::getSidebarItems()   C
last analyzed

Complexity

Conditions 11
Paths 6

Size

Total Lines 50
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 32
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 50
ccs 0
cts 38
cp 0
crap 132
rs 5.4893

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
namespace Anavel\Crud\View\Composers;
4
5
use Anavel\Crud\Contracts\Abstractor\ModelFactory as ModelAbstractorFactory;
6
use Gate;
7
use Route;
8
9
class SidebarComposer
10
{
11
    protected $modelFactory;
12
13
    public function __construct(ModelAbstractorFactory $modelFactory)
14
    {
15
        $this->modelFactory = $modelFactory;
16
    }
17
18
    public function compose($view)
19
    {
20
        $menuItems = $this->getSidebarItems();
21
        $view->with([
22
            'menuItems'  => $menuItems,
23
            'headerName' => config('anavel-crud.name'),
24
        ]);
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    private function getSidebarItems()
31
    {
32
        $modelsGroups = config('anavel-crud.modelsGroups');
33
        $models = config('anavel-crud.models');
34
        $menuItems = [];
35
36
        if (!is_null($modelsGroups)) {
37
            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...
38
                $menuItems[$group]['isActive'] = false;
39
                $menuItems[$group]['name'] = transcrud($group);
40
                $menuItems[$group]['items'] = [];
41
                foreach ($items as $itemName) {
42
                    try {
43
                        $item = $this->getModelItem($itemName);
44
                        $menuItems[$group]['items'][] = $item;
45
                        if (!$menuItems[$group]['isActive'] && $item['isActive']) {
46
                            $menuItems[$group]['isActive'] = true;
47
                        }
48
                        if (isset($models[$itemName])) {
49
                            unset($models[$itemName]);
50
                        }
51
                    } catch (\Exception $e) {
52
                        continue;
53
                    }
54
                }
55
                // Remove empty groups (resulting, most probably, of different permissions)
56
                if (count($menuItems[$group]['items']) < 1) {
57
                    unset($menuItems[$group]);
58
                }
59
            }
60
        }
61
62
        foreach ($models as $modelName => $model) {
63
            try {
64
                $item = $this->getModelItem($modelName);
65
            } catch (\Exception $e) {
66
                continue;
67
            }
68
            $menuItems[$modelName]['isActive'] = $item['isActive'];
69
            $menuItems[$modelName]['name'] = $item['name'];
70
            $menuItems[$modelName]['items'][] = $item;
71
        }
72
73
        //Sort alphabetically de menu items
74
        usort($menuItems, function ($itemA, $itemB) {
75
            return strcmp($itemA['name'], $itemB['name']);
76
        });
77
78
        return $menuItems;
79
    }
80
81
    /**
82
     * @param $modelName
83
     *
84
     * @throws \Exception
85
     *
86
     * @return array
87
     */
88
    private function getModelItem($modelName)
89
    {
90
        $modelAbstractor = $this->modelFactory->getByName($modelName);
91
        if (array_key_exists('authorize', $config = $modelAbstractor->getConfig()) && $config['authorize'] === true) {
92
            if (Gate::denies('adminIndex', $modelAbstractor->getInstance())) {
93
                throw new \Exception('Access denied');
94
            }
95
        }
96
97
        $isActive = false;
98
        if (Route::current()->getParameter('model') === $modelAbstractor->getSlug()) {
99
            $isActive = true;
100
        }
101
102
        $item = [
103
            'route'    => route('anavel-crud.model.index', $modelAbstractor->getSlug()),
104
            'name'     => $modelAbstractor->getName(),
105
            'isActive' => $isActive,
106
        ];
107
108
        return $item;
109
    }
110
}
111