MultiField::inflate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
namespace rtens\domin\delivery\cli\fields;
3
4
use rtens\domin\delivery\cli\CliField;
5
use rtens\domin\delivery\FieldRegistry;
6
use rtens\domin\delivery\ParameterReader;
7
use rtens\domin\Parameter;
8
use watoki\reflect\type\MultiType;
9
10
class MultiField implements CliField {
11
12
    /** @var FieldRegistry */
13
    private $fields;
14
15
    /** @var ParameterReader */
16
    private $reader;
17
18
    public function __construct(FieldRegistry $fields, ParameterReader $reader) {
19
        $this->fields = $fields;
20
        $this->reader = $reader;
21
    }
22
23
    /**
24
     * @param Parameter $parameter
25
     * @return bool
26
     */
27
    public function handles(Parameter $parameter) {
28
        return $parameter->getType() instanceof MultiType;
29
    }
30
31
    /**
32
     * @param Parameter $parameter
33
     * @param string $serialized
34
     * @return mixed
35
     */
36
    public function inflate(Parameter $parameter, $serialized) {
37
        $type = $this->getTypes($parameter)[(int)$serialized];
38
39
        $optionParameter = new Parameter($parameter->getName() . '-value', $type);
40
        return $this->getField($optionParameter)->inflate($optionParameter, $this->reader->read($optionParameter));
41
    }
42
43
    /**
44
     * @param Parameter $parameter
45
     * @return string
46
     */
47 View Code Duplication
    public function getDescription(Parameter $parameter) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
        $out = [''];
49
        foreach ($this->getTypes($parameter) as $i => $option) {
50
            $out[] = '  ' . $i . ' - ' . $option;
51
        }
52
        $out[] = 'Type';
53
        return implode(PHP_EOL, $out);
54
    }
55
56
    /**
57
     * @param Parameter $optionParameter
58
     * @return CliField
59
     */
60
    private function getField($optionParameter) {
61
        return $this->fields->getField($optionParameter);
62
    }
63
64 View Code Duplication
    private function getTypes(Parameter $parameter) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
        $type = $parameter->getType();
66
        if (!($type instanceof MultiType)) {
67
            throw new \InvalidArgumentException("[$type] must be a MultiType");
68
        }
69
70
        return $type->getTypes();
71
    }
72
}