Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

ExportTranslationsCommand::execute()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 14
cp 0
rs 8.9137
c 0
b 0
f 0
cc 6
nc 3
nop 2
crap 42
1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\Command;
4
5
use Kunstmaan\TranslatorBundle\Model\Export\ExportCommand;
6
use Kunstmaan\TranslatorBundle\Service\Command\Exporter\ExportCommandHandler;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
12
13
/**
14
 * @final since 5.1
15
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
16
 */
17
class ExportTranslationsCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
18
{
19
    /**
20
     * @var ExportCommandHandler
21
     */
22
    private $exportCommandHandler;
23
24
    /**
25
     * @param ExportCommandHandler|null $exportCommandHandler
26
     */
27
    public function __construct(/* ExportCommandHandler */ $exportCommandHandler = null)
28
    {
29
        parent::__construct();
30
31
        if (!$exportCommandHandler instanceof ExportCommandHandler) {
32
            @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
34
            $this->setName(null === $exportCommandHandler ? 'kuma:translator:export' : $exportCommandHandler);
35
36
            return;
37
        }
38
39
        $this->exportCommandHandler = $exportCommandHandler;
40
    }
41
42
    protected function configure()
43
    {
44
        $this
45
            ->setName('kuma:translator:export')
46
            ->setDescription('Export stashed translations into files (gzip compressed)')
47
            ->addOption('domains', 'd', InputOption::VALUE_REQUIRED, 'Specify which domains to export, default all domains in the stash')
48
            ->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Specify which format files should be, default is yaml')
49
            ->addOption('locales', 'l', InputOption::VALUE_REQUIRED, 'Specifiy which locales to export, default all in the stash')
50
        ;
51
    }
52
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $domains = $input->getOption('domains');
56
        $format = $input->getOption('format');
57
        $locales = $input->getOption('locales');
58
59
        if (null === $format) {
60
            throw new InvalidArgumentException('A format should be defined, e.g --format yml');
61
        }
62
63
        if (null === $this->exportCommandHandler) {
64
            $this->exportCommandHandler = $this->getContainer()->get('kunstmaan_translator.service.exporter.command_handler');
65
        }
66
67
        $exportCommand = new ExportCommand();
68
        $exportCommand
69
            ->setDomains($domains === null ? false : $domains)
70
            ->setFormat($format === null ? false : $format)
71
            ->setLocales($locales === null ? false : $locales);
72
73
        $this->exportCommandHandler->executeExportCommand($exportCommand);
74
75
        return 0;
76
    }
77
}
78