Completed
Push — master ( 1fe708...6360f1 )
by Arnaud
16s queued 12s
created

CreateTopMenuListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

2 Methods

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