Passed
Pull Request — 1.x (#321)
by Akihito
02:42
created

InputParam   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 21
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 10 1
A __construct() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\Input;
6
7
use BEAR\Resource\ParamInterface;
8
use Ray\Di\InjectorInterface;
9
use ReflectionNamedType;
10
use ReflectionParameter;
11
12
final class InputParam implements ParamInterface
13
{
14
    private readonly string $type;
15
    private readonly bool $isDefaultAvailable;
16
    private readonly mixed $defaultValue;
17
    private readonly InputParamFactory $factory;
18
19
    public function __construct(
20
        ReflectionNamedType $type,
21
        private readonly ReflectionParameter $parameter,
22
    ) {
23
        $this->type = $type->getName();
0 ignored issues
show
Bug introduced by
The property type is declared read-only in BEAR\Resource\Input\InputParam.
Loading history...
24
        $this->isDefaultAvailable = $parameter->isDefaultValueAvailable();
0 ignored issues
show
Bug introduced by
The property isDefaultAvailable is declared read-only in BEAR\Resource\Input\InputParam.
Loading history...
25
        $this->defaultValue = $this->isDefaultAvailable ? $parameter->getDefaultValue() : null;
0 ignored issues
show
Bug introduced by
The property defaultValue is declared read-only in BEAR\Resource\Input\InputParam.
Loading history...
26
        $inputIterator = new InputAttributeIterator();
27
        $this->factory = new InputParamFactory(
0 ignored issues
show
Bug introduced by
The property factory is declared read-only in BEAR\Resource\Input\InputParam.
Loading history...
28
            new InputParamEnumHandler(),
29
            new InputParamObjectHandler($inputIterator),
30
            $inputIterator,
31
        );
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
38
    /** @param array<string, mixed> $query */
39
    public function __invoke(string $varName, array $query, InjectorInterface $injector): mixed
40
    {
41
        return $this->factory->create(
42
            $this->type,
43
            $varName,
44
            $query,
45
            $injector,
46
            $this->parameter,
47
            $this->isDefaultAvailable,
48
            $this->defaultValue,
49
        );
50
    }
51
}
52