Completed
Push — master ( 42f99d...2335a8 )
by Nikola
18:20 queued 03:48
created

ConfigurationDebugCommand::displayHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4286
cc 1
eloc 7
nc 1
nop 1
crap 2
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
use Symfony\Component\Console\Style\SymfonyStyle;
26
27
/**
28
 * Class ConfigurationDebugCommand
29
 *
30
 * Display current exchange rate configuration.
31
 *
32
 * @package RunOpenCode\Bundle\ExchangeRate\Command
33
 */
34
class ConfigurationDebugCommand extends Command
35
{
36
    /**
37
     * @var SourcesRegistryInterface
38
     */
39
    protected $sourcesRegistry;
40
41
    /**
42
     * @var ProcessorsRegistryInterface
43
     */
44
    protected $processorsRegistry;
45
46
    /**
47
     * @var RatesConfigurationRegistryInterface
48
     */
49
    protected $ratesConfigurationRegistry;
50
51
    /**
52
     * @var RepositoryInterface
53
     */
54
    protected $repository;
55
56
    /**
57
     * @var SymfonyStyle
58
     */
59
    protected $sfStyle;
60
61
    public function __construct(
62
        SourcesRegistryInterface $sourcesRegistry,
63
        ProcessorsRegistryInterface $processorsRegistry,
64
        RatesConfigurationRegistryInterface $ratesConfigurationRegistry,
65
        RepositoryInterface $repository
66
    ) {
67
        parent::__construct();
68
        $this->sourcesRegistry = $sourcesRegistry;
69
        $this->processorsRegistry = $processorsRegistry;
70
        $this->ratesConfigurationRegistry = $ratesConfigurationRegistry;
71
        $this->repository = $repository;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function configure()
78
    {
79
        $this
80
            ->setName('roc:exchange-rate:configuration-debug')
81
            ->setDescription('Dump current configuration.')
82
        ;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    protected function execute(InputInterface $input, OutputInterface $output)
89
    {
90
        $this->sfStyle = new SymfonyStyle($input, $output);
91
92
        $this
93
            ->sfStyle
94
            ->title('Displaying current configuration for currency exchange bundle');
95
96
        $this
97
            ->displaySources($output)
98
            ->displayNewLine($output)
99
            ->displayProcessors($output)
100
            ->displayNewLine($output)
101
            ->displayRepository($output)
102
            ->displayNewLine($output)
103
            ->displayRates($output)
104
            ->displayNewLine($output)
105
        ;
106
107
        $this
108
            ->sfStyle
109
            ->success('Configuration is valid.');
110
    }
111
112
    /**
113
     * Display sources.
114
     *
115
     * @param OutputInterface $output
116
     * @return ConfigurationDebugCommand $this
117
     */
118 View Code Duplication
    protected function displaySources(OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
119
    {
120
        $this->sfStyle->section('Sources:');
121
        /**
122
         * @var SourceInterface $source
123
         */
124
        foreach ($this->sourcesRegistry as $source) {
125
            $output->writeln(' -> ' . sprintf('%s as %s', get_class($source), $source->getName()));
126
        }
127
128
        return $this;
129
    }
130
131
    /**
132
     * Display processors.
133
     *
134
     * @param OutputInterface $output
135
     * @return ConfigurationDebugCommand $this
136
     */
137 View Code Duplication
    protected function displayProcessors(OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
138
    {
139
        $this->sfStyle->section('Processors:');
140
        /**
141
         * @var ProcessorInterface $processor
142
         */
143
        foreach ($this->processorsRegistry as $processor) {
144
            $output->writeln(' -> ' . get_class($processor));
145
        }
146
147
        return $this;
148
    }
149
150
    /**
151
     * Display repository.
152
     *
153
     * @param OutputInterface $output
154
     * @return ConfigurationDebugCommand $this
155
     */
156
    protected function displayRepository(OutputInterface $output)
157
    {
158
        $this->sfStyle->section('Repository:');
159
160
        $output->writeln(' -> '. sprintf('%s with %s record(s).', get_class($this->repository), $this->repository->count()));
161
162
        return $this;
163
    }
164
165
    /**
166
     * Display rates.
167
     *
168
     * @param OutputInterface $output
169
     * @return ConfigurationDebugCommand $this
170
     */
171
    protected function displayRates(OutputInterface $output)
172
    {
173
        $this->sfStyle->section('Exchange rates:');
174
175
        $table = new Table($output);
176
177
        $table->setHeaders(array(
178
            array(new TableCell('Registered currency rates', array('colspan' => 3))),
179
            array('Currency code', 'Rate type', 'Source')
180
        ));
181
        /**
182
         * @var Configuration $rateConfiguration
183
         */
184
        foreach ($this->ratesConfigurationRegistry as $rateConfiguration) {
185
            $table->addRow(array(
186
                $rateConfiguration->getCurrencyCode(),
187
                $rateConfiguration->getRateType(),
188
                $rateConfiguration->getSourceName()
189
            ));
190
        }
191
192
        $table->render();
193
194
        return $this;
195
    }
196
197
198
    /**
199
     * Display new line.
200
     *
201
     * @param OutputInterface $output
202
     * @return ConfigurationDebugCommand $this
203
     */
204
    protected function displayNewLine(OutputInterface $output)
205
    {
206
        $output->writeln('');
207
208
        return $this;
209
    }
210
}
211