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

FindMissingCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Incenteev\TranslationCheckerBundle\Command;
4
5
use Incenteev\TranslationCheckerBundle\Translator\ExposingTranslator;
6
use Incenteev\TranslationCheckerBundle\Translator\Extractor\ExtractorInterface;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Translation\Catalogue\TargetOperation;
12
use Symfony\Component\Translation\MessageCatalogue;
13
14
class FindMissingCommand extends Command
15
{
16
    private $exposingTranslator;
17
    private $extractor;
18
19 7
    public function __construct(ExposingTranslator $exposingTranslator, ExtractorInterface $extractor)
20
    {
21 7
        parent::__construct();
22 7
        $this->exposingTranslator = $exposingTranslator;
23 7
        $this->extractor = $extractor;
24 7
    }
25
26 7
    protected function configure()
27
    {
28 7
        $this->setName('incenteev:translation:find-missing')
29 7
            ->setDescription('Finds the missing translations in a catalogue')
30 7
            ->addArgument('locale', InputArgument::REQUIRED, 'The locale being checked')
31 7
            ->setHelp(<<<HELP
32 7
The <info>%command.name%</info> command extracts translation strings from templates
33
of a given bundle and checks if they are available in the catalogue.
34
35
<info>php %command.full_name% en</info>
36
37
<comment>This command can only identify missing string among the string detected
38
by the translation extractor.</comment>
39
HELP
40
            );
41 7
    }
42
43 5
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45 5
        $extractedCatalogue = new MessageCatalogue($input->getArgument('locale'));
46 5
        $this->extractor->extract($extractedCatalogue);
47
48 5
        $loadedCatalogue = $this->exposingTranslator->getCatalogue($input->getArgument('locale'));
49
50 5
        $operation = new TargetOperation($loadedCatalogue, $extractedCatalogue);
51
52 5
        $valid = true;
53
54 5
        foreach ($operation->getDomains() as $domain) {
55 5
            $messages = $operation->getNewMessages($domain);
56
57 5
            if (empty($messages)) {
58 4
                continue;
59
            }
60
61 3
            $valid = false;
62 3
            $output->writeln(sprintf('<comment>%s</comment> messages are missing in the <info>%s</info> domain', count($messages), $domain));
63
64 3
            if ($output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) {
65 2
                continue;
66
            }
67
68 1
            foreach ($messages as $key => $translation) {
69 1
                $output->writeln('    '.$key);
70
            }
71 1
            $output->writeln('');
72
        }
73
74 5
        if ($valid) {
75 2
            $output->writeln(sprintf(
76 2
                '<info>The <comment>%s</comment> catalogue is in sync with the extracted one.</info>',
77 2
                $input->getArgument('locale')
78
            ));
79
80 2
            return 0;
81
        }
82
83 3
        return 1;
84
    }
85
}
86