Completed
Push — master ( 7f45a1...6afdf1 )
by Andrey
01:18
created

MainMenu::setMenuItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Itstructure\AdminModule\widgets\menu;
4
5
use Yii;
6
use yii\base\Widget;
7
8
/**
9
 * Class MainMenu
10
 * Widget to render main menu.
11
 *
12
 * @property MainMenuItem[] $menuItems Main menu item config.
13
 *
14
 * @package Itstructure\AdminModule\widgets
15
 *
16
 * @author Andrey Girnik <[email protected]>
17
 */
18
class MainMenu extends Widget
19
{
20
    /**
21
     * Main menu item config.
22
     *
23
     * @var MainMenuItem[]
24
     */
25
    protected $menuItems = [];
26
27
    /**
28
     * @return MainMenuItem[]
29
     */
30
    public function getMenuItems()
31
    {
32
        return $this->menuItems;
33
    }
34
35
    /**
36
     * Main menu items setter.
37
     *
38
     * @param array|MainMenuItem[] $menuItems menu items config.
39
     *
40
     * @return $this
41
     */
42
    public function setMenuItems(array $menuItems)
43
    {
44
        $this->menuItems = [];
45
        foreach ($menuItems as $item) {
46
            $this->addMenuItem($item);
47
        }
48
        return $this;
49
    }
50
51
    /**
52
     * Create a MenuItem instance and fill menu items array.
53
     *
54
     * @param array|MainMenuItem $item
55
     *
56
     * @return void
57
     */
58 View Code Duplication
    public function addMenuItem($item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
59
    {
60
        if (is_array($item)) {
61
            if (!array_key_exists('class', $item)) {
62
                $item['class'] = MainMenuItem::class;
63
            }
64
            $item = Yii::createObject($item);
65
        }
66
        $this->menuItems[] = $item;
67
    }
68
69
    /**
70
     * Returns main menu template with rendered menu items.
71
     *
72
     * @return string.
0 ignored issues
show
Documentation introduced by
The doc-type string. could not be parsed: Unknown type name "string." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
73
     */
74
    public function run()
75
    {
76
        return $this->render('main-menu', [
77
            'menuItems' => $this->menuItems,
78
        ]);
79
    }
80
}
81