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

InputParam   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 10 1
A __construct() 0 10 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
        $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...
27
            new InputParamEnumHandler(),
28
            new InputParamObjectHandler(),
29
        );
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
36
    /** @param array<string, mixed> $query */
37
    public function __invoke(string $varName, array $query, InjectorInterface $injector): mixed
38
    {
39
        return $this->factory->create(
40
            $this->type,
41
            $varName,
42
            $query,
43
            $injector,
44
            $this->parameter,
45
            $this->isDefaultAvailable,
46
            $this->defaultValue,
47
        );
48
    }
49
}
50