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

ContextualMenuBuilder::buildMenu()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 22
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 34
rs 8.6346
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\AdminResource;
10
use LAG\AdminBundle\Metadata\OperationInterface;
11
use LAG\AdminBundle\Request\Extractor\ParametersExtractorInterface;
12
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface;
13
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
17
18
class ContextualMenuBuilder extends AbstractMenuBuilder
19
{
20
    public function __construct(
21
        private ParametersExtractorInterface $parametersExtractor,
22
        private ResourceRegistryInterface $registry,
23
        private RequestStack $requestStack,
24
        private RouteNameGeneratorInterface $routeNameGenerator,
25
        FactoryInterface $factory,
26
        EventDispatcherInterface $eventDispatcher,
27
    ) {
28
        parent::__construct($factory, $eventDispatcher);
29
    }
30
31
    public function getName(): string
32
    {
33
        return 'contextual';
34
    }
35
36
    protected function buildMenu(ItemInterface $menu): void
37
    {
38
        $request = $this->requestStack->getMainRequest();
39
40
        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

40
        if (!$this->parametersExtractor->supports(/** @scrutinizer ignore-type */ $request)) {
Loading history...
41
            return;
42
        }
43
        $resource = $this->getResource($request);
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $request of LAG\AdminBundle\Bridge\K...uBuilder::getResource() 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

43
        $resource = $this->getResource(/** @scrutinizer ignore-type */ $request);
Loading history...
44
        $operation = $this->getOperation($resource, $request);
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $request of LAG\AdminBundle\Bridge\K...Builder::getOperation() 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

44
        $operation = $this->getOperation($resource, /** @scrutinizer ignore-type */ $request);
Loading history...
45
46
        foreach ($operation->getContextualActions() as $action) {
47
            $contextualResource = $resource;
48
49
            if ($action->getResourceName() !== $resource->getName()) {
50
                $contextualResource = $this->registry->get($action->getResourceName());
51
            }
52
            $contextualOperation = $contextualResource->getOperation($action->getOperationName());
53
            $options = [];
54
55
            if ($action->getUrl()) {
56
                $options['url'] = $action->getUrl();
57
            } elseif ($action->getRoute()) {
58
                $options['route'] = $action->getRoute();
59
            } else {
60
                $options['route'] = $this
61
                    ->routeNameGenerator
62
                    ->generateRouteName($resource, $contextualOperation)
63
                ;
64
            }
65
66
            if ($action->getIcon()) {
67
                $options['extras']['icon'] = $action->getIcon();
68
            }
69
            $menu->addChild($action->getLabel(), $options);
70
        }
71
    }
72
73
    private function getResource(Request $request): AdminResource
74
    {
75
        $resourceName = $this->parametersExtractor->getResourceName($request);
76
77
        return $this->registry->get($resourceName);
78
    }
79
80
    private function getOperation(AdminResource $resource, Request $request): OperationInterface
81
    {
82
        $operationName = $this->parametersExtractor->getOperationName($request);
83
84
        return $resource->getOperation($operationName);
85
    }
86
}
87