ParamsValidator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 101
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validateInputParams() 0 11 1
B validateInputDirectory() 0 34 6
A validateIndexFile() 0 9 2
B validateAuthCredentials() 0 11 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ublaboo\Anabelle\Console\Utils;
6
7
use Ublaboo\Anabelle\Console\Utils\Exception\ParamsValidatorException;
8
use Ublaboo\Anabelle\Http\AuthCredentials;
9
10
final class ParamsValidator
11
{
12
13
	/**
14
	 * @var string
15
	 */
16
	private $binDir;
17
18
19
	public function __construct(string $binDir)
20
	{
21
		$this->binDir = $binDir;
22
	}
23
24
25
	/**
26
	 * Validate directory structure (== check for <directory>/index.md)
27
	 *
28
	 * @throws ParamsValidatorException
29
	 */
30
	public function validateInputParams(
31
		string $inputDirectory,
32
		string $outputDirectory,
33
		AuthCredentials $authCredentials,
34
		bool $overwriteOutputDir
35
	): void
36
	{
37
		$this->validateInputDirectory($inputDirectory, $outputDirectory, $overwriteOutputDir);
38
		$this->validateIndexFile($inputDirectory);
39
		$this->validateAuthCredentials($authCredentials);
40
	}
41
42
43
	/**
44
	 * @throws ParamsValidatorException
45
	 */
46
	private function validateInputDirectory(
47
		string $inputDirectory,
48
		string $outputDirectory,
49
		bool $overwriteOutputDir
50
	): void
51
	{
52
		/**
53
		 * Absolute path?
54
		 */
55
		if (!file_exists($inputDirectory)) {
56
			/**
57
			 * Relative to the bin file?
58
			 */
59
			$inputDirectory = $this->binDir . '/' . $inputDirectory;
60
61
			if (!file_exists($inputDirectory)) {
62
				throw new ParamsValidatorException('Input documentation directory does not exist');
63
			}
64
		}
65
66
		/**
67
		 * Validate $this->inputDirectory is a directory
68
		 */
69
		if (!is_dir($inputDirectory)) {
70
			throw new ParamsValidatorException('Given path is not a directory');
71
		}
72
73
		if (file_exists($outputDirectory) && !$overwriteOutputDir) {
74
			throw new ParamsValidatorException(
75
				"Output directory path already exists."
76
				. " Delete it or use option [-o] as for \"overwrite\" output directory"
77
			);
78
		}
79
	}
80
81
82
	/**
83
	 * @throws ParamsValidatorException
84
	 */
85
	private function validateIndexFile(string $inputDirectory): void
86
	{
87
		/**
88
		 * Validate existence of <directory>/index.md
89
		 */
90
		if (!file_exists($inputDirectory . '/index.md')) {
91
			throw new ParamsValidatorException("Missing file {$inputDirectory}/index.md");
92
		}
93
	}
94
95
96
	/**
97
	 * @throws ParamsValidatorException
98
	 */
99
	private function validateAuthCredentials(AuthCredentials $authCredentials): void
100
	{
101
		/**
102
		 * Validate HTTP AUTH
103
		 */
104
		if (empty($authCredentials->getPass()) && !empty($authCredentials->getUser())) {
105
			throw new ParamsValidatorException("Please set --httpAuthPass [-p]");
106
		} elseif (!empty($authCredentials->getPass()) && empty($authCredentials->getUser())) {
107
			throw new ParamsValidatorException("Please set --httpAuthUser [-u]");
108
		}
109
	}
110
}
111