Completed
Pull Request — master (#666)
by Guilh
04:08 queued 17s
created

resolveOperationPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 3
nop 3
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
namespace ApiPlatform\Core\Bridge\Symfony\Routing;
13
14
use ApiPlatform\Core\Exception\InvalidArgumentException;
15
use ApiPlatform\Core\PathResolver\OperationPathResolverInterface;
16
use Symfony\Component\Routing\RouterInterface;
17
18
/**
19
 * Resolves the operations path using a symfony route.
20
 *
21
 * @author Guilhem N. <[email protected]>
22
 */
23
final class RouterOperationPathResolver implements OperationPathResolverInterface
24
{
25
    private $router;
26
    private $deferred;
27
28
    public function __construct(RouterInterface $router, OperationPathResolverInterface $deferred)
29
    {
30
        $this->router = $router;
31
        $this->deferred = $deferred;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function resolveOperationPath(string $resourceShortName, array $operation, bool $collection) : string
38
    {
39
        if (!isset($operation['route_name'])) {
40
            return $this->deferred->resolveOperationPath($resourceShortName, $operation, $collection);
41
        }
42
43
        $route = $this->router->getRouteCollection()->get($operation['route_name']);
44
        if (null === $route) {
45
            throw new InvalidArgumentException(sprintf('The route "%s" of the ressource "%s" was not found.', $operation['route_name'], $ressourceShortName));
46
        }
47
48
        return $route->getPath();
49
    }
50
}
51