Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

Command/ImportTranslationsFromFileCommand.php (1 issue)

Labels
Severity

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\Service\Command\Importer\Importer;
6
use Kunstmaan\TranslatorBundle\Service\Translator\Translator;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Exception\LogicException;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Translation\TranslatorInterface;
14
15
/**
16
 * Class ImportTranslationsFromFileCommand
17
 */
18
final class ImportTranslationsFromFileCommand extends Command
19
{
20
    /** @var Importer */
21
    private $importer;
22
23
    /** @var TranslatorInterface */
24
    private $translator;
25
26
    /** @var array */
27
    private $locales;
28
29
    /**
30
     * ImportTranslationsFromFileCommand constructor.
31
     *
32
     * @param Importer   $importer
33
     * @param Translator $translator
34
     * @param            $locales
35
     */
36
    public function __construct(Importer $importer, TranslatorInterface $translator, $locales)
37
    {
38
        parent::__construct();
39
        $this->importer = $importer;
40
        $this->translator = $translator;
41
        $this->locales = $locales;
42
    }
43
44
    /**
45
     * Configures this command
46
     */
47
    protected function configure()
48
    {
49
        $this
50
            ->setName('kuma:translator:import-file')
51
            ->setDescription('Import file with translations, supported formats are xlsx, ods, csv')
52
            ->addArgument('file', InputArgument::REQUIRED, 'The full path of the file')
53
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force import, overwrite all existing database entries');
54
    }
55
56
    /**
57
     * @throws LogicException
58
     *
59
     * @param InputInterface  $input
60
     * @param OutputInterface $output
61
     */
62
    protected function execute(InputInterface $input, OutputInterface $output)
63
    {
64
        $file = $input->getArgument('file');
65
        $force = $input->getOption('force');
66
67
        $this->importer->importFromSpreadsheet($file, $this->locales, $force);
0 ignored issues
show
It seems like $file defined by $input->getArgument('file') on line 64 can also be of type array<integer,string> or null; however, Kunstmaan\TranslatorBund...importFromSpreadsheet() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
        if ($force) {
69
            $confirmation = $this->translator->trans('kuma_translator.command.import.flash.force_success');
70
        } else {
71
            $confirmation = $this->translator->trans('kuma_translator.command.import.flash.success');
72
        }
73
        $output->writeln($confirmation);
74
    }
75
}
76