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

Command/ImportTranslationsFromFileCommand.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\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 Translator $translator
0 ignored issues
show
Should the type for parameter $translator not be TranslatorInterface?

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...
33
     * @param            $locales
34
     */
35
    public function __construct(Importer $importer, TranslatorInterface $translator, $locales)
36
    {
37
        parent::__construct();
38
        $this->importer = $importer;
39
        $this->translator = $translator;
40
        $this->locales = $locales;
41
    }
42
43
    /**
44
     * Configures this command
45
     */
46
    protected function configure()
47
    {
48
        $this
49
            ->setName('kuma:translator:import-file')
50
            ->setDescription('Import file with translations, supported formats are xlsx, ods, csv')
51
            ->addArgument('file', InputArgument::REQUIRED, 'The full path of the file')
52
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force import, overwrite all existing database entries');
53
    }
54
55
    /**
56
     * @throws LogicException
57
     */
58
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        $file = $input->getArgument('file');
61
        $force = $input->getOption('force');
62
63
        $this->importer->importFromSpreadsheet($file, $this->locales, $force);
64
        if ($force) {
65
            $confirmation = $this->translator->trans('kuma_translator.command.import.flash.force_success');
66
        } else {
67
            $confirmation = $this->translator->trans('kuma_translator.command.import.flash.success');
68
        }
69
        $output->writeln($confirmation);
70
71
        return 0;
72
    }
73
}
74