Passed
Pull Request — master (#300)
by Arnaud
14:15 queued 08:05
created

TopMenuBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A createMenu() 0 23 4
1
<?php
2
3
namespace LAG\AdminBundle\Bridge\KnpMenu\Builder;
4
5
use Knp\Menu\FactoryInterface;
6
use Knp\Menu\ItemInterface;
7
use LAG\AdminBundle\Admin\Helper\AdminContextInterface;
8
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface;
9
use LAG\AdminBundle\Translation\Helper\TranslationHelperInterface;
10
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
11
12
class TopMenuBuilder implements MenuBuilderInterface
13
{
14
    use MenuBuilderTrait;
15
16
    public function __construct(
17
        private FactoryInterface $factory,
18
        private AdminContextInterface $adminContext,
19
        private RouteNameGeneratorInterface $routeNameGenerator,
20
        private TranslationHelperInterface $translationHelper,
21
        private EventDispatcherInterface $eventDispatcher,
22
    )
23
    {
24
    }
25
26
    public function createMenu(array $options = []): ItemInterface
27
    {
28
        $menu = $this->factory->createItem('root', $options);
29
30
        if (!$this->adminContext->hasAdmin()) {
31
            return $menu;
32
        }
33
        $admin = $this->adminContext->getAdmin();
34
        $action = $admin->getAction()->getName();
35
36
        if ($action === 'list') {
37
            if ($admin->getConfiguration()->hasAction('create')) {
38
                $menu->addChild($this->translationHelper->getTranslationKey('create', $admin->getName()), [
39
                    'route' => $this->routeNameGenerator->generateRouteName($admin->getName(), 'create'),
40
                    'extras' => [
41
                        'icon' => 'plus'
42
                    ],
43
                ]);
44
            }
45
        }
46
        $this->dispatchMenuEvents('top', $menu);
47
48
        return $menu;
49
    }
50
}
51