GenerateDocuCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ublaboo\Anabelle\Console;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\FormatterHelper;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Ublaboo\Anabelle\Console\Utils\Exception\ParamsValidatorException;
14
use Ublaboo\Anabelle\Console\Utils\Logger;
15
use Ublaboo\Anabelle\Console\Utils\ParamsValidator;
16
use Ublaboo\Anabelle\Generator\DocuGenerator;
17
use Ublaboo\Anabelle\Generator\Exception\DocuFileGeneratorException;
18
use Ublaboo\Anabelle\Generator\Exception\DocuGeneratorException;
19
use Ublaboo\Anabelle\Http\AuthCredentials;
20
21
final class GenerateDocuCommand extends Command
22
{
23
24
	/**
25
	 * @var string
26
	 */
27
	private $binDir;
28
29
	/**
30
	 * @var ParamsValidator
31
	 */
32
	private $paramsValidator;
33
34
	/**
35
	 * @var string
36
	 */
37
	private $inputDirectory;
38
39
	/**
40
	 * @var string
41
	 */
42
	private $outputDirectory;
43
44
	/**
45
	 * @var AuthCredentials
46
	 */
47
	private $authCredentials;
48
49
	/**
50
	 * @var bool
51
	 */
52
	private $overwriteOutputDir;
53
54
	/**
55
	 * @var Logger
56
	 */
57
	private $logger;
58
59
60
	public function __construct(string $binDir)
61
	{
62
		parent::__construct();
63
64
		$this->paramsValidator = new ParamsValidator($binDir);
65
		$this->binDir = $binDir;
66
	}
67
68
69
	protected function configure(): void
70
	{
71
		$this->setName('anabelle')
72
			->setDescription('Generates a documentation from target directory')
73
			->setHelp($this->getDescription());
74
75
		$this->addArgument(
76
			'inputDirectory',
77
			InputArgument::REQUIRED,
78
			'Input documentation directory'
79
		);
80
81
		$this->addArgument(
82
			'outputDirectory',
83
			InputArgument::REQUIRED,
84
			'Output documentation directory'
85
		);
86
87
		$this->addOption(
88
			'httpAuthUser',
89
			'-u',
90
			InputOption::VALUE_OPTIONAL,
91
			'Should be there any HTTP authentication?'
92
		);
93
94
		$this->addOption(
95
			'httpAuthPass',
96
			'-p',
97
			InputOption::VALUE_OPTIONAL,
98
			'Should be there any HTTP authentication?'
99
		);
100
101
		$this->addOption(
102
			'overwriteOutputDir',
103
			'-o',
104
			InputOption::VALUE_NONE,
105
			'Should be the output directory overwritten with ne documentation?'
106
		);
107
	}
108
109
110
	public function initialize(InputInterface $input, OutputInterface $output): void
111
	{
112
		$input->validate();
113
114
		$this->inputDirectory = $input->getArgument('inputDirectory');
115
		$this->outputDirectory = $input->getArgument('outputDirectory');
116
		$this->authCredentials = new AuthCredentials(
117
			$input->getOption('httpAuthUser'),
118
			$input->getOption('httpAuthPass')
119
		);
120
		$this->overwriteOutputDir = $input->getOption('overwriteOutputDir');
121
		$this->logger = new Logger($output);
122
123
		/**
124
		 * Validate input params (documentation directory structure)
125
		 */
126
		try {
127
			$this->paramsValidator->validateInputParams(
128
				$this->inputDirectory,
129
				$this->outputDirectory,
130
				$this->authCredentials,
131
				$this->overwriteOutputDir
132
			);
133
		} catch (ParamsValidatorException $e) {
134
			$this->printError($output, $e->getMessage());
135
			exit(1);
136
		}
137
	}
138
139
140
	protected function execute(InputInterface $input, OutputInterface $output): void
141
	{
142
		$docuGenerator = new DocuGenerator(
143
			$this->inputDirectory,
144
			$this->outputDirectory,
145
			$this->authCredentials,
146
			$this->logger
147
		);
148
149
		try {
150
			$docuGenerator->run();
151
		} catch (DocuGeneratorException $e) {
152
			$this->printError($output, $e->getMessage());
153
			exit(1);
154
		} catch (DocuFileGeneratorException $e) {
155
			$this->printError($output, $e->getMessage());
156
			exit(1);
157
		}
158
	}
159
160
161
	private function printError(OutputInterface $output, string $message): void
162
	{
163
		$formatter = $this->getHelper('formatter');
164
165
		if ($formatter instanceof FormatterHelper) {
166
			$block = $formatter->formatBlock($message, 'error', true);
167
168
			$output->writeln("\n{$block}\n");
169
		}
170
	}
171
}
172