TopMenuBuilder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A createMenu() 0 46 5
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\Event\Events\MenuCreatedEvent;
10
use LAG\AdminBundle\Event\Events\MenuCreateEvent;
11
use LAG\AdminBundle\Event\MenuEvents;
12
use LAG\AdminBundle\Metadata\Index;
13
use LAG\AdminBundle\Request\Extractor\ParametersExtractorInterface;
14
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface;
15
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
18
19
class TopMenuBuilder
20
{
21
    public function __construct(
22
        private ParametersExtractorInterface $parametersExtractor,
23
        private ResourceRegistryInterface $registry,
24
        private RequestStack $requestStack,
25
        private FactoryInterface $factory,
26
        private RouteNameGeneratorInterface $routeNameGenerator,
27
        private EventDispatcherInterface $eventDispatcher,
28
    ) {
29
    }
30
31
    public function createMenu(array $options = []): ItemInterface
32
    {
33
        $menu = $this->factory->createItem('root', $options);
34
        $request = $this->requestStack->getMainRequest();
35
36
        if (!$this->parametersExtractor->supports($request)) {
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $request of LAG\AdminBundle\Request\...orInterface::supports() does only seem to accept Symfony\Component\HttpFoundation\Request, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        if (!$this->parametersExtractor->supports(/** @scrutinizer ignore-type */ $request)) {
Loading history...
37
            return $menu;
38
        }
39
        $resourceName = $this->parametersExtractor->getResourceName($request);
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $request of LAG\AdminBundle\Request\...face::getResourceName() does only seem to accept Symfony\Component\HttpFoundation\Request, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $resourceName = $this->parametersExtractor->getResourceName(/** @scrutinizer ignore-type */ $request);
Loading history...
40
        $operationName = $this->parametersExtractor->getOperationName($request);
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $request of LAG\AdminBundle\Request\...ace::getOperationName() does only seem to accept Symfony\Component\HttpFoundation\Request, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $operationName = $this->parametersExtractor->getOperationName(/** @scrutinizer ignore-type */ $request);
Loading history...
41
42
        $resource = $this->registry->get($resourceName);
43
        $operation = $resource->getOperation($operationName);
44
45
        $this->eventDispatcher->dispatch($event = new MenuCreateEvent($menu), MenuEvents::MENU_CREATE);
46
        $this->eventDispatcher->dispatch($event = new MenuCreateEvent($event->getMenu()), sprintf(
47
            MenuEvents::NAMED_EVENT_PATTERN,
48
            'top',
49
        ));
50
        $menu = $event->getMenu();
51
52
        if (!$operation instanceof Index) {
53
            return $menu;
54
        }
55
56
        foreach ($operation->getListActions() as $listAction) {
0 ignored issues
show
Bug introduced by
The method getListActions() does not exist on LAG\AdminBundle\Metadata\Index. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        foreach ($operation->/** @scrutinizer ignore-call */ getListActions() as $listAction) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
            $route = $listAction->getRouteName();
58
59
            if ($route === null) {
60
                $route = $this
61
                    ->routeNameGenerator
62
                    ->generateRouteName($resource, $resource->getOperation($listAction->getOperationName()))
63
                ;
64
            }
65
66
            $menu->addChild($listAction->getLabel(), [
67
                'route' => $route,
68
            ]);
69
        }
70
        $this->eventDispatcher->dispatch(new MenuCreatedEvent($menu), MenuEvents::MENU_CREATED);
71
        $this->eventDispatcher->dispatch(new MenuCreatedEvent($menu), sprintf(
72
            MenuEvents::NAMED_EVENT_PATTERN,
73
            'top',
74
        ));
75
76
        return $menu;
77
    }
78
}
79