1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers; |
4
|
|
|
|
5
|
|
|
use Consolidation\AnnotatedCommand\AnnotationData; |
6
|
|
|
use Consolidation\AnnotatedCommand\CommandData; |
7
|
|
|
use Consolidation\AnnotatedCommand\Hooks\HookManager; |
8
|
|
|
use Consolidation\AnnotatedCommand\Hooks\ProcessResultInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Call hooks |
12
|
|
|
*/ |
13
|
|
|
class ProcessResultHookDispatcher extends HookDispatcher implements ProcessResultInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Process result and decide what to do with it. |
17
|
|
|
* Allow client to add transformation / interpretation |
18
|
|
|
* callbacks. |
19
|
|
|
*/ |
20
|
|
|
public function process($result, CommandData $commandData) |
21
|
|
|
{ |
22
|
|
|
$processors = $this->getProcessResultHooks($commandData->annotationData()); |
23
|
|
|
foreach ($processors as $processor) { |
24
|
|
|
$result = $this->callProcessor($processor, $result, $commandData); |
25
|
|
|
} |
26
|
|
|
$alterers = $this->getAlterResultHooks($commandData->annotationData()); |
27
|
|
|
foreach ($alterers as $alterer) { |
28
|
|
|
$result = $this->callProcessor($alterer, $result, $commandData); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $result; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function callProcessor($processor, $result, CommandData $commandData) |
35
|
|
|
{ |
36
|
|
|
$processed = null; |
37
|
|
|
if ($processor instanceof ProcessResultInterface) { |
38
|
|
|
$processed = $processor->process($result, $commandData); |
39
|
|
|
} |
40
|
|
|
if (is_callable($processor)) { |
41
|
|
|
$processed = $processor($result, $commandData); |
42
|
|
|
} |
43
|
|
|
if (isset($processed)) { |
44
|
|
|
return $processed; |
45
|
|
|
} |
46
|
|
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function getProcessResultHooks(AnnotationData $annotationData) |
50
|
|
|
{ |
51
|
|
|
return $this->getHooks( |
52
|
|
|
[ |
53
|
|
|
HookManager::PRE_PROCESS_RESULT, |
54
|
|
|
HookManager::PROCESS_RESULT, |
55
|
|
|
HookManager::POST_PROCESS_RESULT |
56
|
|
|
], |
57
|
|
|
$annotationData |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
protected function getAlterResultHooks(AnnotationData $annotationData) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return $this->getHooks( |
64
|
|
|
[ |
65
|
|
|
HookManager::PRE_ALTER_RESULT, |
66
|
|
|
HookManager::ALTER_RESULT, |
67
|
|
|
HookManager::POST_ALTER_RESULT, |
68
|
|
|
HookManager::POST_COMMAND_HOOK, |
69
|
|
|
], |
70
|
|
|
$annotationData |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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.