Passed
Pull Request — master (#259)
by Arnaud
08:22
created

CreateTopMenuListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 20 4
A __construct() 0 8 1
1
<?php
2
3
namespace LAG\AdminBundle\Event\Listener\Menu;
4
5
use LAG\AdminBundle\Admin\Helper\AdminHelperInterface;
6
use LAG\AdminBundle\Event\Events\MenuEvent;
7
use LAG\AdminBundle\Menu\Factory\MenuItemFactoryInterface;
8
use LAG\AdminBundle\Translation\Helper\TranslationHelperInterface;
9
10
class CreateTopMenuListener
11
{
12
    private AdminHelperInterface $adminHelper;
13
    private TranslationHelperInterface $translationHelper;
14
    private MenuItemFactoryInterface $menuItemFactory;
15
16
    public function __construct(
17
        AdminHelperInterface $adminHelper,
18
        TranslationHelperInterface $translationHelper,
19
        MenuItemFactoryInterface $menuItemFactory
20
    ) {
21
        $this->adminHelper = $adminHelper;
22
        $this->translationHelper = $translationHelper;
23
        $this->menuItemFactory = $menuItemFactory;
24
    }
25
26
    public function __invoke(MenuEvent $event): void
27
    {
28
        if ($event->getMenuName() !== 'top' || !$this->adminHelper->hasAdmin()) {
29
            return;
30
        }
31
        $admin = $this->adminHelper->getAdmin();
32
        $menu = $event->getMenu();
33
34
        // Do not add the return link when we already are on the list action
35
        if (!$admin->getAction()->getConfiguration()->shouldAddReturnLink()) {
36
            return;
37
        }
38
        $child = $this->menuItemFactory->create('return', [
39
            'admin' => $admin->getName(),
40
            'action' => 'list',
41
            'text' => $this->translationHelper->transWithPattern('return', [], null, null, null, 'ui'),
42
            'icon' => 'fas fa-arrow-left',
43
            'linkAttributes' => ['class' => 'btn btn-info btn-icon-split btn-sm'],
44
        ]);
45
        $menu->addChild($child);
46
    }
47
}
48