Completed
Pull Request — master (#19)
by Christophe
04:30
created

CompareCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Incenteev\TranslationCheckerBundle\Command;
4
5
use Incenteev\TranslationCheckerBundle\Translator\ExposingTranslator;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Translation\Catalogue\TargetOperation;
12
use Symfony\Component\Translation\MessageCatalogue;
13
14
class CompareCommand extends Command
15
{
16
    private $exposingTranslator;
17
18 13
    public function __construct(ExposingTranslator $exposingTranslator)
19
    {
20 13
        parent::__construct();
21
22 13
        $this->exposingTranslator = $exposingTranslator;
23 13
    }
24
25 13
    protected function configure()
26
    {
27 13
        $this->setName('incenteev:translation:compare')
28 13
            ->setDescription('Compares two translation catalogues to ensure they are in sync')
29 13
            ->addArgument('locale', InputArgument::REQUIRED, 'The locale being checked')
30 13
            ->addArgument('source', InputArgument::OPTIONAL, 'The source of the comparison', 'en')
31 13
            ->addOption('obsolete-only', null, InputOption::VALUE_NONE, 'Report only obsolete keys')
32 13
            ->addOption('domain', 'd', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The domains being compared')
33 13
            ->setHelp(<<<EOF
34 13
The <info>%command.name%</info> command compares 2 translation catalogues to
35
ensure they are in sync. If there is missing keys or obsolete keys in the target
36
catalogue, the command will exit with an error code.
37
38
When running the command in verbose mode, the translation keys will also be displayed.
39
<info>php %command.full_name% fr --verbose</info>
40
41
The <info>--domain</info> option allows to restrict the domains being checked.
42
It can be specified several times to check several domains. If the option is not passed,
43
all domains will be compared.
44
45
The <info>--obsolete-only</info> option allows to check only obsolete keys, and ignore any
46
missing keys.
47
EOF
48
            );
49 13
    }
50
51 13
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53 13
        $sourceCatalogue = $this->exposingTranslator->getCatalogue($input->getArgument('source'));
54 13
        $comparedCatalogue = $this->exposingTranslator->getCatalogue($input->getArgument('locale'));
55
56
        // Change the locale of the catalogue as DiffOperation requires operating on a single locale
57 13
        $catalogue = new MessageCatalogue($sourceCatalogue->getLocale(), $comparedCatalogue->all());
58
59 13
        $operation = new TargetOperation($catalogue, $sourceCatalogue);
60
61 13
        $domains = $operation->getDomains();
62 13
        $restrictedDomains = $input->getOption('domain');
63 13
        if (!empty($restrictedDomains)) {
64 4
            $domains = array_intersect($domains, $restrictedDomains);
65 4
            $output->writeln(sprintf('<comment>Checking the domains %s</comment>', implode(', ', $domains)));
66
        }
67
68 13
        $checkMissing = !$input->getOption('obsolete-only');
69
70 13
        $valid = true;
71
72 13
        foreach ($domains as $domain) {
73 13
            $missingMessages = $checkMissing ? $operation->getNewMessages($domain) : array();
74 13
            $obsoleteMessages = $operation->getObsoleteMessages($domain);
75 13
            $written = false;
76
77 13 View Code Duplication
            if (!empty($missingMessages)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
78 5
                $valid = false;
79 5
                $written = true;
80 5
                $output->writeln(sprintf('<comment>%s</comment> messages are missing in the <info>%s</info> domain', count($missingMessages), $domain));
81
82 5
                $this->displayMessages($output, $missingMessages);
83
            }
84
85 13 View Code Duplication
            if (!empty($obsoleteMessages)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86 5
                $valid = false;
87 5
                $written = true;
88 5
                $output->writeln(sprintf('<comment>%s</comment> messages are obsolete in the <info>%s</info> domain', count($obsoleteMessages), $domain));
89
90 5
                $this->displayMessages($output, $obsoleteMessages);
91
            }
92
93 13
            if ($written) {
94 13
                $output->writeln('');
95
            }
96
        }
97
98 13
        if ($valid) {
99 5
            $output->writeln(sprintf(
100 5
                '<info>The <comment>%s</comment> catalogue is in sync with the <comment>%s</comment> one.</info>',
101 5
                $input->getArgument('locale'),
102 5
                $input->getArgument('source')
103
            ));
104
105 5
            return 0;
106
        }
107
108 8
        return 1;
109
    }
110
111 8
    private function displayMessages(OutputInterface $output, array $messages)
112
    {
113 8
        if ($output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) {
114 6
            return;
115
        }
116
117 2
        foreach ($messages as $key => $translation) {
118 2
            $output->writeln('    '.$key);
119
        }
120 2
        $output->writeln('');
121 2
    }
122
}
123