Completed
Push — master ( cb7793...7b1e0a )
by Arne
01:36
created

AbstractCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
namespace Archivr\Cli\Command;
4
5
use Archivr\Configuration;
6
use Archivr\ConfigurationFileReader;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
abstract class AbstractCommand extends Command
13
{
14
    /**
15
     * @var Configuration
16
     */
17
    protected $config;
18
19
    protected function execute(InputInterface $input, OutputInterface $output)
20
    {
21
        $this->registerFormatters($output);
22
23
        $config = $this->getConfiguration($input);
24
25
        if ($config === null)
26
        {
27
            $output->writeln(sprintf('This does not seem to be an archive!'));
28
29
            return 1;
30
        }
31
32
        return $this->executePrepared($input, $output, $config);
33
    }
34
35
    protected function executePrepared(InputInterface $input, OutputInterface $output, Configuration $configuration): int
36
    {
37
        return 0;
38
    }
39
40
    protected function getConfiguration(InputInterface $input)
41
    {
42
        if ($this->config === null)
43
        {
44
            $reader = new ConfigurationFileReader();
45
46
            if ($input->getOption('config'))
47
            {
48
                $this->config = $reader->getConfiguration($input->getOption('config'));
49
            }
50
            elseif (is_file('archivr.json'))
51
            {
52
                $this->config = $reader->getConfiguration('archivr.json');
53
            }
54
        }
55
56
        return $this->config;
57
    }
58
59
    protected function registerFormatters(OutputInterface $output)
60
    {
61
        $output->getFormatter()->setStyle('bold', new OutputFormatterStyle(null, null, ['bold']));
62
    }
63
}
64