Completed
Push — master ( 356e08...d08624 )
by Tobias
09:26
created

StatusCommand::execute()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.991

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 33
ccs 13
cts 21
cp 0.619
rs 8.439
c 1
b 0
f 0
cc 6
eloc 20
nc 16
nop 2
crap 7.991
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
    /**
49
     * @param CatalogueCounter     $catalogueCounter
50
     * @param ConfigurationManager $configurationManager
51
     * @param CatalogueFetcher     $catalogueFetcher
52
     */
53 1
    public function __construct(
54
        CatalogueCounter $catalogueCounter,
55
        ConfigurationManager $configurationManager,
56
        CatalogueFetcher $catalogueFetcher
57
    ) {
58 1
        $this->catalogueCounter = $catalogueCounter;
59 1
        $this->configurationManager = $configurationManager;
60 1
        $this->catalogueFetcher = $catalogueFetcher;
61
62 1
        parent::__construct();
63 1
    }
64
65 1 View Code Duplication
    protected function configure()
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...
66
    {
67
        $this
68 1
            ->setName(self::$defaultName)
69 1
            ->setDescription('Show status about your translations.')
70 1
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
71 1
            ->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', false)
72 1
            ->addOption('json', null, InputOption::VALUE_NONE, 'If we should output in Json format')
73 1
            ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The translations for bundle you want to check.')
74
        ;
75 1
    }
76
77 1
    protected function execute(InputInterface $input, OutputInterface $output)
78
    {
79 1
        $config = $this->configurationManager->getConfiguration($input->getArgument('configuration'));
80 1
        $this->configureBundleDirs($input, $config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->configurationMana...ument('configuration')) on line 79 can be null; however, Translation\Bundle\Comma...::configureBundleDirs() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
81
82 1
        $locales = [];
83 1
        if ($inputLocale = $input->getArgument('locale')) {
84 1
            $locales = [$inputLocale];
85
        }
86
87 1
        $catalogues = $this->catalogueFetcher->getCatalogues($config, $locales);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->configurationMana...ument('configuration')) on line 79 can be null; however, Translation\Bundle\Catal...etcher::getCatalogues() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
88
89 1
        $stats = [];
90 1
        foreach ($catalogues as $catalogue) {
91 1
            $stats[$catalogue->getLocale()] = $this->catalogueCounter->getCatalogueStatistics($catalogue);
92
        }
93
94 1
        if ($input->getOption('json')) {
95 1
            $output->writeln(json_encode($stats));
96
97 1
            return;
98
        }
99
100
        $io = new SymfonyStyle($input, $output);
101
        foreach ($stats as $locale => $stat) {
102
            $rows = [];
103
            foreach ($stat as $domain => $data) {
104
                $rows[] = [$domain, $data['defined'], $data['new'], $data['obsolete']];
105
            }
106
            $io->title('Locale: '.$locale);
107
            $io->table(['Domain', 'Defined', 'New', 'Obsolete'], $rows);
108
        }
109
    }
110
}
111