Passed
Push — master ( 673f30...a0fef5 )
by Arnaud
25:58 queued 19:36
created

AdminExtension::getOperationUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Twig\Extension;
6
7
use LAG\AdminBundle\Application\Configuration\ApplicationConfiguration;
8
use LAG\AdminBundle\Metadata\Link;
9
use LAG\AdminBundle\Metadata\OperationInterface;
10
use LAG\AdminBundle\Routing\UrlGenerator\UrlGeneratorInterface;
11
use LAG\AdminBundle\Security\Helper\SecurityHelper;
12
use LAG\AdminBundle\Twig\Render\ActionRendererInterface;
13
use Twig\Extension\AbstractExtension;
14
use Twig\TwigFunction;
15
16
class AdminExtension extends AbstractExtension
17
{
18 5
    public function __construct(
19
        private bool $mediaBundleEnabled,
20
        private ApplicationConfiguration $applicationConfiguration,
21
        private SecurityHelper $security,
22
        private ActionRendererInterface $actionRenderer,
23 5
        private UrlGeneratorInterface $urlGenerator,
24 5
    ) {
25 5
    }
26 5
27
    public function getFunctions(): array
28 1
    {
29
        return [
30
            new TwigFunction('lag_admin_config', [$this, 'getConfigurationValue']),
31 1
            new TwigFunction('lag_admin_operation_allowed', [$this, 'isOperationAllowed']),
32 1
            new TwigFunction('lag_admin_action', [$this, 'renderAction'], ['is_safe' => ['html']]),
33 1
            new TwigFunction('lag_admin_operation_url', [$this, 'getOperationUrl'], ['is_safe' => ['html']]),
34 1
            new TwigFunction('lag_admin_media_bundle_enabled', [$this, 'isMediaBundleEnabled']),
35
        ];
36
    }
37
38 1
    public function getConfigurationValue(string $name): mixed
39
    {
40
        return $this->applicationConfiguration->get($name);
41 1
    }
42 1
43
    public function isOperationAllowed(string $resourceName, string $operationName): bool
44
    {
45
        return $this->security->isOperationAllowed($resourceName, $operationName);
46
    }
47
48
    /**
49 1
     * @param array<string, mixed> $options
50
     */
51 1
    public function renderAction(Link $action, mixed $data = null, array $options = []): string
52
    {
53
        return $this->actionRenderer->render($action, $data, $options);
54
    }
55
56
    public function getOperationUrl(OperationInterface $operation, mixed $data = null): string
57 1
    {
58
        return $this->urlGenerator->generateFromOperationName(
59 1
            $operation->getResource()->getName(),
0 ignored issues
show
Bug introduced by
It seems like $operation->getResource()->getName() 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

59
            /** @scrutinizer ignore-type */ $operation->getResource()->getName(),
Loading history...
60
            $operation->getName(),
0 ignored issues
show
Bug introduced by
It seems like $operation->getName() 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

60
            /** @scrutinizer ignore-type */ $operation->getName(),
Loading history...
61
            $data,
62
        );
63
    }
64
65
    public function isMediaBundleEnabled(): bool
66
    {
67
        return $this->mediaBundleEnabled;
68
    }
69
}
70