Completed
Push — master ( 99f595...183b0a )
by Kévin
14s
created

RouteNameResolver::isSameSubresource()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
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
42% 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 View Code Duplication
        if (func_num_args() > 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
            $context = func_get_arg(2);
42
        } else {
43
            $context = [];
44
            @trigger_error(sprintf('Method %s() will have a third `$context = []` argument in version 3.0. Not defining it is deprecated since 2.1.', __METHOD__), E_USER_DEPRECATED);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
        }
46
47
        $operationType = OperationTypeDeprecationHelper::getOperationType($operationType);
48
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 === OperationType::SUBRESOURCE && false === $this->isSameSubresource($context, $route->getDefault('_api_subresource_context'))) {
56
                    continue;
57
                }
58
59
                return $routeName;
60
            }
61
        }
62
63
        throw new InvalidArgumentException(sprintf('No %s route associated with the type "%s".', $operationType, $resourceClass));
64
    }
65
66
    private function isSameSubresource(array $context, array $currentContext): bool
67
    {
68
        $subresources = array_keys($context['subresource_resources']);
69
        $currentSubresources = [];
70
71
        foreach ($currentContext['identifiers'] as $identiferContext) {
72
            $currentSubresources[] = $identiferContext[1];
73
        }
74
75
        if ($currentSubresources === $subresources) {
76
            return true;
77
        }
78
79
        return false;
80
    }
81
}
82