Completed
Push — master ( 6a0e64...33e708 )
by Tobias
03:13
created

SyncCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 12
loc 63
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 11 1
A execute() 0 24 3
A cleanParameters() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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')));
63
64
                return 0;
65
        }
66
67
        $export = $this->cleanParameters($input->getOption('export-config'));
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 View Code Duplication
    public function cleanParameters(array $raw)
0 ignored issues
show
Duplication introduced by
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...
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