Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Command/ImportTranslationsCommand.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\Import\ImportCommand;
6
use Kunstmaan\TranslatorBundle\Service\Command\Importer\ImportCommandHandler;
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
12
/**
13
 * @final      since 5.1
14
 *
15
 * @deprecated since 5.1
16
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
17
 * NEXT_MAJOR file will be renamed
18
 *
19
 * Class ImportTranslationsFromCodeCommand
20
 */
21
class ImportTranslationsCommand 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...
22
{
23
    /**
24
     * @var ImportCommandHandler
25
     */
26
    private $importCommandHandler;
27
28
    /**
29
     * @var string
30
     */
31
    private $defaultBundle;
32
33
    /**
34
     * @var array
35
     */
36
    private $bundles;
37
38
    /**
39
     * ImportTranslationsCommand constructor.
40
     *
41
     * @param ImportCommandHandler $importCommandHandler
42
     * @param string               $defaultBundle
43
     * @param array                $bundles
44
     */
45 View Code Duplication
    public function __construct(/* ImportCommandHandler */
46
        $importCommandHandler = null,
47
        string $defaultBundle,
48
        array $bundles
49
    ) {
50
        parent::__construct();
51
52
        if (!$importCommandHandler instanceof ImportCommandHandler) {
53
            @trigger_error(
54
                sprintf(
55
                    '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. ',
56
                    __METHOD__
57
                ),
58
                E_USER_DEPRECATED
59
            );
60
61
            $this->setName(null === $importCommandHandler ? 'kuma:translator:import' : $importCommandHandler);
62
63
            return;
64
        }
65
66
        $this->importCommandHandler = $importCommandHandler;
67
        $this->defaultBundle = $defaultBundle;
68
        $this->bundles = $bundles;
69
    }
70
71
    /**
72
     * Configures this command.
73
     */
74
    protected function configure()
75
    {
76
        $this
77
            ->setName('kuma:translator:import')
78
            ->setDescription('Import translation files into database')
79
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force import, overwrite all existing database entries')
80
            ->addOption('locales', 'l', InputOption::VALUE_REQUIRED, 'Language import, only import a specific locale')
81
            ->addOption('globals', 'g', InputOption::VALUE_NONE, 'Global app import, import the global translations of your app')
82
            ->addOption(
83
                'defaultbundle',
84
                'd',
85
                InputOption::VALUE_REQUIRED,
86
                'Import the translations for specific bundles, use "own", "all" or "custom"'
87
            )
88
            ->addOption(
89
                'bundles',
90
                'b',
91
                InputOption::VALUE_OPTIONAL,
92
                'A list of bundle names that need to be imported (comma delimited) , only used When "defaultbundle" is set to "custom"'
93
            );
94
    }
95
96
    /**
97
     * @param InputInterface  $input
98
     * @param OutputInterface $output
99
     */
100
    protected function execute(InputInterface $input, OutputInterface $output)
101
    {
102
        $force = $input->getOption('force');
103
        $locales = $input->getOption('locales');
104
        $globals = $input->getOption('globals');
105
        $defaultBundle = $input->getOption('defaultbundle') ?: $this->defaultBundle;
106
        $bundles = $input->getOption('bundles') ? array_map('trim', explode(',', $input->getOption('bundles'))) : $this->bundles;
107
        if (null === $this->importCommandHandler) {
108
            $this->importCommandHandler = $this->getContainer()->get('kunstmaan_translator.service.importer.command_handler');
109
        }
110
111
        $importCommand = new ImportCommand();
112
        $importCommand
113
            ->setForce($force)
114
            ->setLocales($locales)
115
            ->setGlobals($globals)
116
            ->setDefaultBundle($defaultBundle)
117
            ->setBundles($bundles);
118
119
        $imported = $this->importCommandHandler->executeImportCommand($importCommand);
120
121
        $output->writeln(sprintf('Translation imported: %d', $imported));
122
123
        return 0;
124
    }
125
}
126