Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

MenuTwigExtension::getDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Kunstmaan\MenuBundle\Twig;
4
5
use Kunstmaan\MenuBundle\Entity\MenuItem;
6
use Kunstmaan\MenuBundle\Repository\MenuItemRepositoryInterface;
7
use Kunstmaan\MenuBundle\Service\RenderService;
8
9
class MenuTwigExtension extends \Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated with message: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
10
{
11
    /**
12
     * @var RenderService
13
     */
14
    private $renderService;
15
16
    /**
17
     * @var MenuItemRepositoryInterface
18
     */
19
    private $repository;
20
21
    /**
22
     * @param MenuItemRepositoryInterface $repository
23
     * @param RenderService               $renderService
24
     */
25
    public function __construct(MenuItemRepositoryInterface $repository, RenderService $renderService)
26
    {
27
        $this->renderService = $renderService;
28
        $this->repository = $repository;
29
    }
30
31
    /**
32
     * Returns a list of functions to add to the existing list.
33
     *
34
     * @return array An array of functions
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Twig_SimpleFunction[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
35
     */
36
    public function getFunctions()
37
    {
38
        return array(
39
            new \Twig_SimpleFunction(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
40
                'get_menu',
41
                array($this, 'getMenu'),
42
                array(
43
                    'is_safe' => array('html'),
44
                    'needs_environment' => true,
45
                )
46
            ),
47
            new \Twig_SimpleFunction('get_menu_items', array($this, 'getMenuItems')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
48
        );
49
    }
50
51
    /**
52
     * Get a html representation of a menu.
53
     *
54
     * @param string $name
55
     * @param string $lang
56
     * @param array  $options
57
     *
58
     * @return string
59
     */
60
    public function getMenu(\Twig_Environment $environment, $name, $lang, $options = array())
61
    {
62
        $options = array_merge($this->getDefaultOptions(), $options);
63
64
        $renderService = $this->renderService;
65
        $options['nodeDecorator'] = function ($node) use ($environment, $renderService, $options) {
66
            return $renderService->renderMenuItemTemplate($environment, $node, $options);
67
        };
68
69
        $arrayResult = $this->getMenuItems($name, $lang);
70
71
        return $this->repository->buildTree($arrayResult, $options);
72
    }
73
74
    /**
75
     * Get an array with menu items of a menu.
76
     *
77
     * @param string $name
78
     * @param string $lang
79
     *
80
     * @return array
81
     */
82
    public function getMenuItems($name, $lang)
83
    {
84
        /** @var MenuItem $menuRepo */
85
        $arrayResult = $this->repository->getMenuItemsForLanguage($name, $lang);
86
87
        // Make sure the parent item is not offline
88
        $foundIds = array();
89
        foreach ($arrayResult as $array) {
90
            $foundIds[] = $array['id'];
91
        }
92
        foreach ($arrayResult as $key => $array) {
93
            if (!\is_null($array['parent']) && !\in_array($array['parent']['id'], $foundIds)) {
94
                unset($arrayResult[$key]);
95
            }
96
        }
97
98
        return $arrayResult;
99
    }
100
101
    /**
102
     * Get the default options to render the html.
103
     *
104
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,boolean|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
105
     */
106
    private function getDefaultOptions()
107
    {
108
        return array(
109
            'decorate' => true,
110
            'rootOpen' => '<ul>',
111
            'rootClose' => '</ul>',
112
            'childOpen' => '<li>',
113
            'childClose' => '</li>',
114
        );
115
    }
116
}
117