Completed
Push — master ( 2cde75...6c52d6 )
by Arne
02:04
created

SynchronizeCommand::executeConfigured()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 8
nop 3
1
<?php
2
3
namespace Storeman\Cli\Command;
4
5
use Storeman\Storeman;
6
use Storeman\Cli\SynchronizationProgressListener;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class SynchronizeCommand extends AbstractCommand
12
{
13
    protected function configure()
14
    {
15
        parent::configure();
16
17
        $this->setName('sync');
18
        $this->setDescription('Synchronizes the local state with the vault state.');
19
        $this->addOption('vaults', null, InputOption::VALUE_REQUIRED, 'Comma-separated list of vault titles to synchronize with. Defaults to all configured.');
20
        $this->addOption('prefer-local', null, InputOption::VALUE_NONE, 'Always prefers local changes over remote changes.');
21
        $this->addOption('prefer-remote', null, InputOption::VALUE_NONE, 'Always prefers remote changes over local changes.');
22
    }
23
24
    protected function executeConfigured(InputInterface $input, OutputInterface $output, Storeman $storeman): int
25
    {
26
        $vaultTitles = $input->getOption('vaults') ? explode(',', $input->getOption('vaults')) : [];
27
        $preferLocal = $input->getOption('prefer-local');
28
        $preferRemote = $input->getOption('prefer-remote');
29
30
        if ($preferLocal && $preferRemote)
31
        {
32
            $output->writeln('<error>Only one of --prefer-local and --prefer-remote options can be used at the same time.</error>');
33
34
            return 1;
35
        }
36
37
        $configuration = $storeman->getConfiguration();
38
39
        if ($preferLocal)
40
        {
41
            foreach ($configuration->getVaults() as $vaultConfiguration)
42
            {
43
                $vaultConfiguration->setConflictHandler('preferLocal');
44
            }
45
        }
46
        elseif ($preferRemote)
47
        {
48
            foreach ($configuration->getVaults() as $vaultConfiguration)
49
            {
50
                $vaultConfiguration->setConflictHandler('preferRemote');
51
            }
52
        }
53
54
        $storeman->synchronize($vaultTitles, new SynchronizationProgressListener($output));
55
56
        $output->writeln(PHP_EOL . '<info>Done!</info>');
57
58
        return 0;
59
    }
60
}
61