Completed
Push — master ( 65f893...067f9c )
by Michal
03:11
created

TranslatorCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 6
eloc 16
dl 0
loc 28
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A execute() 0 18 5
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\Command\Translator;
4
5
use Efabrica\TranslationsAutomatization\Exception\InvalidConfigInstanceReturnedException;
6
use InvalidArgumentException;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class TranslatorCommand extends Command
14
{
15 6
    protected function configure()
16
    {
17 6
        $this->setName('translate')
18 6
            ->setDescription('Creates new language version of translated texts')
19 6
            ->addArgument('config', InputArgument::REQUIRED, 'Path to config file. Instance of ' . TranslatorConfig::class  . ' have to be returned')
20 6
            ->addOption('params', null, InputOption::VALUE_REQUIRED, 'Params for config in format a=b&c=d');
21 6
    }
22
23 6
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25 6
        if (!is_file($input->getArgument('config'))) {
26 2
            throw new InvalidArgumentException('File "' . $input->getArgument('config') . '" does not exist');
27
        }
28 4
        parse_str($input->getOption('params'), $params);
29 4
        extract($params);
30
31 4
        $translatorConfig = require_once $input->getArgument('config');
32 4
        if ($translatorConfig instanceof InvalidConfigInstanceReturnedException) {
33
            throw $translatorConfig;
34
        }
35 4
        if (!$translatorConfig instanceof TranslatorConfig) {
36 2
            throw new InvalidConfigInstanceReturnedException('"' . (is_object($translatorConfig) ? get_class($translatorConfig) : $translatorConfig) . '" is not instance of ' . TranslatorConfig::class);
37
        }
38
39 2
        $translatorConfig->translate();
40 2
        $output->writeln('<comment>DONE</comment>');
41 2
    }
42
}
43