1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate Bundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\Bundle\ExchangeRate\Command; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\ExchangeRate\Configuration; |
13
|
|
|
use RunOpenCode\ExchangeRate\Contract\ProcessorInterface; |
14
|
|
|
use RunOpenCode\ExchangeRate\Contract\ProcessorsRegistryInterface; |
15
|
|
|
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface; |
16
|
|
|
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface; |
17
|
|
|
use RunOpenCode\ExchangeRate\Contract\SourceInterface; |
18
|
|
|
use RunOpenCode\ExchangeRate\Contract\SourcesRegistryInterface; |
19
|
|
|
use Symfony\Component\Console\Command\Command; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class DebugCommand |
26
|
|
|
* |
27
|
|
|
* Display current exchange rate configuration. |
28
|
|
|
* |
29
|
|
|
* @package RunOpenCode\Bundle\ExchangeRate\Command |
30
|
|
|
*/ |
31
|
|
|
class DebugCommand extends Command |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $baseCurrency; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var SourcesRegistryInterface |
40
|
|
|
*/ |
41
|
|
|
protected $sourcesRegistry; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ProcessorsRegistryInterface |
45
|
|
|
*/ |
46
|
|
|
protected $processorsRegistry; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var RatesConfigurationRegistryInterface |
50
|
|
|
*/ |
51
|
|
|
protected $ratesConfigurationRegistry; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var RepositoryInterface |
55
|
|
|
*/ |
56
|
|
|
protected $repository; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var SymfonyStyle |
60
|
|
|
*/ |
61
|
|
|
protected $output; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
protected $valid; |
67
|
|
|
|
68
|
5 |
|
public function __construct( |
69
|
|
|
$baseCurrency, |
70
|
|
|
SourcesRegistryInterface $sourcesRegistry, |
71
|
|
|
ProcessorsRegistryInterface $processorsRegistry, |
72
|
|
|
RatesConfigurationRegistryInterface $ratesConfigurationRegistry, |
73
|
|
|
RepositoryInterface $repository |
74
|
|
|
) { |
75
|
5 |
|
parent::__construct(); |
76
|
|
|
|
77
|
5 |
|
$this->baseCurrency = $baseCurrency; |
78
|
5 |
|
$this->sourcesRegistry = $sourcesRegistry; |
79
|
5 |
|
$this->processorsRegistry = $processorsRegistry; |
80
|
5 |
|
$this->ratesConfigurationRegistry = $ratesConfigurationRegistry; |
81
|
5 |
|
$this->repository = $repository; |
82
|
5 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
5 |
|
protected function configure() |
88
|
|
|
{ |
89
|
|
|
$this |
90
|
5 |
|
->setName('debug:runopencode:exchange-rate') |
91
|
5 |
|
->setAliases([ |
92
|
5 |
|
'debug:exchange-rate', |
93
|
|
|
'debug:roc:exchange-rate' |
94
|
|
|
]) |
95
|
5 |
|
->setDescription('Debug exchange rate bundle configuration.') |
96
|
|
|
; |
97
|
5 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
5 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
103
|
|
|
{ |
104
|
5 |
|
$this->output = new SymfonyStyle($input, $output); |
105
|
5 |
|
$this->valid = true; |
106
|
|
|
|
107
|
5 |
|
$this->output->title(sprintf('Debugging current configuration for currency exchange bundle with base currency "%s"', $this->baseCurrency)); |
108
|
|
|
|
109
|
5 |
|
$this->displaySources(); |
110
|
5 |
|
$this->output->newLine(); |
111
|
5 |
|
$this->displayProcessors(); |
112
|
5 |
|
$this->output->newLine(); |
113
|
5 |
|
$this->displayProcessors(); |
114
|
5 |
|
$this->output->newLine(); |
115
|
5 |
|
$this->displayRepository(); |
116
|
5 |
|
$this->output->newLine(); |
117
|
5 |
|
$this->displayRates(); |
118
|
|
|
|
119
|
5 |
|
$this->output->section('Summary:'); |
120
|
|
|
|
121
|
5 |
|
if ($this->valid) { |
122
|
2 |
|
$this->output->success('Configuration is valid.'); |
123
|
2 |
|
return 0; |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
$this->output->error('Configuration is not valid!'); |
127
|
3 |
|
return -1; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Display sources. |
132
|
|
|
* |
133
|
|
|
* @return DebugCommand $this |
134
|
|
|
*/ |
135
|
5 |
|
protected function displaySources() |
136
|
|
|
{ |
137
|
5 |
|
$this->output->section('Sources:'); |
138
|
|
|
|
139
|
5 |
|
$headers = ['Name', 'Class']; |
140
|
|
|
|
141
|
5 |
|
$rows = []; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @var SourceInterface $source |
145
|
|
|
*/ |
146
|
5 |
|
foreach ($this->sourcesRegistry as $source) { |
147
|
|
|
|
148
|
4 |
|
$rows[] = [ |
149
|
4 |
|
$source->getName(), |
150
|
4 |
|
get_class($source) |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
5 |
|
if (count($rows) > 0) { |
155
|
4 |
|
$this->output->table($headers, $rows); |
156
|
4 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
$this->valid = false; |
160
|
1 |
|
$this->output->error('There are no registered sources.'); |
161
|
1 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Display processors. |
166
|
|
|
* |
167
|
|
|
* @return DebugCommand $this |
168
|
|
|
*/ |
169
|
5 |
|
protected function displayProcessors() |
170
|
|
|
{ |
171
|
5 |
|
$this->output->section('Processors:'); |
172
|
|
|
|
173
|
5 |
|
$processors = []; |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @var ProcessorInterface $processor |
177
|
|
|
*/ |
178
|
5 |
|
foreach ($this->processorsRegistry as $processor) { |
179
|
4 |
|
$processors[] = get_class($processor); |
180
|
|
|
} |
181
|
|
|
|
182
|
5 |
|
if (count($processors) > 0) { |
183
|
4 |
|
$this->output->writeln(sprintf('Registered processors implementations: "%s"', implode('", "', $processors))); |
184
|
4 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
1 |
|
$this->output->warning('There are no registered processors.'); |
188
|
1 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Display repository. |
193
|
|
|
* |
194
|
|
|
* @return DebugCommand $this |
195
|
|
|
*/ |
196
|
5 |
|
protected function displayRepository() |
197
|
|
|
{ |
198
|
5 |
|
$this->output->section('Repository:'); |
199
|
|
|
|
200
|
5 |
|
$this->output->writeln(sprintf('Registered "%s" repository implementation with "%s" records so far.', get_class($this->repository), $this->repository->count())); |
201
|
|
|
|
202
|
5 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Display rates. |
207
|
|
|
* |
208
|
|
|
* @return DebugCommand $this |
209
|
|
|
*/ |
210
|
5 |
|
protected function displayRates() |
211
|
|
|
{ |
212
|
5 |
|
$this->output->section('Registered exchange rates:'); |
213
|
|
|
|
214
|
5 |
|
$headers = ['Currency code', 'Rate type', 'Source', 'Source exists']; |
215
|
|
|
|
216
|
5 |
|
$rows = []; |
217
|
5 |
|
$missingSources = []; |
218
|
|
|
/** |
219
|
|
|
* @var Configuration $rateConfiguration |
220
|
|
|
*/ |
221
|
5 |
|
foreach ($this->ratesConfigurationRegistry as $rateConfiguration) { |
222
|
|
|
|
223
|
4 |
|
$sourceExists = $this->sourcesRegistry->has($rateConfiguration->getSourceName()); |
224
|
|
|
|
225
|
4 |
|
$rows[] = [ |
226
|
4 |
|
$rateConfiguration->getCurrencyCode(), |
227
|
4 |
|
$rateConfiguration->getRateType(), |
228
|
4 |
|
$rateConfiguration->getSourceName(), |
229
|
4 |
|
$sourceExists ? 'Yes' : 'No', |
230
|
|
|
]; |
231
|
|
|
|
232
|
4 |
|
if (!$sourceExists) { |
233
|
4 |
|
$missingSources[] = $rateConfiguration->getSourceName(); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
5 |
|
if (count($rows) > 0) { |
238
|
4 |
|
$this->output->table($headers, $rows); |
239
|
|
|
|
240
|
4 |
|
if (count($missingSources) > 0) { |
241
|
2 |
|
$this->valid = false; |
242
|
2 |
|
$this->output->error(sprintf('Missing sources detected: "%s".', implode('", "', $missingSources))); |
243
|
|
|
} |
244
|
|
|
|
245
|
4 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
1 |
|
$this->valid = false; |
249
|
1 |
|
$this->output->error('There are no registered currency exchange rates.'); |
250
|
1 |
|
return $this; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|