Passed
Push — 1.x ( 90de9d...e2e7a5 )
by Akihito
05:28 queued 03:04
created

NamedParameter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 30
ccs 10
cts 10
cp 1
rs 10
wmc 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BEAR\Resource\Exception\ParameterException;
8
use InvalidArgumentException;
9
use Override;
10
use Ray\Di\InjectorInterface;
11
12
final readonly class NamedParameter implements NamedParameterInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 12 at column 6
Loading history...
13
{
14
    public function __construct(
15
        private NamedParamMetasInterface $paramMetas,
16
        private InjectorInterface $injector,
17
    ) {
18
    }
19
20
    /**
21 107
     * {@inheritDoc}
22
     *
23 107
     * @throws ParameterException Thrown when the query parameter is invalid.
24 107
     */
25 107
    #[Override]
26
    public function getParameters(callable $callable, array $query): array
27
    {
28
        $metas = ($this->paramMetas)($callable);
29
        $parameters = [];
30 75
        foreach ($metas as $varName => $param) {
31
32 75
            /** @psalm-suppress MixedAssignment */
33 75
            try {
34 75
                $parameters[$varName] = $param($varName, $query, $this->injector);
35
            } catch (InvalidArgumentException $e) {
36 68
                // handle missing query parameter in Ray.InputQuery
37
                throw new ParameterException($varName, $e->getCode(), $e);
38
            }
39 68
        }
40
41
        return $parameters;
42
    }
43
}
44