Failed Conditions
Push — master ( 5d2ea6...f862f5 )
by Alexander
12:15
created

AbstractCommand::getInputFromCompletionContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Code-Insight library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/code-insight
9
 */
10
11
namespace ConsoleHelpers\CodeInsight\Command;
12
13
14
use ConsoleHelpers\ConsoleKit\Command\AbstractCommand as BaseCommand;
15
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
16
use Symfony\Component\Console\Input\ArgvInput;
17
use Symfony\Component\Console\Input\InputInterface;
18
19
/**
20
 * Base command class.
21
 */
22
abstract class AbstractCommand extends BaseCommand
23
{
24
25
	/**
26
	 * Prepare dependencies.
27
	 *
28
	 * @return void
29
	 */
30
	protected function prepareDependencies()
31
	{
32
		$container = $this->getContainer();
0 ignored issues
show
Unused Code introduced by
The assignment to $container is dead and can be removed.
Loading history...
33
	}
34
35
	/**
36
	 * Returns input from completion context.
37
	 *
38
	 * @param CompletionContext $context Completion context.
39
	 *
40
	 * @return InputInterface
41
	 */
42
	protected function getInputFromCompletionContext(CompletionContext $context)
43
	{
44
		$words = $context->getWords();
45
		array_splice($words, 1, 1); // Remove the command name.
46
47
		return new ArgvInput($words, $this->getDefinition());
48
	}
49
50
	/**
51
	 * Returns and validates path.
52
	 *
53
	 * @param string $raw_path Raw path.
54
	 *
55
	 * @return string
56
	 * @throws \InvalidArgumentException When path isn't valid.
57
	 */
58
	protected function getPath($raw_path)
59
	{
60
		if ( !$raw_path ) {
61
			return '';
62
		}
63
64
		$path = realpath($raw_path);
65
66
		if ( !$path || !file_exists($path) || !is_dir($path) ) {
67
			throw new \InvalidArgumentException('The "' . $raw_path . '" path is invalid.');
68
		}
69
70
		return $path;
71
	}
72
73
}
74