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

TranslationFinderCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 25
c 0
b 0
f 0
ccs 0
cts 16
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 15 4
A configure() 0 6 1
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