Completed
Push — master ( e84808...5a9d2e )
by Oleg
07:58
created

Menu   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.89%

Importance

Changes 10
Bugs 0 Features 1
Metric Value
wmc 12
c 10
b 0
f 1
lcom 1
cbo 2
dl 0
loc 106
ccs 32
cts 36
cp 0.8889
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 8 3
A render() 0 4 1
A has() 0 4 1
A make() 0 14 1
A forget() 0 6 2
A fromArray() 0 14 1
A toArray() 0 8 2
1
<?php
2
3
namespace Malezha\Menu;
4
5
use Illuminate\Contracts\Container\Container;
6
use Malezha\Menu\Contracts\Attributes;
7
use Malezha\Menu\Contracts\Menu as MenuContract;
8
use Malezha\Menu\Contracts\Builder as BuilderContract;
9
10
/**
11
 * Class Menu
12
 * @package Malezha\Menu
13
 */
14
class Menu implements MenuContract
15
{
16
    /**
17
     * @var BuilderContract[]
18
     */
19
    protected $menuList = [];
20
21
    /**
22
     * @var Container
23
     */
24
    protected $container;
25
26
    /**
27
     * @inheritDoc
28
     */
29 7
    public function __construct(Container $container)
30
    {
31 7
        $this->container = $container;
32 7
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37 4
    public function make($name, callable $callback, $type = Builder::UL, $attributes = [], $activeAttributes = [])
38
    {
39 4
        $menu = $this->container->make(BuilderContract::class, [
40 4
            'container' => $this->container, 
41 4
            'name' => $name, 
42 4
            'type' => $type, 
43 4
            'attributes' => $this->container->make(Attributes::class, ['attributes' => $attributes]), 
44 4
            'activeAttributes' => $this->container->make(Attributes::class, ['attributes' => $activeAttributes]),
45
        ]);
46 4
        call_user_func($callback, $menu);
47 4
        $this->menuList[$name] = $menu;
48
49 4
        return $menu;
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55 4
    public function get($name)
56
    {
57 4
        if (array_key_exists($name, $this->menuList) && ($menu = $this->menuList[$name]) instanceof BuilderContract) {
58 3
            return $menu;
59
        }
60
61 1
        throw new \RuntimeException('Menu not found');
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67 1
    public function render($name, $view = null)
68
    {
69 1
        return $this->get($name)->render($view);
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 2
    public function has($name)
76
    {
77 2
        return array_key_exists($name, $this->menuList);
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83 1
    public function forget($name)
84
    {
85 1
        if ($this->has($name)) {
86 1
            unset($this->menuList[$name]);
87
        }
88 1
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93 1
    public function fromArray($name, array $builder)
94
    {
95
        /** @var BuilderContract $menu */
96 1
        $menu = $this->container->make(BuilderContract::class, [
97 1
            'attributes' => $this->container->make(Attributes::class, ['attributes' => []]),
98 1
            'activeAttributes' => $this->container->make(Attributes::class, ['attributes' => []]),
99
        ]);
100
        
101 1
        $menu = $menu->fromArray($builder);
102
103 1
        $this->menuList[$name] = $menu;
104
        
105 1
        return $menu;
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function toArray($name)
112
    {
113
        if (!$this->has($name)) {
114
            throw new \RuntimeException("Menu not found");
115
        }
116
        
117
        return $this->menuList[$name]->toArray();
118
    }
119
}