Passed
Pull Request — 2.4 (#2760)
by Vincent
05:50 queued 25s
created

RouteNameResolver::isSameSubresource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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 !== $operation && (empty($methods) || \in_array('GET', $methods, true)) && null !== $currentResourceClass && is_a($resourceClass, $currentResourceClass, 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