ExtensionMenuCollector   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A add() 0 3 1
A get() 0 13 2
A has() 0 3 1
A getAllByContext() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ThemeBundle\ExtensionMenu;
15
16
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
17
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
18
19
class ExtensionMenuCollector
20
{
21
    /**
22
     * @var ExtensionMenuInterface[]
23
     */
24
    private array $extensionMenus;
25
26
    public function __construct(
27
        private readonly EventDispatcherInterface $eventDispatcher,
28
        #[TaggedIterator(ExtensionMenuInterface::class)]
29
        iterable $extensionMenus = []
30
    ) {
31
        $this->extensionMenus = [];
32
        foreach ($extensionMenus as $extensionMenu) {
33
            $this->add($extensionMenu);
34
        }
35
    }
36
37
    public function add(ExtensionMenuInterface $extensionMenu): void
38
    {
39
        $this->extensionMenus[$extensionMenu->getBundleName()] = $extensionMenu;
40
    }
41
42
    public function get(string $bundleName, string $context = ExtensionMenuInterface::CONTEXT_ADMIN): iterable
43
    {
44
        if ($this->has($bundleName)) {
45
            $menu = $this->extensionMenus[$bundleName]->get($context);
46
47
            // fire event here to add more menu items like additional services, etc
48
            $event = new ExtensionMenuEvent($bundleName, $context, $menu);
49
            $menu = $this->eventDispatcher->dispatch($event)->getMenu();
50
51
            return $menu;
52
        }
53
54
        return null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null returns the type null which is incompatible with the type-hinted return iterable.
Loading history...
55
    }
56
57
    public function getAllByContext(string $context = ExtensionMenuInterface::CONTEXT_ADMIN): array
58
    {
59
        $menus = [];
60
        foreach ($this->extensionMenus as $bundleName => $extensionMenu) {
61
            $menu = $extensionMenu->get($context);
62
            if (null !== $menu) {
63
                $menus[$bundleName] = $menu;
64
            }
65
        }
66
67
        return $menus;
68
    }
69
70
    public function has(string $bundleName): bool
71
    {
72
        return isset($this->extensionMenus[$bundleName]);
73
    }
74
}
75