UrlGenerator::generateFromOperationName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 9
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Routing\UrlGenerator;
6
7
use LAG\AdminBundle\Metadata\AdminResource;
8
use LAG\AdminBundle\Metadata\OperationInterface;
9
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface;
10
use LAG\AdminBundle\Routing\Parameter\ParametersMapper;
11
use Symfony\Component\Routing\RouterInterface;
12
use Symfony\Component\String\Inflector\EnglishInflector;
13
14
use function Symfony\Component\String\u;
15
16
class UrlGenerator implements UrlGeneratorInterface
17
{
18
    public function __construct(
19
        private RouterInterface $router,
20
        private ResourceRegistryInterface $resourceRegistry,
21
    ) {
22
    }
23
24
    public function generatePath(
25
        AdminResource $resource,
26
        OperationInterface $operation,
27
    ): string {
28
        $resource = $operation->getResource();
29
        $resourceName = (new EnglishInflector())->pluralize($resource->getName())[0];
0 ignored issues
show
Bug introduced by
It seems like $resource->getName() can also be of type null; however, parameter $singular of Symfony\Component\String...hInflector::pluralize() 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

29
        $resourceName = (new EnglishInflector())->pluralize(/** @scrutinizer ignore-type */ $resource->getName())[0];
Loading history...
30
31
        $path = u($resource->getPrefix())
0 ignored issues
show
Bug introduced by
The method getPrefix() does not exist on LAG\AdminBundle\Metadata\AdminResource. ( Ignorable by Annotation )

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

31
        $path = u($resource->/** @scrutinizer ignore-call */ getPrefix())

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...
32
            ->replace('{resourceName}', $resourceName)
33
        ;
34
35
        foreach ($operation->getRouteParameters() as $parameter => $requirement) {
36
            $path = $path
37
                ->append('/')
38
                ->append('{'.$parameter.'}')
39
            ;
40
        }
41
        $operationPath = u($operation->getPath());
42
43
        if ($operationPath->length() > 0) {
44
            $operationPath = $operationPath->ensureStart('/');
45
        }
46
47
        return $path
48
            ->append($operationPath->toString())
49
            ->toString()
50
        ;
51
    }
52
53
    public function generateFromRouteName(string $routeName, array $routeParameters = [], mixed $data = null): string
54
    {
55
        $mappedRouteParameters = $routeParameters;
56
57
        if ($data !== null) {
58
            $mapper = new ParametersMapper();
59
            $mappedRouteParameters = $mapper->map($data, $routeParameters);
60
        }
61
62
        return $this->router->generate($routeName, $mappedRouteParameters);
63
    }
64
65
    public function generateFromOperationName(string $resourceName, string $operationName, mixed $data = null): string
66
    {
67
        $resource = $this->resourceRegistry->get($resourceName);
68
        $operation = $resource->getOperation($operationName);
69
70
        return $this->generateFromRouteName(
71
            $operation->getRoute(),
0 ignored issues
show
Bug introduced by
It seems like $operation->getRoute() can also be of type null; however, parameter $routeName of LAG\AdminBundle\Routing\...generateFromRouteName() 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

71
            /** @scrutinizer ignore-type */ $operation->getRoute(),
Loading history...
72
            array_keys($operation->getRouteParameters()),
0 ignored issues
show
Bug introduced by
It seems like $operation->getRouteParameters() can also be of type null; however, parameter $array of array_keys() does only seem to accept array, 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

72
            array_keys(/** @scrutinizer ignore-type */ $operation->getRouteParameters()),
Loading history...
73
            $data,
74
        );
75
    }
76
}
77