ExtractorCommand::execute()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7.2791

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 38
ccs 23
cts 28
cp 0.8214
rs 8.5546
cc 7
nc 7
nop 2
crap 7.2791
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\Command\Extractor;
4
5
use Efabrica\TranslationsAutomatization\Exception\InvalidConfigInstanceReturnedException;
6
use Efabrica\TranslationsAutomatization\Tokenizer\Token;
7
use InvalidArgumentException;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class ExtractorCommand extends Command
16
{
17 6
    protected function configure()
18
    {
19 6
        $this->setName('extract')
20 6
            ->setDescription('Finds non-translated texts, replaces them with translate tokens and store these texts to storage')
21 6
            ->addArgument('config', InputArgument::REQUIRED, 'Path to config file. Instance of ' . ExtractorConfig::class  . ' have to be returned')
22 6
            ->addOption('params', null, InputOption::VALUE_REQUIRED, 'Params for config in format --params="a=b&c=d"');
23 6
    }
24
25 6
    protected function execute(InputInterface $input, OutputInterface $output)
26
    {
27 6
        if (!is_file($input->getArgument('config'))) {
28 2
            throw new InvalidArgumentException('File "' . $input->getArgument('config') . '" does not exist');
29
        }
30 4
        parse_str($input->getOption('params'), $params);
31 4
        extract($params);
32
33 4
        $extractorConfig = require_once $input->getArgument('config');
34 4
        if ($extractorConfig instanceof InvalidConfigInstanceReturnedException) {
35
            throw $extractorConfig;
36
        }
37 4
        if (!$extractorConfig instanceof ExtractorConfig) {
38 2
            throw new InvalidConfigInstanceReturnedException('"' . (is_object($extractorConfig) ? get_class($extractorConfig) : $extractorConfig) . '" is not instance of ' . ExtractorConfig::class);
39
        }
40
41 2
        $output->writeln('');
42 2
        $output->writeln('Finding tokens...');
43 2
        $tokenCollections = $extractorConfig->extract();
44 2
        $totalTokens = 0;
45 2
        foreach ($tokenCollections as $tokenCollection) {
46
            $totalTokens += count($tokenCollection->getTokens());
47
        }
48 2
        $output->writeln($totalTokens . ' tokens found');
49 2
        $output->writeln('Processing tokens...');
50 2
        $output->writeln('');
51
52 2
        $progressBar = new ProgressBar($output, $totalTokens);
53 2
        $tokensReplaced = 0;
54 2
        foreach ($tokenCollections as $tokenCollection) {
55
            $extractorConfig->process($tokenCollection, function (Token $token) use ($progressBar, &$tokensReplaced) {
0 ignored issues
show
Unused Code introduced by
The parameter $token is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
            $extractorConfig->process($tokenCollection, function (/** @scrutinizer ignore-unused */ Token $token) use ($progressBar, &$tokensReplaced) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
                $progressBar->advance();
57
                $tokensReplaced++;
58
            });
59
        }
60 2
        $output->writeln("\n\n");
61 2
        $output->writeln('<comment>' . $tokensReplaced . ' tokens replaced</comment>');
62 2
        return $tokensReplaced;
63
    }
64
}
65