Completed
Push — master ( 0cd01e...687a6e )
by Beñat
02:45
created

MenuRendererFunction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel package.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\CMSKernel\Infrastructure\Symfony\Bundle\Twig;
13
14
use LIN3S\CMSKernel\Application\Query\Menu\MenuOfCodeHandler;
15
use LIN3S\CMSKernel\Application\Query\Menu\MenuOfCodeQuery;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
18
/**
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
class MenuRendererFunction extends \Twig_Extension
22
{
23
    private $requestStack;
24
    private $menuOfCodeHandler;
25
26
    public function __construct(RequestStack $requestStack, MenuOfCodeHandler $menuOfCodeHandler)
27
    {
28
        $this->requestStack = $requestStack;
29
        $this->menuOfCodeHandler = $menuOfCodeHandler;
30
    }
31
32
    public function getFunctions()
33
    {
34
        return [
35
            new \Twig_SimpleFunction('lin3sCmsMenu', [$this, 'menu']),
36
        ];
37
    }
38
39
    public function menu($menuCode, $locale = null)
40
    {
41
        if (null === $locale) {
42
            $request = $this->requestStack->getMasterRequest();
43
            $locale = $request->getLocale();
44
        }
45
46
        $menu = $this->menuOfCodeHandler->__invoke(
47
            new MenuOfCodeQuery(
48
                $menuCode,
49
                $locale
50
            )
51
        );
52
53
        return $menu['tree'];
54
    }
55
}
56