Completed
Push — master ( fef7a6...aff413 )
by Arne
02:38
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 Archivr\Cli\Command;
4
5
use Archivr\ArchivR;
6
use Archivr\Cli\SynchronizationProgressListener;
7
use Archivr\Configuration;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class SynchronizeCommand extends AbstractConfiguredCommand
13
{
14
    protected function configure()
15
    {
16
        parent::configure();
17
18
        $this->setName('sync');
19
        $this->setDescription('Synchronizes the local state with the vault state.');
20
        $this->addOption('vaults', null, InputOption::VALUE_REQUIRED, 'Comma-separated list of vault titles to synchronize with. Defaults to all configured.');
21
        $this->addOption('prefer-local', null, InputOption::VALUE_NONE, 'Always prefers local changes over remote changes.');
22
        $this->addOption('prefer-remote', null, InputOption::VALUE_NONE, 'Always prefers remote changes over local changes.');
23
    }
24
25
    protected function executeConfigured(InputInterface $input, OutputInterface $output, Configuration $configuration): int
26
    {
27
        $vaultTitles = $input->getOption('vaults') ? explode(',', $input->getOption('vaults')) : [];
28
        $preferLocal = $input->getOption('prefer-local');
29
        $preferRemote = $input->getOption('prefer-remote');
30
31
        if ($preferLocal && $preferRemote)
32
        {
33
            $output->writeln('<error>Only one of --prefer-local and --prefer-remote options can be used at the same time.</error>');
34
35
            return 1;
36
        }
37
38
        if ($preferLocal)
39
        {
40
            foreach ($configuration->getVaultConfigurations() as $vaultConfiguration)
41
            {
42
                $vaultConfiguration->setConflictHandler('preferLocal');
43
            }
44
        }
45
        elseif ($preferRemote)
46
        {
47
            foreach ($configuration->getVaultConfigurations() as $vaultConfiguration)
48
            {
49
                $vaultConfiguration->setConflictHandler('preferRemote');
50
            }
51
        }
52
53
        $archivr = new ArchivR($configuration);
54
        $archivr->synchronize($vaultTitles, new SynchronizationProgressListener($output));
55
56
        $output->writeln(PHP_EOL . '<info>Done!</info>');
57
58
        return 0;
59
    }
60
}
61