Completed
Push — master ( 5e7167...708efe )
by Nikola
03:44
created

DebugCommand::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

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