Completed
Pull Request — master (#725)
by Guilh
55:49 queued 51:04
created

RouterOperationPathResolver::getContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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