Passed
Pull Request — master (#203)
by Sergei
13:18 queued 10:44
created

RouteArgumentResolver::getParameterValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router\HydratorAttribute;
6
7
use Yiisoft\Hydrator\Context;
8
use Yiisoft\Hydrator\ParameterAttributeInterface;
9
use Yiisoft\Hydrator\ParameterAttributeResolverInterface;
10
use Yiisoft\Hydrator\Result;
11
use Yiisoft\Hydrator\UnexpectedAttributeException;
12
use Yiisoft\Router\CurrentRoute;
13
14
final class RouteArgumentResolver implements ParameterAttributeResolverInterface
15
{
16 3
    public function __construct(
17
        private CurrentRoute $currentRoute,
18
    ) {
19 3
    }
20
21 3
    public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): Result
22
    {
23 3
        if (!$attribute instanceof RouteArgument) {
24 1
            throw new UnexpectedAttributeException(RouteArgument::class, $attribute);
25
        }
26
27 2
        $arguments = $this->currentRoute->getArguments();
28
29 2
        $name = $attribute->getName() ?? $context->getParameter()->getName();
30
31 2
        if (array_key_exists($name, $arguments)) {
32 1
            return Result::success($arguments[$name]);
33
        }
34
35 1
        return Result::fail();
36
    }
37
}
38