Completed
Push — master ( e62fa2...4391c3 )
by Kristof
133:20 queued 117:59
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
22
{
23
    /**
24
     * @var ImportCommandHandler
25
     */
26
    private $importCommandHandler;
27
28
    /**
29
     * @param ImportCommandHandler|null $importCommandHandler
30
     */
31
    public function __construct(/* ImportCommandHandler */ $importCommandHandler = null)
32
    {
33
        parent::__construct();
34
35
        if (!$importCommandHandler instanceof ImportCommandHandler) {
36
            @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 Jeroen Thora
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...
37
38
            $this->setName(null === $importCommandHandler ? 'kuma:translator:import' : $importCommandHandler);
39
40
            return;
41
        }
42
43
        $this->importCommandHandler = $importCommandHandler;
44
    }
45
46
    /**
47
     * Configures this command.
48
     */
49
    protected function configure()
50
    {
51
        $this
52
            ->setName('kuma:translator:import')
53
            ->setDescription('Import translation files into database')
54
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force import, overwrite all existing database entries')
55
            ->addOption('locales', 'l', InputOption::VALUE_REQUIRED, 'Language import, only import a specific locale')
56
            ->addOption('globals', 'g', InputOption::VALUE_NONE, 'Global app import, import the global translations of your app')
57
            ->addOption('defaultbundle', 'd', InputOption::VALUE_REQUIRED, 'Import the translations for specific bundles, use "own", "all" or "custom"')
58
            ->addOption('bundles', 'b', InputOption::VALUE_OPTIONAL, 'A list of bundle names that need to be imported (comma delimited) , only used When "defaultbundle" is set to "custom"')
59
        ;
60
    }
61
62
    /**
63
     * @param InputInterface  $input
64
     * @param OutputInterface $output
65
     */
66
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68
        $force = $input->getOption('force');
69
        $locales = $input->getOption('locales');
70
        $globals = $input->getOption('globals');
71
        $defaultBundle = $input->getOption('defaultbundle');
72
        $bundles = $input->hasOption('bundles') ? array_map('trim', explode(',', $input->getOption('bundles'))) : array();
73
        if (null === $this->importCommandHandler) {
74
            $this->importCommandHandler = $this->getContainer()->get('kunstmaan_translator.service.importer.command_handler');
75
        }
76
77
        $importCommand = new ImportCommand();
78
        $importCommand
79
            ->setForce($force)
80
            ->setLocales($locales)
81
            ->setGlobals($globals)
82
            ->setDefaultBundle($defaultBundle)
83
            ->setBundles($bundles);
84
85
        $imported = $this->importCommandHandler->executeImportCommand($importCommand);
86
87
        $output->writeln(sprintf('Translation imported: %d', $imported));
88
    }
89
}
90