Completed
Push — master ( 2abc3b...5aa123 )
by Alexis
16:40
created

MenuBuilder::isValidMachineNamme()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\MenuBundle\Builder;
4
5
use Alpixel\Bundle\MenuBundle\Exception\LocaleException;
6
use Alpixel\Bundle\MenuBundle\Model\ItemInterface;
7
use Doctrine\ORM\EntityManager;
8
use Knp\Menu\FactoryInterface;
9
use Knp\Menu\MenuItem as KnpMenuItem;
10
use UnexpectedValueException;
11
12
class MenuBuilder
13
{
14
    protected $entityManager;
15
    protected $factory;
16
    protected $knpMenu;
17
    protected $defaultLocale;
18
19
20
    /**
21
     * MenuBuilder constructor.
22
     *
23
     * @param EntityManager $entityManager
24
     * @param FactoryInterface $factory
25
     */
26
    function __construct(EntityManager $entityManager, FactoryInterface $factory)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28
        $this->entityManager = $entityManager;
29
        $this->factory       = $factory;
30
        $this->knpMenu       = null;
31
    }
32
33
    /**
34
     * Check if locale is valid
35
     *
36
     * @param $locale
37
     * @return bool
38
     */
39
    protected static function isValidLocale($locale)
40
    {
41
        if (is_string($locale) && !empty($locale)) {
42
            return true;
43
        }
44
45
        return false;
46
    }
47
48
    /**
49
     * The parameter $locale must be defined in your
50
     * symfony configuration file under parameters.
51
     *
52
     * @param $locale String
53
     * @return $this
54
     */
55
    public function setDefaultLocale($locale)
56
    {
57
        if(self::isValidLocale($locale)) {
58
            $this->defaultLocale = $locale;
59
60
            return $this;
61
        }
62
63
        throw new LocaleException('
64
            The $locale parameter must be a non empty string or the locale is not defined
65
            under the Symfony parameters configuration.
66
        ');
67
    }
68
69
    /**
70
     * Check if the machineName is valid
71
     *
72
     * @param $machineName
73
     * @return bool
74
     */
75
    public static function isValidMachineNamme($machineName)
76
    {
77
        if (is_string($machineName) && !empty($machineName)) {
78
            return true;
79
        }
80
81
        return false;
82
    }
83
84
    /**
85
     * Retrun null or a KnpMenuItem instance
86
     *
87
     * @return null|KnpMenuItem
88
     */
89
    public function getKnpMenu()
90
    {
91
        return $this->knpMenu;
92
    }
93
94
    /**
95
     * Set the KnpMenuItem instance
96
     *
97
     * @param KnpMenuItem $knpMenu
98
     * @return $this
99
     */
100
    public function setKnpMenu(KnpMenuItem $knpMenu)
101
    {
102
        $this->knpMenu = $knpMenu;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Create KnpMenuItem
109
     *
110
     * @param  string $machineName The name of menu
111
     * @param  string $locale      Language code (Recommanded ISO-639)
112
     *
113
     * @return KnpMenuItem         Get formatted menu
114
     */
115
    public function createKnpMenu($machineName, $locale = null)
116
    {
117
        if (!self::isValidMachineNamme($machineName)) {
118
            throw new UnexpectedValueException('The parameter $machineName must be a non empty string');
119
        }
120
121
        if ($locale === null) {
122
            $locale = $this->defaultLocale;
123
        } else if (!self::isValidLocale($locale)) {
124
            throw new LocaleException();
125
        }
126
127
        $repository = $this->entityManager->getRepository('AlpixelMenuBundle:Menu');
128
        $menu       = $repository->findOneMenuByMachineNameAndLocale($machineName, $locale);
129
        $items      = $menu->getItems()->toArray();
130
131
        $this->setKnpMenu($this->factory->createItem('root'));
0 ignored issues
show
Compatibility introduced by
$this->factory->createItem('root') of type object<Knp\Menu\ItemInterface> is not a sub-type of object<Knp\Menu\MenuItem>. It seems like you assume a concrete implementation of the interface Knp\Menu\ItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
132
        foreach ($items as $item) {
133
            if($item->getParent() === null) {
134
                $this->getTree($this->knpMenu, $item);
0 ignored issues
show
Bug introduced by
It seems like $this->knpMenu can be null; however, getTree() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
135
            }
136
        }
137
138
        return $this->getKnpMenu();
139
    }
140
141
    /**
142
     * Create tree un KnpMenuItem
143
     *
144
     * @param  KnpMenuItem      $knpMenu
145
     * @param  ItemInterface    $item
146
     * @param  KnpMenuItem|null $parent
147
     *
148
     * @return KnpMenuItem      A formatted KnpMenu
149
     */
150
    protected function getTree(KnpMenuItem $knpMenu, ItemInterface $item, KnpMenuItem $parent = null)
151
    {
152
        $menuItem = ($parent === null) ? $knpMenu->addChild($item) : $parent->addChild($item);
153
154
        if ($uri = $item->getUri() !== null) {
155
            $menuItem->setUri($uri);
0 ignored issues
show
Documentation introduced by
$uri is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
156
        }
157
158
        foreach ($item->getChidlren() as $child) {
159
            $this->getTree($knpMenu, $child, $menuItem);
0 ignored issues
show
Documentation introduced by
$menuItem is of type object<Knp\Menu\ItemInterface>, but the function expects a null|object<Knp\Menu\MenuItem>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
        }
161
162
        return $menuItem;
163
    }
164
}
165