MenuRenderer::renderForMenu()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4286
cc 1
eloc 6
nc 1
nop 2
1
<?php namespace Modules\Menu\Services;
2
3
use Illuminate\Support\Facades\URL;
4
5
class MenuRenderer
6
{
7
    /**
8
     * @var int Id of the menu to render
9
     */
10
    protected $menuId;
11
    /**
12
     * @var string
13
     */
14
    private $startTag = '<div class="dd">';
15
    /**
16
     * @var string
17
     */
18
    private $endTag = '</div>';
19
    /**
20
     * @var string
21
     */
22
    private $menu = '';
23
24
    /**
25
     * @param $menuId
26
     * @param $menuItems
27
     * @return string
28
     */
29
    public function renderForMenu($menuId, $menuItems)
30
    {
31
        $this->menuId = $menuId;
32
33
        $this->menu .= $this->startTag;
34
        $this->generateHtmlFor($menuItems);
35
        $this->menu .= $this->endTag;
36
37
        return $this->menu;
38
    }
39
40
    /**
41
     * Generate the html for the given items
42
     * @param $items
43
     */
44
    private function generateHtmlFor($items)
45
    {
46
        $this->menu .= '<ol class="dd-list">';
47
        foreach ($items as $item) {
48
            $this->menu .= "<li class=\"dd-item\" data-id=\"{$item->id}\">";
49
            $editLink = URL::route('dashboard.menuitem.edit', [$this->menuId, $item->id]);
50
            $style = $item->isRoot() ? 'none' : 'inline';
51
            $this->menu .= <<<HTML
52
<div class="btn-group" role="group" aria-label="Action buttons" style="display: {$style}">
53
    <a class="btn btn-sm btn-info" style="float:left;" href="{$editLink}">
54
        <i class="fa fa-pencil"></i>
55
    </a>
56
    <a class="btn btn-sm btn-danger jsDeleteMenuItem" style="float:left; margin-right: 15px;" data-item-id="{$item->id}">
57
       <i class="fa fa-times"></i>
58
    </a>
59
</div>
60
HTML;
61
            $handleClass = $item->isRoot() ? 'dd-handle-root' : 'dd-handle';
62
            if (isset($item->icon) && $item->icon != '') {
63
                $this->menu .= "<div class=\"{$handleClass}\"><i class=\"{$item->icon}\" ></i> {$item->title}</div>";
64
            } else {
65
                $this->menu .= "<div class=\"{$handleClass}\">{$item->title}</div>";
66
            }
67
68
            if ($this->hasChildren($item)) {
69
                $this->generateHtmlFor($item->items);
70
            }
71
72
            $this->menu .= '</li>';
73
        }
74
        $this->menu .= '</ol>';
75
    }
76
77
    /**
78
     * @param $item
79
     * @return bool
80
     */
81
    private function hasChildren($item)
82
    {
83
        return count($item->items);
84
    }
85
}
86