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

InitCommand::execute()   C

Complexity

Conditions 9
Paths 7

Size

Total Lines 64
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 6.5449
c 0
b 0
f 0
cc 9
eloc 33
nc 7
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Storeman\Cli\Command;
4
5
use Storeman\Configuration;
6
use Storeman\ConfigurationFileWriter;
7
use Storeman\PathUtils;
8
use Storeman\VaultConfiguration;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class InitCommand extends AbstractCommand
14
{
15
    protected function configure()
16
    {
17
        parent::configure();
18
19
        $this->setName('init');
20
        $this->setDescription('Sets up a local archive copy.');
21
        $this->addOption('path', null, InputOption::VALUE_REQUIRED, 'Path to use as local path for the archive.');
22
        $this->addOption('identity', 'i', InputOption::VALUE_REQUIRED, 'Identity to be used.');
23
        $this->addOption('exclude', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Relative path exclusion(s).');
24
        $this->addOption('writeDefaults', null, InputOption::VALUE_NONE, 'Forces writing of default values which are omitted as a default.');
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function execute(InputInterface $input, OutputInterface $output): int
31
    {
32
        parent::execute($input, $output);
33
34
        $configFilePath = PathUtils::getAbsolutePath($input->getOption('config'));
35
        $configFileDir = dirname($configFilePath);
36
37
        if (!is_writable($configFileDir))
38
        {
39
            $output->writeln("<error>Cannot write to {$configFileDir}</error>");
40
41
            return 1;
42
        }
43
44
45
        $container = $this->getContainer();
46
47
        $configuration = new Configuration($input->getOption('path') ?: $this->consoleStyle->ask('Local path', '.'));
48
        $configuration->setIdentity($input->getOption('identity') ?: $this->consoleStyle->ask('Identity', get_current_user()));
49
        $configuration->setExclude($input->getOption('exclude') ?: $this->consoleStyle->askMultiple('Excluded path(s)'));
50
51
        // at least one storage driver has to be set up
52
        do
53
        {
54
            $vaultConfig = new VaultConfiguration($this->consoleStyle->choice('Storage driver', $container->getStorageAdapterNames()));
55
            $vaultConfig->setTitle($this->consoleStyle->ask('Title', $vaultConfig->getAdapter()));
56
            $vaultConfig->setLockAdapter($this->consoleStyle->choice('Lock adapter', $container->getLockAdapterNames(), $vaultConfig->getLockAdapter()));
57
            $vaultConfig->setIndexMerger($this->consoleStyle->choice('Index merger', $container->getIndexMergerNames(), $vaultConfig->getIndexMerger()));
58
            $vaultConfig->setConflictHandler($this->consoleStyle->choice('Conflict handler', $container->getConflictHandlerNames(), $vaultConfig->getConflictHandler()));
59
            $vaultConfig->setOperationListBuilder($this->consoleStyle->choice('Operation list builder', $container->getOperationListBuilderNames(), $vaultConfig->getOperationListBuilder()));
60
61
            while ($settingName = $this->consoleStyle->ask('Additional setting name'))
62
            {
63
                if ($settingValue = $this->consoleStyle->ask('Additional setting value'))
64
                {
65
                    $vaultConfig->setSetting($settingName, $settingValue);
66
                }
67
            }
68
69
            $configuration->addVault($vaultConfig);
70
        }
71
        while($this->consoleStyle->choice('Add another vault?', ['y', 'n'], 'n') === 'y');
72
73
74
        $skipDefaults = !$input->getOption('writeDefaults');
75
76
        $configurationFileWriter = new ConfigurationFileWriter();
77
78
        $output->writeln("The following content will be written to {$configFilePath}:");
79
        $output->writeln($configurationFileWriter->buildConfigurationFile($configuration, $skipDefaults));
80
81
        if ($this->consoleStyle->confirm('Continue? ', true))
82
        {
83
            $configurationFileWriter->writeConfigurationFile($configuration, $configFilePath, $skipDefaults);
84
85
            $output->writeln("<info>Successfully written config file to {$configFilePath}</info>");
86
        }
87
        else
88
        {
89
            $output->writeln("<comment>Aborted</comment>");
90
        }
91
92
        return 0;
93
    }
94
}
95