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

RouterOperationPathResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

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