Completed
Push — master ( e62fa2...4391c3 )
by Kristof
133:20 queued 117:59
created

Command/ImportTranslationsCommand.php (3 issues)

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
     * @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
0 ignored issues
show
Should the type for parameter $importCommandHandler not be ImportCommandHandler|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
42
     * @param string               $defaultBundle
43
     * @param array                $bundles
44
     */
45 View Code Duplication
    public function __construct(/* ImportCommandHandler */
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
        $importCommandHandler = null,
47
        string $defaultBundle,
48
        array $bundles
49
    ) {
50
        parent::__construct();
51
52
        if (!$importCommandHandler instanceof ImportCommandHandler) {
53
            @trigger_error(
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...
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
}
124