StatusCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
c 2
b 0
f 0
dl 0
loc 81
ccs 0
cts 49
cp 0
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B execute() 0 36 6
A configure() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Command;
13
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Translation\Bundle\Catalogue\CatalogueCounter;
21
use Translation\Bundle\Catalogue\CatalogueFetcher;
22
use Translation\Bundle\Service\ConfigurationManager;
23
24
/**
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
class StatusCommand extends Command
28
{
29
    use BundleTrait;
30
31
    protected static $defaultName = 'translation:status';
32
33
    /**
34
     * @var CatalogueCounter
35
     */
36
    private $catalogueCounter;
37
38
    /**
39
     * @var ConfigurationManager
40
     */
41
    private $configurationManager;
42
43
    /**
44
     * @var CatalogueFetcher
45
     */
46
    private $catalogueFetcher;
47
48
    public function __construct(
49
        CatalogueCounter $catalogueCounter,
50
        ConfigurationManager $configurationManager,
51
        CatalogueFetcher $catalogueFetcher
52
    ) {
53
        $this->catalogueCounter = $catalogueCounter;
54
        $this->configurationManager = $configurationManager;
55
        $this->catalogueFetcher = $catalogueFetcher;
56
57
        parent::__construct();
58
    }
59
60
    protected function configure(): void
61
    {
62
        $this
63
            ->setName(self::$defaultName)
64
            ->setDescription('Show status about your translations.')
65
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
66
            ->addArgument('locale', InputArgument::OPTIONAL, 'The locale to use. If omitted, we use all configured locales.', false)
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type null|string|string[] expected by parameter $default of Symfony\Component\Consol...\Command::addArgument(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            ->addArgument('locale', InputArgument::OPTIONAL, 'The locale to use. If omitted, we use all configured locales.', /** @scrutinizer ignore-type */ false)
Loading history...
67
            ->addOption('json', null, InputOption::VALUE_NONE, 'If we should output in Json format')
68
            ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle for which you want to check the translations.')
69
        ;
70
    }
71
72
    protected function execute(InputInterface $input, OutputInterface $output): int
73
    {
74
        $configName = $input->getArgument('configuration');
75
        $config = $this->configurationManager->getConfiguration($configName);
76
77
        $this->configureBundleDirs($input, $config);
78
79
        $locales = [];
80
        if ($inputLocale = $input->getArgument('locale')) {
81
            $locales = [$inputLocale];
82
        }
83
84
        $catalogues = $this->catalogueFetcher->getCatalogues($config, $locales);
85
86
        $stats = [];
87
        foreach ($catalogues as $catalogue) {
88
            $stats[$catalogue->getLocale()] = $this->catalogueCounter->getCatalogueStatistics($catalogue);
89
        }
90
91
        if ($input->getOption('json')) {
92
            $output->writeln(\json_encode($stats));
93
94
            return 0;
95
        }
96
97
        $io = new SymfonyStyle($input, $output);
98
        foreach ($stats as $locale => $stat) {
99
            $rows = [];
100
            foreach ($stat as $domain => $data) {
101
                $rows[] = [$domain, $data['defined'], $data['new'], $data['obsolete']];
102
            }
103
            $io->title('Locale: '.$locale);
104
            $io->table(['Domain', 'Defined', 'New', 'Obsolete'], $rows);
105
        }
106
107
        return 0;
108
    }
109
}
110