Completed
Push — master ( cd1493...0fdab2 )
by Michael
02:22
created

Console::getCommands()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Stats;
4
5
use Joomla\Application\Cli\ColorStyle;
6
use Joomla\Application\Cli\Output\Processor\ColorProcessor;
7
use Joomla\Controller\AbstractController;
8
use Joomla\DI\ContainerAwareInterface;
9
use Joomla\DI\ContainerAwareTrait;
10
11
/**
12
 * CLI Console
13
 *
14
 * @since  1.0
15
 */
16
class Console implements ContainerAwareInterface
17
{
18
	use ContainerAwareTrait;
19
20
	/**
21
	 * Array of available command objects
22
	 *
23
	 * @var    CommandInterface[]
24
	 * @since  1.0
25
	 */
26
	private $commands = [];
27
28
	/**
29
	 * Get the available commands.
30
	 *
31
	 * @return  CommandInterface[]
32
	 *
33
	 * @since   1.0
34
	 * @throws  \RuntimeException
35
	 */
36
	public function getCommands()
37
	{
38
		if (empty($this->commands))
39
		{
40
			$this->commands = $this->loadCommands();
41
		}
42
43
		return $this->commands;
44
	}
45
46
	/**
47
	 * Load the application's commands
48
	 *
49
	 * @return  CommandInterface[]
50
	 *
51
	 * @since   1.0
52
	 */
53
	private function loadCommands()
54
	{
55
		$commands = [];
56
57
		/** @var \DirectoryIterator $fileInfo */
58
		foreach (new \DirectoryIterator(__DIR__ . '/Commands') as $fileInfo)
59
		{
60
			if ($fileInfo->isDot() || !$fileInfo->isFile())
61
			{
62
				continue;
63
			}
64
65
			$command   = $fileInfo->getBasename('.php');
66
			$className = __NAMESPACE__ . "\\Commands\\$command";
67
68
			if (false == class_exists($className))
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
69
			{
70
				throw new \RuntimeException(sprintf('Required class "%s" not found.', $className));
71
			}
72
73
			$commands[strtolower(str_replace('Command', '', $command))] = $this->getContainer()->get($className);
74
		}
75
76
		return $commands;
77
	}
78
}
79