Completed
Pull Request — 2.0 (#31)
by Nicolas
02:57
created

MenuPresenter::setLocale()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace Modules\Menu\Presenters;
4
5
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
6
use Pingpong\Menus\MenuItem;
7
use Pingpong\Menus\Presenters\Presenter;
8
9
class MenuPresenter extends Presenter
10
{
11
    public function setLocale($item)
12
    {
13
        if (starts_with($item->url, 'http')) {
14
            return;
15
        }
16
        if (LaravelLocalization::hideDefaultLocaleInURL() === false) {
17
            $item->url = locale() . '/' . preg_replace('%^/?' . locale() . '/%', '$1', $item->url);
18
        }
19
    }
20
    /**
21
     * {@inheritdoc }.
22
     */
23
    public function getOpenTagWrapper()
24
    {
25
        return PHP_EOL . '<ul class="nav navbar-nav">' . PHP_EOL;
26
    }
27
28
    /**
29
     * {@inheritdoc }.
30
     */
31
    public function getCloseTagWrapper()
32
    {
33
        return PHP_EOL . '</ul>' . PHP_EOL;
34
    }
35
36
    /**
37
     * {@inheritdoc }.
38
     */
39
    public function getMenuWithoutDropdownWrapper($item)
40
    {
41
        $this->setLocale($item);
42
43
        return '<li' . $this->getActiveState($item) . '><a href="' . $item->getUrl() . '" ' . $item->getAttributes() . '>' . $item->getIcon() . ' ' . $item->title . '</a></li>' . PHP_EOL;
44
    }
45
46
    /**
47
     * {@inheritdoc }.
48
     */
49
    public function getActiveState($item, $state = ' class="active"')
50
    {
51
        return $item->isActive() ? $state : null;
52
    }
53
54
    /**
55
     * Get active state on child items.
56
     *
57
     * @param $item
58
     * @param string $state
59
     *
60
     * @return null|string
61
     */
62
    public function getActiveStateOnChild($item, $state = 'active')
63
    {
64
        return $item->hasActiveOnChild() ? $state : null;
65
    }
66
67
    /**
68
     * {@inheritdoc }.
69
     */
70 View Code Duplication
    public function getMenuWithDropDownWrapper($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...
71
    {
72
        return '<li class="dropdown' . $this->getActiveStateOnChild($item, ' active') . '">
73
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">
74
                    ' . $item->getIcon() . ' ' . $item->title . '
75
                    <b class="caret"></b>
76
                  </a>
77
                  <ul class="dropdown-menu">
78
                    ' . $this->getChildMenuItems($item) . '
79
                  </ul>
80
                </li>'
81
        . PHP_EOL;
82
    }
83
84
    /**
85
     * Get multilevel menu wrapper.
86
     *
87
     * @param MenuItem $item
88
     *
89
     * @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...
90
     */
91 View Code Duplication
    public function getMultiLevelDropdownWrapper($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...
92
    {
93
        return '<li class="dropdown' . $this->getActiveStateOnChild($item, ' active') . '">
94
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">
95
                    ' . $item->getIcon() . ' ' . $item->title . '
96
                    <b class="caret pull-right caret-right"></b>
97
                  </a>
98
                  <ul class="dropdown-menu">
99
                    ' . $this->getChildMenuItems($item) . '
100
                  </ul>
101
                </li>'
102
        . PHP_EOL;
103
    }
104
}
105