Passed
Push — main ( b13b1c...30676d )
by Emlyn
12:24
created

AbstractCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 30
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 36
ccs 35
cts 35
cp 1
crap 1
rs 9.44
1
<?php
2
/**
3
 * @category Library
4
 * @license MIT http://opensource.org/licenses/MIT
5
 * @link https://github.com/emlynwest/changelog
6
 */
7
8
namespace ChangeLog\Console;
9
10
use ChangeLog\ChangeLog;
11
use ChangeLog\GenericFactory;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Shared common logic for commands.
19
 */
20
abstract class AbstractCommand extends Command
21
{
22
	const DEFAULT_FACTORY = 'default';
23
24
	/**
25
	 * @var array
26
	 */
27
	protected $config;
28
29
	/**
30
	 * @var ChangeLog
31
	 */
32
	protected $changeLog;
33
34 11
	protected function configure()
35
	{
36 11
		$this->addOption(
37 11
			'config',
38 11
			null,
39 11
			InputOption::VALUE_OPTIONAL,
40 11
			'Location of config file.',
41 11
			'changelog.config.php'
42 11
		);
43 11
		$this->addOption(
44 11
			'input',
45 11
			null,
46 11
			InputOption::VALUE_OPTIONAL,
47 11
			'Config to use for input processor',
48 11
			'default'
49 11
		);
50 11
		$this->addOption(
51 11
			'parser',
52 11
			null,
53 11
			InputOption::VALUE_OPTIONAL,
54 11
			'Config to use for parser processor',
55 11
			'default'
56 11
		);
57 11
		$this->addOption(
58 11
			'renderer',
59 11
			null,
60 11
			InputOption::VALUE_OPTIONAL,
61 11
			'Config to use for renderer processor',
62 11
			'default'
63 11
		);
64 11
		$this->addOption(
65 11
			'output',
66 11
			null,
67 11
			InputOption::VALUE_OPTIONAL,
68 11
			'Config to use for output processor',
69 11
			'default'
70 11
		);
71
	}
72
73 11
	public function execute(InputInterface $input, OutputInterface $output): int
74
	{
75 11
		$configLocation = $input->getOption('config');
76
77 11
		if ( ! is_file($configLocation) && ! is_readable($configLocation))
78
		{
79 1
			throw new ConfigNotFoundException('Unable to open config file: ' . $configLocation);
80
		}
81
82 10
		$this->config = require $configLocation;
83
84
		// Construct a changelog object to manipulate
85 10
		$this->changeLog = new ChangeLog();
86
87 10
		$this->setInput($input->getOption('input'));
88 10
		$this->setParser($input->getOption('parser'));
89 10
		$this->setRenderer($input->getOption('renderer'));
90 10
		$this->setOutput($input->getOption('output'));
91
92 10
		return Command::SUCCESS;
93
	}
94
95 10
	protected function setInput($factoryName)
96
	{
97 10
		$factory = new GenericFactory('\ChangeLog\IO\\');
98 10
		$instance = $factory->getInstance(
99 10
			$this->config['input'][$factoryName]['strategy'],
100 10
			$this->config['input'][$factoryName]['config']
101 10
		);
102 10
		$this->changeLog->setInput($instance);
103
	}
104
105 10
	protected function setParser($factoryName)
106
	{
107 10
		$factory = new GenericFactory('\ChangeLog\Parser\\');
108 10
		$instance = $factory->getInstance($this->config['parser'][$factoryName]['strategy']);
109 10
		$this->changeLog->setParser($instance);
110
	}
111
112 10
	protected function setRenderer($factoryName)
113
	{
114 10
		$factory = new GenericFactory('\ChangeLog\Renderer\\');
115 10
		$instance = $factory->getInstance($this->config['renderer'][$factoryName]['strategy']);
116 10
		$this->changeLog->setRenderer($instance);
117
	}
118
119 10
	protected function setOutput($factoryName)
120
	{
121 10
		$factory = new GenericFactory('\ChangeLog\IO\\');
122 10
		$instance = $factory->getInstance(
123 10
			$this->config['output'][$factoryName]['strategy'],
124 10
			$this->config['output'][$factoryName]['config']
125 10
		);
126 10
		$this->changeLog->setOutput($instance);
127
	}
128
129
}
130