Issues (83)

Command/SyncCommand.php (2 issues)

Languages
Labels
Severity
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Command;
13
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Translation\Bundle\Service\StorageManager;
20
use Translation\Bundle\Service\StorageService;
21
22
/**
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
class SyncCommand extends Command
26
{
27
    use StorageTrait;
28
29
    protected static $defaultName = 'translation:sync';
30
31
    public function __construct(StorageManager $storageManager)
32
    {
33
        $this->storageManager = $storageManager;
34
35
        parent::__construct();
36
    }
37
38
    protected function configure(): void
39
    {
40
        $this
41
            ->setName(self::$defaultName)
42
            ->setDescription('Sync the translations with the remote storage')
43
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
44
            ->addArgument('direction', InputArgument::OPTIONAL, 'Use "down" if local changes should be overwritten, otherwise "up"', 'down')
45
            ->addOption('export-config', 'exconf', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Options to send to the StorageInterface::export() function. Ie, when downloading. Example: --export-config foo:bar', [])
46
            ->addOption('import-config', 'imconf', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Options to send to the StorageInterface::import() function. Ie, when uploading. Example: --import-config foo:bar', [])
47
        ;
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output): int
51
    {
52
        switch ($input->getArgument('direction')) {
53
            case 'down':
54
                $direction = StorageService::DIRECTION_DOWN;
55
56
                break;
57
            case 'up':
58
                $direction = StorageService::DIRECTION_UP;
59
60
                break;
61
            default:
62
                $output->writeln(\sprintf('Direction must be either "up" or "down". Not "%s".', $input->getArgument('direction')));
0 ignored issues
show
It seems like $input->getArgument('direction') can also be of type string[]; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
                $output->writeln(\sprintf('Direction must be either "up" or "down". Not "%s".', /** @scrutinizer ignore-type */ $input->getArgument('direction')));
Loading history...
63
64
                return 0;
65
        }
66
67
        $export = $this->cleanParameters($input->getOption('export-config'));
0 ignored issues
show
It seems like $input->getOption('export-config') can also be of type boolean and null and string; however, parameter $raw of Translation\Bundle\Comma...mand::cleanParameters() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $export = $this->cleanParameters(/** @scrutinizer ignore-type */ $input->getOption('export-config'));
Loading history...
68
        $import = $this->cleanParameters($input->getOption('import-config'));
69
70
        $this->getStorage($input->getArgument('configuration'))->sync($direction, $import, $export);
71
72
        return 0;
73
    }
74
75
    public function cleanParameters(array $raw)
76
    {
77
        $config = [];
78
79
        foreach ($raw as $string) {
80
            // Assert $string looks like "foo:bar"
81
            list($key, $value) = \explode(':', $string, 2);
82
            $config[$key][] = $value;
83
        }
84
85
        return $config;
86
    }
87
}
88