Passed
Pull Request — master (#18)
by Michal
02:43
created

TranslationFinderCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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