FindMissingCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6

Importance

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