Passed
Pull Request — master (#203)
by Sergei
02:37
created

RouteArgumentResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameterValue() 0 17 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router\HydratorAttribute;
6
7
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface;
8
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeResolverInterface;
9
use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException;
10
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext;
11
use Yiisoft\Hydrator\Result;
12
use Yiisoft\Router\CurrentRoute;
13
14
use function array_key_exists;
15
16
final class RouteArgumentResolver implements ParameterAttributeResolverInterface
17
{
18 3
    public function __construct(
19
        private CurrentRoute $currentRoute,
20
    ) {
21 3
    }
22
23 3
    public function getParameterValue(
24
        ParameterAttributeInterface $attribute,
25
        ParameterAttributeResolveContext $context,
26
    ): Result {
27 3
        if (!$attribute instanceof RouteArgument) {
28 1
            throw new UnexpectedAttributeException(RouteArgument::class, $attribute);
29
        }
30
31 2
        $arguments = $this->currentRoute->getArguments();
32
33 2
        $name = $attribute->getName() ?? $context->getParameter()->getName();
34
35 2
        if (array_key_exists($name, $arguments)) {
36 1
            return Result::success($arguments[$name]);
37
        }
38
39 1
        return Result::fail();
40
    }
41
}
42