1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\reflection; |
3
|
|
|
|
4
|
|
|
use rtens\domin\Parameter; |
5
|
|
|
use rtens\domin\reflection\types\TypeFactory; |
6
|
|
|
|
7
|
|
|
class GenericObjectAction extends ObjectAction { |
8
|
|
|
|
9
|
|
|
private $execute; |
10
|
|
|
private $fill; |
11
|
|
|
private $caption; |
12
|
|
|
private $description; |
13
|
|
|
private $paramMap = []; |
14
|
|
|
|
15
|
|
|
public function __construct($class, TypeFactory $types, CommentParser $parser, callable $execute) { |
16
|
|
|
parent::__construct($class, $types, $parser); |
17
|
|
|
$this->execute = $execute; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function setCaption($caption) { |
21
|
|
|
$this->caption = $caption; |
22
|
|
|
return $this; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function caption() { |
26
|
|
|
return $this->caption ?: parent::caption(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setDescription($description) { |
30
|
|
|
$this->description = $description; |
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function description() { |
35
|
|
|
return $this->description ?: parent::description(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setExecute(callable $execute) { |
39
|
|
|
$this->execute = $execute; |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setFill(callable $fill) { |
44
|
|
|
$this->fill = $fill; |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param callable $callback Filters the return value of execute |
50
|
|
|
*/ |
51
|
|
|
public function setAfterExecute(callable $callback) { |
52
|
|
|
$oldExecute = $this->execute; |
53
|
|
|
$this->execute = function ($object) use ($oldExecute, $callback) { |
54
|
|
|
return $callback(call_user_func($oldExecute, $object)); |
55
|
|
|
}; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* @param callable $map Receives Parameter and returns Parameter |
61
|
|
|
* @return static |
62
|
|
|
*/ |
63
|
|
|
public function mapParameter($name, callable $map) { |
64
|
|
|
$this->paramMap[$name] = $map; |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return \rtens\domin\Parameter[] |
70
|
|
|
* @throws \Exception |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function parameters() { |
|
|
|
|
73
|
|
|
return array_map(function (Parameter $parameter) { |
74
|
|
|
if (array_key_exists($parameter->getName(), $this->paramMap)) { |
75
|
|
|
return call_user_func($this->paramMap[$parameter->getName()], $parameter); |
76
|
|
|
} |
77
|
|
|
return $parameter; |
78
|
|
|
}, parent::parameters()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Called by execute() with the instantiated object |
83
|
|
|
* |
84
|
|
|
* @param object $object |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
protected function executeWith($object) { |
88
|
|
|
return call_user_func($this->execute, $object); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function fill(array $parameters) { |
|
|
|
|
92
|
|
|
$parameters = parent::fill($parameters); |
93
|
|
|
if ($this->fill) { |
94
|
|
|
return call_user_func($this->fill, $parameters); |
95
|
|
|
} |
96
|
|
|
return $parameters; |
97
|
|
|
} |
98
|
|
|
} |
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.