Passed
Push — input-query ( 722d21 )
by Akihito
06:11 queued 02:47
created

InputParam::__invoke()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use Override;
8
use Ray\Di\InjectorInterface;
9
use Ray\InputQuery\InputQueryInterface;
10
use ReflectionNamedType;
11
use ReflectionParameter;
12
13
use function assert;
14
use function class_exists;
15
16
/** @psalm-import-type Query from Types */
17
final class InputParam implements ParamInterface
18
{
19
    /** @param InputQueryInterface<object> $inputQuery */
20
    public function __construct(
21
        private readonly InputQueryInterface $inputQuery,
22
        private readonly ReflectionParameter $parameter,
23
    ) {
24
    }
25
26
    /**
27
     * {@inheritDoc}
28
     */
29
    #[Override]
30
    public function __invoke(string $varName, array $query, InjectorInterface $injector)
31
    {
32
        $type = $this->parameter->getType();
33
        if ($type instanceof ReflectionNamedType && ! $type->isBuiltin()) {
34
            $inputClass = $type->getName();
35
            assert(class_exists($inputClass));
36
37
            return $this->inputQuery->create($inputClass, $query);
38
        }
39
40
        // For built-in types, return from query directly
41
        return $query[$varName] ?? null;
42
    }
43
}
44