Completed
Push — master ( a8dd61...33fc3e )
by Adrian
03:51
created

SidebarComposer::compose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 4
b 1
f 0
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 2
rs 9.4285
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
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'] = $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
            }
56
        }
57
58
        foreach ($models as $modelName => $model) {
59
            try {
60
                $item = $this->getModelItem($modelName);
61
            } catch (\Exception $e) {
62
                continue;
63
            }
64
            $menuItems[$modelName]['isActive'] =  $item['isActive'];
65
            $menuItems[$modelName]['name'] = $item['name'];
66
            $menuItems[$modelName]['items'][] = $item;
67
        }
68
69
        //Sort alphabetically de menu items
70
        usort($menuItems, function($itemA, $itemB)
71
        {
72
            return strcmp($itemA['name'],$itemB['name']);
73
        });
74
        return $menuItems;
75
    }
76
77
    /**
78
     * @param $modelName
79
     * @return array
80
     * @throws \Exception
81
     */
82
    private function getModelItem($modelName)
83
    {
84
        $modelAbstractor = $this->modelFactory->getByName($modelName);
85
        if (array_key_exists('authorize', $config = $modelAbstractor->getConfig()) && $config['authorize'] === true) {
86
            if (Gate::denies('adminIndex', $modelAbstractor->getInstance())) {
87
                throw new \Exception('Access denied');
88
            }
89
        }
90
91
        $isActive = false;
92
        if (Route::current()->getParameter('model') === $modelAbstractor->getSlug()) {
93
            $isActive = true;
94
        }
95
96
        $item = [
97
            'route' => route('anavel-crud.model.index', $modelAbstractor->getSlug()),
98
            'name' => $modelAbstractor->getName(),
99
            'isActive' => $isActive
100
        ];
101
102
        return $item;
103
    }
104
}
105