Completed
Pull Request — master (#725)
by Guilh
59:22 queued 54:31
created

RouterOperationPathResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 35
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A resolveOperationPath() 0 13 3
A getContext() 0 4 1
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