Completed
Push — master ( 10f95b...bf15d4 )
by Michael
09:02
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 0
Metric Value
dl 0
loc 9
ccs 0
cts 4
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Joomla! Statistics Server
4
 *
5
 * @copyright  Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
6
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
7
 */
8
9
namespace Joomla\StatsServer;
10
11
use Joomla\DI\{
12
	ContainerAwareInterface, ContainerAwareTrait
13
};
14
15
/**
16
 * CLI Console
17
 *
18
 * @since  1.0
19
 */
20
class Console implements ContainerAwareInterface
21
{
22
	use ContainerAwareTrait;
23
24
	/**
25
	 * Array of available command objects
26
	 *
27
	 * @var    CommandInterface[]
28
	 * @since  1.0
29
	 */
30
	private $commands = [];
31
32
	/**
33
	 * Get the available commands.
34
	 *
35
	 * @return  CommandInterface[]
36
	 *
37
	 * @since   1.0
38
	 * @throws  \RuntimeException
39
	 */
40
	public function getCommands() : array
41
	{
42
		if (empty($this->commands))
43
		{
44
			$this->commands = $this->loadCommands();
45
		}
46
47
		return $this->commands;
48
	}
49
50
	/**
51
	 * Load the application's commands
52
	 *
53
	 * @return  CommandInterface[]
54
	 *
55
	 * @since   1.0
56
	 */
57
	private function loadCommands() : array
58
	{
59
		$commands = [];
60
61
		/** @var \DirectoryIterator $fileInfo */
62
		foreach (new \DirectoryIterator(__DIR__ . '/Commands') as $fileInfo)
63
		{
64
			if ($fileInfo->isDot())
65
			{
66
				continue;
67
			}
68
69
			if ($fileInfo->isDir())
70
			{
71
				$namespace = $fileInfo->getFilename();
72
73
				/** @var \DirectoryIterator $subFileInfo */
74
				foreach (new \DirectoryIterator($fileInfo->getPathname()) as $subFileInfo)
75
				{
76
					if ($subFileInfo->isDot() || !$subFileInfo->isFile())
77
					{
78
						continue;
79
					}
80
81
					$command   = $subFileInfo->getBasename('.php');
82
					$className = __NAMESPACE__ . "\\Commands\\$namespace\\$command";
83
84
					if (!class_exists($className))
85
					{
86
						throw new \RuntimeException(sprintf('Required class "%s" not found.', $className));
87
					}
88
89
					// If the class isn't instantiable, it isn't a valid command
90 View Code Duplication
					if ((new \ReflectionClass($className))->isInstantiable())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
					{
92
						$commands[strtolower("$namespace:" . str_replace('Command', '', $command))] = $this->getContainer()->get($className);
93
					}
94
				}
95
			}
96
			else
97
			{
98
				$command   = $fileInfo->getBasename('.php');
99
				$className = __NAMESPACE__ . "\\Commands\\$command";
100
101
				if (!class_exists($className))
102
				{
103
					throw new \RuntimeException(sprintf('Required class "%s" not found.', $className));
104
				}
105
106
				// If the class isn't instantiable, it isn't a valid command
107 View Code Duplication
				if ((new \ReflectionClass($className))->isInstantiable())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
				{
109
					$commands[strtolower(str_replace('Command', '', $command))] = $this->getContainer()->get($className);
110
				}
111
			}
112
		}
113
114
		return $commands;
115
	}
116
}
117