|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Xervice\Processor\Business\Model\Processor; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use DataProvider\ProcessRunDataProvider; |
|
8
|
|
|
use Xervice\Processor\Business\Dependency\ProcessConfigurationPluginInterface; |
|
9
|
|
|
use Xervice\Processor\Business\Exception\ProcessException; |
|
10
|
|
|
use Xervice\Processor\Business\Exception\ProcessNotFoundException; |
|
11
|
|
|
use Xervice\Processor\Business\Model\Hydrator\DataHydratorInterface; |
|
12
|
|
|
use Xervice\Processor\Business\Model\Translator\DataTranslatorInterface; |
|
13
|
|
|
use Xervice\Validator\Business\Exception\ValidationException; |
|
14
|
|
|
use Xervice\Validator\Business\ValidatorFacade; |
|
15
|
|
|
|
|
16
|
|
|
class Processor implements ProcessorInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var \Xervice\Processor\Business\Model\Processor\ProcessConfigurationPluginCollection |
|
20
|
|
|
*/ |
|
21
|
|
|
private $processorConfigurationPluginCollection; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Xervice\Validator\Business\ValidatorFacade |
|
25
|
|
|
*/ |
|
26
|
|
|
private $validatorFacade; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \Xervice\Processor\Business\Model\Hydrator\DataHydratorInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $dataHydrator; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \Xervice\Processor\Business\Model\Translator\DataTranslatorInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $dataTranslator; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Processor constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param \Xervice\Processor\Business\Model\Processor\ProcessConfigurationPluginCollection $processorConfigurationPluginCollection |
|
42
|
|
|
* @param \Xervice\Validator\Business\ValidatorFacade $validatorFacade |
|
43
|
|
|
* @param \Xervice\Processor\Business\Model\Hydrator\DataHydratorInterface $dataHydrator |
|
44
|
|
|
* @param \Xervice\Processor\Business\Model\Translator\DataTranslatorInterface $dataTranslator |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function __construct( |
|
47
|
|
|
ProcessConfigurationPluginCollection $processorConfigurationPluginCollection, |
|
48
|
|
|
ValidatorFacade $validatorFacade, |
|
49
|
|
|
DataHydratorInterface $dataHydrator, |
|
50
|
|
|
DataTranslatorInterface $dataTranslator |
|
51
|
|
|
) { |
|
52
|
3 |
|
$this->processorConfigurationPluginCollection = $processorConfigurationPluginCollection; |
|
53
|
3 |
|
$this->validatorFacade = $validatorFacade; |
|
54
|
3 |
|
$this->dataHydrator = $dataHydrator; |
|
55
|
3 |
|
$this->dataTranslator = $dataTranslator; |
|
56
|
3 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param \DataProvider\ProcessRunDataProvider $runDataProvider |
|
60
|
|
|
* |
|
61
|
|
|
* @throws \Xervice\Processor\Business\Exception\ProcessNotFoundException |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function process(ProcessRunDataProvider $runDataProvider): void |
|
64
|
|
|
{ |
|
65
|
3 |
|
$process = $this->getProcessByName($runDataProvider->getName()); |
|
66
|
|
|
|
|
67
|
2 |
|
$inputHandler = $process->getInputHandler(); |
|
68
|
2 |
|
$outputHandler = $process->getOutputHandler(); |
|
69
|
|
|
|
|
70
|
2 |
|
$inputHandler->init($runDataProvider); |
|
71
|
2 |
|
$outputHandler->init($runDataProvider); |
|
72
|
|
|
|
|
73
|
|
|
try { |
|
74
|
2 |
|
while (!$inputHandler->eof()) { |
|
75
|
2 |
|
$payload = $inputHandler->read(); |
|
76
|
|
|
|
|
77
|
2 |
|
$payload = $this->processData($payload, $process); |
|
78
|
|
|
|
|
79
|
1 |
|
$outputHandler->write($payload); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
1 |
|
catch (ValidationException $validationException) { |
|
83
|
1 |
|
throw new ProcessException("Data are invalid. Validation is failed", 0, $validationException); |
|
84
|
|
|
} |
|
85
|
1 |
|
finally { |
|
86
|
2 |
|
$inputHandler->close(); |
|
87
|
2 |
|
$outputHandler->close(); |
|
88
|
|
|
} |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $processName |
|
93
|
|
|
* |
|
94
|
|
|
* @return \Xervice\Processor\Business\Dependency\ProcessConfigurationPluginInterface |
|
95
|
|
|
* @throws \Xervice\Processor\Business\Exception\ProcessNotFoundException |
|
96
|
|
|
*/ |
|
97
|
3 |
|
protected function getProcessByName(string $processName): ProcessConfigurationPluginInterface |
|
98
|
|
|
{ |
|
99
|
3 |
|
foreach ($this->processorConfigurationPluginCollection as $configurationPlugin) { |
|
100
|
3 |
|
if ($configurationPlugin->getProcessName() === $processName) { |
|
101
|
3 |
|
return $configurationPlugin; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
$exceptionMessage = sprintf( |
|
106
|
1 |
|
'No process found with name %s', |
|
107
|
1 |
|
$processName |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
1 |
|
throw new ProcessNotFoundException($exceptionMessage); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param array $payload |
|
115
|
|
|
* @param \Xervice\Processor\Business\Dependency\ProcessConfigurationPluginInterface $process |
|
116
|
|
|
* |
|
117
|
|
|
* @return array |
|
118
|
|
|
* @throws \Xervice\Validator\Business\Exception\ValidationException |
|
119
|
|
|
*/ |
|
120
|
2 |
|
protected function processData(array $payload, ProcessConfigurationPluginInterface $process): array |
|
121
|
|
|
{ |
|
122
|
2 |
|
$this->validatorFacade->validate( |
|
123
|
2 |
|
$payload, |
|
124
|
2 |
|
$process->getValidatorConfigurationPlugins() |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
1 |
|
$payload = $this->dataHydrator->hydrate( |
|
128
|
1 |
|
$payload, |
|
129
|
1 |
|
$process->getHydratorPlugins() |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
1 |
|
$payload = $this->dataTranslator->translate( |
|
133
|
1 |
|
$payload, |
|
134
|
1 |
|
$process->getTranslatorPlugins() |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
1 |
|
return $process->process($payload); |
|
138
|
|
|
} |
|
139
|
|
|
} |