1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate Bundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2016 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\Helper\FormatterHelper; |
21
|
|
|
use Symfony\Component\Console\Helper\Table; |
22
|
|
|
use Symfony\Component\Console\Helper\TableCell; |
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ConfigurationDebugCommand |
28
|
|
|
* |
29
|
|
|
* Display current exchange rate configuration. |
30
|
|
|
* |
31
|
|
|
* @package RunOpenCode\Bundle\ExchangeRate\Command |
32
|
|
|
*/ |
33
|
|
|
class ConfigurationDebugCommand extends Command |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var SourcesRegistryInterface |
37
|
|
|
*/ |
38
|
|
|
protected $sourcesRegistry; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ProcessorsRegistryInterface |
42
|
|
|
*/ |
43
|
|
|
protected $processorsRegistry; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var RatesConfigurationRegistryInterface |
47
|
|
|
*/ |
48
|
|
|
protected $ratesConfigurationRegistry; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var RepositoryInterface |
52
|
|
|
*/ |
53
|
|
|
protected $repository; |
54
|
|
|
|
55
|
|
|
public function __construct( |
56
|
|
|
SourcesRegistryInterface $sourcesRegistry, |
57
|
|
|
ProcessorsRegistryInterface $processorsRegistry, |
58
|
|
|
RatesConfigurationRegistryInterface $ratesConfigurationRegistry, |
59
|
|
|
RepositoryInterface $repository |
60
|
|
|
) { |
61
|
|
|
parent::__construct(); |
62
|
|
|
$this->sourcesRegistry = $sourcesRegistry; |
63
|
|
|
$this->processorsRegistry = $processorsRegistry; |
64
|
|
|
$this->ratesConfigurationRegistry = $ratesConfigurationRegistry; |
65
|
|
|
$this->repository = $repository; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
protected function configure() |
72
|
|
|
{ |
73
|
|
|
$this |
74
|
|
|
->setName('roc:exchange-rate:configuration-debug') |
75
|
|
|
->setDescription('Dump current configuration.') |
76
|
|
|
; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
83
|
|
|
{ |
84
|
|
|
$this |
85
|
|
|
->displayHeader($output) |
86
|
|
|
->displayNewLine($output) |
87
|
|
|
->displaySources($output) |
88
|
|
|
->displayNewLine($output) |
89
|
|
|
->displayProcessors($output) |
90
|
|
|
->displayNewLine($output) |
91
|
|
|
->displayRepository($output) |
92
|
|
|
->displayNewLine($output) |
93
|
|
|
->displayRates($output) |
94
|
|
|
->displayNewLine($output) |
95
|
|
|
->displayFooter($output) |
96
|
|
|
; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Display header. |
101
|
|
|
* |
102
|
|
|
* @param OutputInterface $output |
103
|
|
|
* @return ConfigurationDebugCommand $this |
104
|
|
|
*/ |
105
|
|
|
protected function displayHeader(OutputInterface $output) |
106
|
|
|
{ |
107
|
|
|
$output->writeln($this->getFormatter()->formatBlock(array( |
108
|
|
|
'-------------------------------------------------------------', |
109
|
|
|
'Displaying current configuration for currency exchange bundle', |
110
|
|
|
'-------------------------------------------------------------' |
111
|
|
|
), 'info')); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Display footer. |
118
|
|
|
* |
119
|
|
|
* @param OutputInterface $output |
120
|
|
|
* @return ConfigurationDebugCommand $this |
121
|
|
|
*/ |
122
|
|
|
protected function displayFooter(OutputInterface $output) |
123
|
|
|
{ |
124
|
|
|
$output->writeln($this->getFormatter()->formatBlock('-------------------------------------------------------------', 'info')); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Display sources. |
131
|
|
|
* |
132
|
|
|
* @param OutputInterface $output |
133
|
|
|
* @return ConfigurationDebugCommand $this |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
protected function displaySources(OutputInterface $output) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
/** |
138
|
|
|
* @var SourceInterface $source |
139
|
|
|
*/ |
140
|
|
|
foreach ($this->sourcesRegistry as $source) { |
141
|
|
|
$output->writeln($this->getFormatter()->formatSection('Source', sprintf('%s as %s', get_class($source), $source->getName()))); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Display processors. |
149
|
|
|
* |
150
|
|
|
* @param OutputInterface $output |
151
|
|
|
* @return ConfigurationDebugCommand $this |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
protected function displayProcessors(OutputInterface $output) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
/** |
156
|
|
|
* @var ProcessorInterface $processor |
157
|
|
|
*/ |
158
|
|
|
foreach ($this->processorsRegistry as $processor) { |
159
|
|
|
$output->writeln($this->getFormatter()->formatSection('Processor', get_class($processor))); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Display repository. |
167
|
|
|
* |
168
|
|
|
* @param OutputInterface $output |
169
|
|
|
* @return ConfigurationDebugCommand $this |
170
|
|
|
*/ |
171
|
|
|
protected function displayRepository(OutputInterface $output) |
172
|
|
|
{ |
173
|
|
|
$output->writeln($this->getFormatter()->formatSection('Repository', sprintf('%s with %s records.', get_class($this->repository), $this->repository->count()))); |
|
|
|
|
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Display rates. |
180
|
|
|
* |
181
|
|
|
* @param OutputInterface $output |
182
|
|
|
* @return ConfigurationDebugCommand $this |
183
|
|
|
*/ |
184
|
|
|
protected function displayRates(OutputInterface $output) |
185
|
|
|
{ |
186
|
|
|
$table = new Table($output); |
187
|
|
|
|
188
|
|
|
$table->setHeaders(array( |
189
|
|
|
array(new TableCell('Registered currency rates', array('colspan' => 3))), |
190
|
|
|
array('Currency code', 'Rate type', 'Source', 'Alias') |
191
|
|
|
)); |
192
|
|
|
/** |
193
|
|
|
* @var Configuration $rateConfiguration |
194
|
|
|
*/ |
195
|
|
|
foreach ($this->ratesConfigurationRegistry as $rateConfiguration) { |
196
|
|
|
$table->addRow(array( |
197
|
|
|
$rateConfiguration->getCurrencyCode(), |
198
|
|
|
$rateConfiguration->getRateType(), |
199
|
|
|
$rateConfiguration->getSource(), |
200
|
|
|
$rateConfiguration->getAlias() ?: 'N/A' |
201
|
|
|
)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$table->render(); |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Display new line. |
211
|
|
|
* |
212
|
|
|
* @param OutputInterface $output |
213
|
|
|
* @return ConfigurationDebugCommand $this |
214
|
|
|
*/ |
215
|
|
|
protected function displayNewLine(OutputInterface $output) |
216
|
|
|
{ |
217
|
|
|
$output->writeln(''); |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get formatter. |
224
|
|
|
* |
225
|
|
|
* @return FormatterHelper |
226
|
|
|
*/ |
227
|
|
|
protected function getFormatter() |
228
|
|
|
{ |
229
|
|
|
return $this->getHelper('formatter'); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
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.