Passed
Pull Request — master (#203)
by Sergei
05:13
created

RouteResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getParameterValue() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router\HydratorAttribute;
6
7
use Yiisoft\Hydrator\Context;
8
use Yiisoft\Hydrator\NotResolvedException;
9
use Yiisoft\Hydrator\ParameterAttributeInterface;
10
use Yiisoft\Hydrator\ParameterAttributeResolverInterface;
11
use Yiisoft\Hydrator\UnexpectedAttributeException;
12
use Yiisoft\Router\CurrentRoute;
13
14
final class RouteResolver 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): mixed
22
    {
23 3
        if (!$attribute instanceof Route) {
24 1
            throw new UnexpectedAttributeException(Route::class, $attribute);
25
        }
26
27 2
        $arguments = $this->currentRoute->getArguments();
28
29 2
        $name = $attribute->getName();
30 2
        if ($name === null) {
31 2
            return $arguments;
32
        }
33
34 2
        return $arguments[$name] ?? throw new NotResolvedException();
35
    }
36
}
37