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

AbstractCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 0
loc 93
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A execute() 0 18 2
A executeConfigured() 0 4 1
A setUpIO() 0 6 1
A getContainer() 0 8 1
A getConfiguration() 0 11 3
1
<?php
2
3
namespace Storeman\Cli\Command;
4
5
use Storeman\Cli\ConfigurationFileReader;
6
use Storeman\Cli\ConflictHandler\ConsolePromptConflictHandler;
7
use Storeman\Cli\ConsoleStyle;
8
use Storeman\Configuration;
9
use Storeman\Container;
10
use Storeman\Storeman;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * Abstract class which provides a storeman instance to the concrete command which is already configured adequately
18
 * to run in a CLI environment.
19
 */
20
abstract class AbstractCommand extends Command
21
{
22
    /**
23
     * @var InputInterface
24
     */
25
    protected $input;
26
27
    /**
28
     * @var OutputInterface
29
     */
30
    protected $output;
31
32
    /**
33
     * @var ConsoleStyle
34
     */
35
    protected $consoleStyle;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function configure()
41
    {
42
        parent::configure();
43
44
        $this->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Path to configuration file. Defaults to "./storeman.json".', './storeman.json');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $this->setUpIO($input, $output);
53
54
        $container = $this->getContainer();
55
        $config = $this->getConfiguration($input);
56
57
        if ($config === null)
58
        {
59
            $output->writeln('<error>This does not seem to be an archive!</error>');
60
61
            return 1;
62
        }
63
64
        $storeman = new Storeman($config, $container);
65
66
        return $this->executeConfigured($input, $output, $storeman);
67
    }
68
69
    protected function executeConfigured(InputInterface $input, OutputInterface $output, Storeman $storeman): int
70
    {
71
        throw new \LogicException(sprintf('Either %s::execute() or %s::executeConfigured() has to be implemented.', __CLASS__, __CLASS__));
72
    }
73
74
    protected function setUpIO(InputInterface $input, OutputInterface $output): void
75
    {
76
        $this->input = $input;
77
        $this->output = $output;
78
        $this->consoleStyle = new ConsoleStyle($input, $output);
79
    }
80
81
    /**
82
     * Builds and returns container to be used in a CLI context.
83
     *
84
     * @return Container
85
     */
86
    protected function getContainer(): Container
87
    {
88
        $container = new Container();
89
90
        $container->addConflictHandler('consolePrompt', ConsolePromptConflictHandler::class)->withArgument($this->consoleStyle);
91
92
        return $container;
93
    }
94
95
    /**
96
     * Tries to read in the archive configuration either from the default path or a user provided one.
97
     *
98
     * @param InputInterface $input
99
     * @return Configuration
100
     */
101
    protected function getConfiguration(InputInterface $input): ?Configuration
102
    {
103
        $configFilePath = $input->getOption('config');
104
105
        if ($configFilePath && is_file($configFilePath))
106
        {
107
            return (new ConfigurationFileReader())->getConfiguration($configFilePath);
108
        }
109
110
        return null;
111
    }
112
}
113