ActionRenderer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 38
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A render() 0 29 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Twig\Render;
6
7
use LAG\AdminBundle\Exception\Validation\InvalidActionException;
8
use LAG\AdminBundle\Metadata\Link;
9
use LAG\AdminBundle\Routing\UrlGenerator\UrlGeneratorInterface;
10
use Symfony\Component\Validator\Constraints\Valid;
11
use Symfony\Component\Validator\Validator\ValidatorInterface;
12
use Twig\Environment;
13
14
class ActionRenderer implements ActionRendererInterface
15
{
16
    public function __construct(
17
        private UrlGeneratorInterface $urlGenerator,
18
        private ValidatorInterface $validator,
19
        private Environment $environment,
20
    ) {
21
    }
22
23
    public function render(
24
        Link $action,
25
        mixed $data = null,
26
        array $options = []
27
    ): string {
28
        $errors = $this->validator->validate($action, [new Valid()]);
29
30
        if ($errors->count() > 0) {
31
            throw new InvalidActionException($errors);
32
        }
33
34
        if ($action->getRoute()) {
35
            $url = $this->urlGenerator->generateFromRouteName(
36
                $action->getRoute(),
37
                $action->getRouteParameters(),
38
                $data,
39
            );
40
        } else {
41
            $url = $this->urlGenerator->generateFromOperationName(
42
                $action->getResourceName(),
0 ignored issues
show
Bug introduced by
It seems like $action->getResourceName() can also be of type null; however, parameter $resourceName of LAG\AdminBundle\Routing\...rateFromOperationName() does only seem to accept string, 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

42
                /** @scrutinizer ignore-type */ $action->getResourceName(),
Loading history...
43
                $action->getOperationName(),
0 ignored issues
show
Bug introduced by
It seems like $action->getOperationName() can also be of type null; however, parameter $operationName of LAG\AdminBundle\Routing\...rateFromOperationName() does only seem to accept string, 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
                /** @scrutinizer ignore-type */ $action->getOperationName(),
Loading history...
44
                $data,
45
            );
46
        }
47
48
        return $this->environment->render($action->getTemplate(), [
49
            'action' => $action,
50
            'url' => $url,
51
            'options' => $options,
52
        ]);
53
    }
54
}
55