FindMissingCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 70
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A __construct() 0 5 1
B execute() 0 41 6
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