Help::execute()   C
last analyzed

Complexity

Conditions 8
Paths 42

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 8.0955

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 31
cts 35
cp 0.8857
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 27
nc 42
nop 0
crap 8.0955
1
<?php
2
3
4
namespace Genesis\Commands;
5
6
7
use Genesis\Cli;
8
use Genesis\InvalidArgumentException;
9
10
11
/**
12
 * @author Adam Bisek <[email protected]>
13
 */
14
class Help extends Command
15
{
16
17
	private $sections = [];
18
19
20 4
	public function addSection($name, $description = NULL)
21
	{
22 4
		$this->sections[$name] = [
23 4
			'description' => $description,
24 4
			'tasks' => [],
25
		];
26 4
	}
27
28
29
	/**
30
	 * @param $name
31
	 * @return bool
32
	 */
33 3
	public function hasSection($name)
34
	{
35 3
		return isset($this->sections[$name]);
36
	}
37
38
39
	/**
40
	 * @return array
41
	 */
42
	public function getSections()
43
	{
44
		return $this->sections;
45
	}
46
47
48
	/**
49
	 * @param $section
50
	 * @param array $tasks
51
	 */
52 4
	public function setSectionTasks($section, array $tasks)
53
	{
54 4
		if (!isset($this->sections[$section])) {
55
			throw new InvalidArgumentException("Section '$section' not found.");
56
		}
57 4
		$this->sections[$section]['tasks'] = $tasks;
58 4
	}
59
60
61 4
	public function execute()
62
	{
63
		// detect max width
64 4
		$minColumnWidth = 30;
65 4
		foreach ($this->sections as $sectionName => $data) {
66 4
			if (strlen($sectionName) > $minColumnWidth) {
67
				$minColumnWidth = strlen($sectionName) + 5;
68
			}
69 4
			foreach ($data['tasks'] as $taskName => $description) {
70 4
				if (strlen($taskName) > $minColumnWidth) {
71
					$minColumnWidth = strlen($taskName) + 2 + 5;
72
				}
73 4
			}
74 4
		}
75
		// empty section first
76 4
		if (isset($this->sections[''])) {
77 3
			$val = $this->sections[''];
78 3
			unset($this->sections['']);
79 3
			$this->sections = ['' => $val] + $this->sections;
80 3
		}
81
82 4
		echo Cli::getColoredString(str_repeat('-', 14), 'green') . PHP_EOL;
83 4
		echo Cli::getColoredString('HELP', 'green') . PHP_EOL;
84 4
		echo Cli::getColoredString(str_repeat('-', 14), 'green') . PHP_EOL . PHP_EOL;
85 4
		echo "Available tasks:" . PHP_EOL;
86 4
		foreach ($this->sections as $sectionName => $data) {
87 4
			echo Cli::getColoredString($sectionName, 'yellow');
88 4
			echo str_repeat(" ", $minColumnWidth - strlen($sectionName) + 2); // +2 = two spaces before taskName (below)
89 4
			echo Cli::getColoredString($data['description'], 'dark_gray');
90 4
			echo PHP_EOL;
91 4
			foreach ($data['tasks'] as $taskName => $description) {
92 4
				echo "  " . Cli::getColoredString($taskName, 'light_blue');
93 4
				echo str_repeat(" ", $minColumnWidth - strlen($taskName));
94 4
				echo Cli::getColoredString($description, 'gray');
95 4
				echo PHP_EOL;
96 4
			}
97 4
			echo PHP_EOL;
98 4
		}
99 4
	}
100
101
}