Completed
Push — master ( fef7a6...aff413 )
by Arne
02:38
created

AbstractConfiguredCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 60
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A execute() 0 13 2
executeConfigured() 0 1 ?
A getConfiguration() 0 16 3
1
<?php
2
3
namespace Archivr\Cli\Command;
4
5
use Archivr\Configuration;
6
use Archivr\ConfigurationFileReader;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
abstract class AbstractConfiguredCommand extends AbstractCommand
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    protected function configure()
17
    {
18
        parent::configure();
19
20
        $this->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Configuration file to use. Defaults to "archivr.json".');
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function execute(InputInterface $input, OutputInterface $output)
27
    {
28
        $config = $this->getConfiguration($input);
29
30
        if ($config === null)
31
        {
32
            $output->writeln('<error>This does not seem to be an archive!</error>');
33
34
            return 1;
35
        }
36
37
        return $this->executeConfigured($input, $output, $config);
38
    }
39
40
    /**
41
     * @param InputInterface $input
42
     * @param OutputInterface $output
43
     * @param Configuration $configuration
44
     * @return int
45
     */
46
    abstract protected function executeConfigured(InputInterface $input, OutputInterface $output, Configuration $configuration): int;
47
48
    /**
49
     * Tries to read in the archive configuration either from the default path or a user provided one.
50
     *
51
     * @param InputInterface $input
52
     * @return Configuration
53
     */
54
    protected function getConfiguration(InputInterface $input)
55
    {
56
        $config = null;
57
        $reader = new ConfigurationFileReader();
58
59
        if ($input->getOption('config'))
60
        {
61
            $config = $reader->getConfiguration($input->getOption('config'));
62
        }
63
        elseif (is_file('archivr.json'))
64
        {
65
            $config = $reader->getConfiguration('archivr.json');
66
        }
67
68
        return $config;
69
    }
70
}
71