Passed
Pull Request — master (#374)
by Arnaud
32:43 queued 25:11
created

ResourceMenuBuilder::buildMenu()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 19
rs 9.5555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Bridge\KnpMenu\Builder;
6
7
use Knp\Menu\FactoryInterface;
8
use Knp\Menu\ItemInterface;
9
use LAG\AdminBundle\Metadata\Index;
10
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface;
11
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface;
12
use Symfony\Component\String\Inflector\EnglishInflector;
13
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
14
15
use function Symfony\Component\String\u;
16
17
class ResourceMenuBuilder extends AbstractMenuBuilder
18
{
19
    public function __construct(
20
        private ResourceRegistryInterface $resourceRegistry,
21
        private RouteNameGeneratorInterface $routeNameGenerator,
22
        FactoryInterface $factory,
23
        EventDispatcherInterface $eventDispatcher,
24
    ) {
25
        parent::__construct($factory, $eventDispatcher);
26
    }
27
28
    public function getName(): string
29
    {
30
        return 'resource';
31
    }
32
33
    protected function buildMenu(ItemInterface $menu): void
34
    {
35
        $inflector = new EnglishInflector();
36
37
        if ($menu->hasChildren()) {
38
            return;
39
        }
40
41
        foreach ($this->resourceRegistry->all() as $resource) {
42
            foreach ($resource->getOperations() as $operation) {
43
                if (!$operation instanceof Index) {
44
                    continue;
45
                }
46
                $label = $inflector->pluralize(u($resource->getName())->snake()->toString())[0];
47
                $route = $this->routeNameGenerator->generateRouteName($resource, $operation);
48
49
                $menu
50
                    ->addChild($label, ['route' => $route])
51
                    ->setLabel('lag_admin.menu.'.$label)
52
                ;
53
            }
54
        }
55
    }
56
}
57