Passed
Pull Request — 2.4 (#2760)
by Vincent
03:53
created

RouteNameResolver   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 20

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isSameSubresource() 0 10 2
C getRouteName() 0 41 17
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Routing;
15
16
use ApiPlatform\Core\Api\OperationType;
17
use ApiPlatform\Core\Api\OperationTypeDeprecationHelper;
18
use ApiPlatform\Core\Exception\InvalidArgumentException;
19
use Symfony\Component\Routing\RouterInterface;
20
21
/**
22
 * {@inheritdoc}
23
 *
24
 * @author Kévin Dunglas <[email protected]>
25
 */
26
final class RouteNameResolver implements RouteNameResolverInterface
27
{
28
    private $router;
29
30
    public function __construct(RouterInterface $router)
31
    {
32
        $this->router = $router;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getRouteName(string $resourceClass, $operationType /*, array $context = [] */): string
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
    {
40
        if (\func_num_args() > 2) {
41
            $context = func_get_arg(2);
42
        } else {
43
            $context = [];
44
        }
45
46
        $operationType = OperationTypeDeprecationHelper::getOperationType($operationType);
47
48
        // Try with strict class name
49
        foreach ($this->router->getRouteCollection()->all() as $routeName => $route) {
50
            $currentResourceClass = $route->getDefault('_api_resource_class');
51
            $operation = $route->getDefault(sprintf('_api_%s_operation_name', $operationType));
52
            $methods = $route->getMethods();
53
54
            if ($resourceClass === $currentResourceClass && null !== $operation && (empty($methods) || \in_array('GET', $methods, true))) {
55
                if (OperationType::SUBRESOURCE === $operationType && false === $this->isSameSubresource($context, $route->getDefault('_api_subresource_context'))) {
56
                    continue;
57
                }
58
59
                return $routeName;
60
            }
61
        }
62
63
        // Maybe the parent class is a resource
64
        foreach ($this->router->getRouteCollection()->all() as $routeName => $route) {
65
            $currentResourceClass = $route->getDefault('_api_resource_class');
66
            $operation = $route->getDefault(sprintf('_api_%s_operation_name', $operationType));
67
            $methods = $route->getMethods();
68
69
            if (null !== $currentResourceClass && is_a($resourceClass, $currentResourceClass, true) && null !== $operation && (empty($methods) || \in_array('GET', $methods, true))) {
70
                if (OperationType::SUBRESOURCE === $operationType && false === $this->isSameSubresource($context, $route->getDefault('_api_subresource_context'))) {
71
                    continue;
72
                }
73
74
                return $routeName;
75
            }
76
        }
77
78
        throw new InvalidArgumentException(sprintf('No %s route associated with the type "%s".', $operationType, $resourceClass));
79
    }
80
81
    private function isSameSubresource(array $context, array $currentContext): bool
82
    {
83
        $subresources = array_keys($context['subresource_resources']);
84
        $currentSubresources = [];
85
86
        foreach ($currentContext['identifiers'] as $identiferContext) {
87
            $currentSubresources[] = $identiferContext[1];
88
        }
89
90
        return $currentSubresources === $subresources;
91
    }
92
}
93