Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Command/ExportTranslationsCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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);
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