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

InputParam   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 3
A __construct() 0 4 1
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