Passed
Branch master (613786)
by Hannes
02:27
created

StatusConsole::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of byrokrat\giroapp.
4
 *
5
 * byrokrat\giroapp is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\giroapp is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016-20 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Console;
24
25
use byrokrat\giroapp\DependencyInjection;
26
use byrokrat\giroapp\Domain\State\AwaitingResponseStateInterface;
27
use byrokrat\giroapp\Domain\State\Active;
28
use byrokrat\giroapp\Domain\State\Error;
29
use byrokrat\giroapp\Domain\State\ExportableStateInterface;
30
use byrokrat\giroapp\Domain\State\Revoked;
31
use byrokrat\giroapp\Domain\State\Paused;
32
use byrokrat\giroapp\Status\StatisticsManager;
33
use Money\Money;
34
use Money\MoneyFormatter;
35
use Symfony\Component\Console\Command\Command;
36
use Symfony\Component\Console\Input\InputOption;
37
use Symfony\Component\Console\Input\InputInterface;
38
use Symfony\Component\Console\Output\OutputInterface;
39
40
/**
41
 * Command to display database status
42
 */
43
final class StatusConsole implements ConsoleInterface
44
{
45
    use DependencyInjection\DonorQueryProperty,
46
        DependencyInjection\MoneyFormatterProperty;
47
48
    /** @var StatisticsManager */
49
    private $statisticsManager;
50
51
    public function __construct(StatisticsManager $statisticsManager)
52
    {
53
        $this->statisticsManager = $statisticsManager;
54
    }
55
56
    public function configure(Command $command): void
57
    {
58
        $command
59
            ->setName('status')
60
            ->setDescription('Show current status')
61
            ->setHelp('Examine the status of the giroapp database')
62
            ->addOption(
63
                'show',
64
                null,
65
                InputOption::VALUE_REQUIRED,
66
                'Show only named statistic (possible values include donor-count, '
67
                . 'monthly-amount, exportable-count, waiting-count, error-count, revoked-count and paused-count)'
68
            )
69
            ->addOption('all', 'a', InputOption::VALUE_NONE, 'Show all statistics, including 0 values')
70
        ;
71
    }
72
73
    public function execute(InputInterface $input, OutputInterface $output): void
74
    {
75
        /** @var string */
76
        $showOnly = $input->getOption('show');
77
78
        if ($showOnly) {
79
            $output->writeln((string)$this->statisticsManager->getStatistic($showOnly)->getValue());
80
            return;
81
        }
82
83
        foreach ($this->statisticsManager->getAllStatistics() as $statistic) {
84
            if ($statistic->getValue() == 0 && !$input->getOption('all')) {
85
                continue;
86
            }
87
88
            $output->writeln("{$statistic->getDescription()}: <comment>{$statistic->getValue()}</comment>");
89
        }
90
    }
91
}
92