Passed
Pull Request — master (#116)
by Arnaud
03:38
created

MenuExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasMenu() 0 3 1
A __construct() 0 4 1
A getMenu() 0 7 1
A getFunctions() 0 5 1
1
<?php
2
3
namespace LAG\AdminBundle\Bridge\Twig\Extension;
4
5
use LAG\AdminBundle\Factory\MenuFactory;
6
use LAG\AdminBundle\View\ViewInterface;
7
use Twig\Environment;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFunction;
10
11
class MenuExtension extends AbstractExtension
12
{
13
    /**
14
     * @var MenuFactory
15
     */
16
    private $menuFactory;
17
18
    /**
19
     * @var Environment
20
     */
21
    private $environment;
22
23
    public function __construct(MenuFactory $menuFactory, Environment $environment)
24
    {
25
        $this->menuFactory = $menuFactory;
26
        $this->environment = $environment;
27
    }
28
29
    public function getFunctions(): array
30
    {
31
        return [
32
            new TwigFunction('admin_menu', [$this, 'getMenu'], ['is_safe' => ['html']]),
33
            new TwigFunction('admin_has_menu', [$this, 'hasMenu']),
34
        ];
35
    }
36
37
    /**
38
     * Render a menu according to given name.
39
     *
40
     * @param string             $name
41
     * @param ViewInterface|null $view
42
     *
43
     * @return string
44
     */
45
    public function getMenu(string $name, ViewInterface $view = null)
46
    {
47
        $menu = $this->menuFactory->getMenu($name);
48
49
        return $this->environment->render($menu->get('template'), [
50
            'menu' => $menu,
51
            'admin' => $view,
52
        ]);
53
    }
54
55
    /**
56
     * Return true if a menu with the given name exists.
57
     *
58
     * @param string $name
59
     *
60
     * @return bool
61
     */
62
    public function hasMenu(string $name): bool
63
    {
64
        return $this->menuFactory->hasMenu($name);
65
    }
66
}
67