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\factory\Factory; |
9
|
|
|
use watoki\factory\Injector; |
10
|
|
|
use watoki\reflect\PropertyReader; |
11
|
|
|
use watoki\reflect\type\ClassType; |
12
|
|
|
use watoki\reflect\TypeFactory; |
13
|
|
|
|
14
|
|
|
class ObjectField implements CliField { |
15
|
|
|
|
16
|
|
|
/** @var TypeFactory */ |
17
|
|
|
private $types; |
18
|
|
|
|
19
|
|
|
/** @var FieldRegistry */ |
20
|
|
|
private $fields; |
21
|
|
|
|
22
|
|
|
/** @var ParameterReader */ |
23
|
|
|
private $reader; |
24
|
|
|
|
25
|
|
|
public function __construct(TypeFactory $types, FieldRegistry $fields, ParameterReader $reader) { |
26
|
|
|
$this->types = $types; |
27
|
|
|
$this->fields = $fields; |
28
|
|
|
$this->reader = $reader; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Parameter $parameter |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public function handles(Parameter $parameter) { |
36
|
|
|
return $parameter->getType() instanceof ClassType; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Parameter $parameter |
41
|
|
|
* @param null $serialized |
42
|
|
|
* @return object |
43
|
|
|
*/ |
44
|
|
|
public function inflate(Parameter $parameter, $serialized) { |
45
|
|
|
$properties = []; |
46
|
|
|
foreach ($this->getProperties($parameter) as $property) { |
47
|
|
|
$propertyParameter = new Parameter($parameter->getName() . '-' . $property->name(), $property->type()); |
48
|
|
|
|
49
|
|
|
$field = $this->getField($propertyParameter); |
50
|
|
|
$properties[$property->name()] = $field->inflate($propertyParameter, $this->reader->read($propertyParameter)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$injector = new Injector(new Factory()); |
54
|
|
|
$instance = $injector->injectConstructor($this->getClass($parameter), $properties, function () { |
55
|
|
|
return false; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
foreach ($this->getProperties($parameter) as $property) { |
59
|
|
|
$value = $properties[$property->name()]; |
60
|
|
|
if (!is_null($value) && $property->canSet()) { |
61
|
|
|
$property->set($instance, $value); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $instance; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
private function getClass(Parameter $parameter) { |
|
|
|
|
69
|
|
|
$type = $parameter->getType(); |
70
|
|
|
if (!($type instanceof ClassType)) { |
71
|
|
|
throw new \InvalidArgumentException("[$type] is not a ClassType"); |
72
|
|
|
} |
73
|
|
|
return $type->getClass(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Parameter $param |
78
|
|
|
* @return CliField |
79
|
|
|
*/ |
80
|
|
|
private function getField(Parameter $param) { |
81
|
|
|
return $this->fields->getField($param); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Parameter $parameter |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getDescription(Parameter $parameter) { |
89
|
|
|
return '(press enter)'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
private function getProperties(Parameter $parameter, $object = null) { |
|
|
|
|
93
|
|
|
$reader = new PropertyReader($this->types, $this->getClass($parameter)); |
94
|
|
|
|
95
|
|
|
foreach ($reader->readInterface($object) as $property) { |
96
|
|
|
if ($property->canSet()) { |
97
|
|
|
yield $property; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
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.