Completed
Pull Request — master (#19)
by Michal
05:19 queued 03:25
created

TranslationFinderCommand::execute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 2
crap 20
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\TranslationFinder\Command;
4
5
use Efabrica\TranslationsAutomatization\Exception\InvalidConfigInstanceReturnedException;
6
use Efabrica\TranslationsAutomatization\TranslationFinder\TranslationFinder;
7
use InvalidArgumentException;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class TranslationFinderCommand extends Command
14
{
15
    protected function configure()
16
    {
17
        $this->setName('finder')
18
            ->setDescription('Finds non-translated texts and replaces them with translate tokens')
19
            ->addOption('config', null, InputOption::VALUE_REQUIRED, 'Path to config file. Instance of TranslationFinder have to be returned')
20
            ->addOption('params', null, InputOption::VALUE_REQUIRED, 'Params for config in format a=b&c=d');
21
    }
22
23
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        if (!is_file($input->getOption('config'))) {
26
            throw new InvalidArgumentException('File "' . $input->getOption('config') . '" does not exist');
27
        }
28
        parse_str($input->getOption('params'), $params);
29
        extract($params);
30
31
        $translationFinder = require_once $input->getOption('config');
32
        if (!$translationFinder instanceof TranslationFinder) {
33
            throw new InvalidConfigInstanceReturnedException('"' . (is_object($translationFinder) ? get_class($translationFinder) : $translationFinder) . '" is not instance of ' . TranslationFinder::class);
34
        }
35
36
        $result = $translationFinder->translate();
37
        $output->writeln('<comment>' . $result . ' tokens replaced</comment>');
38
    }
39
}
40