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

RouteNameResolver::getRouteName()   C

Complexity

Conditions 17
Paths 26

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 22
nc 26
nop 2
dl 0
loc 41
rs 5.2166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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