Completed
Push — master ( c7b947...966759 )
by Thomas
08:09
created

AbstractKeekoCommand::sanitizeInput()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 10
nc 12
nop 1
1
<?php
2
namespace keeko\tools\command;
3
4
use keeko\framework\schema\PackageSchema;
5
use keeko\tools\helpers\IOHelper;
6
use keeko\tools\helpers\ServiceLoaderTrait;
7
use keeko\tools\services\CommandService;
8
use phootwork\lang\Text;
9
use Psr\Log\LogLevel;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Logger\ConsoleLogger;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Application;
17
18
abstract class AbstractKeekoCommand extends Command {
19
20
	use ServiceLoaderTrait;
21
	
22
	/** @var PackageSchema */
23
	protected $package;
24
25
	public function __construct($name = null) {
26
		parent::__construct($name);
27
	}
28
29
	/* (non-PHPdoc)
30
	 * @see \Symfony\Component\Console\Command\Command::initialize()
31
	 */
32
	protected function initialize(InputInterface $input, OutputInterface $output) {
33
		// io
34
		$io = new IOHelper();
35
		$io->setInput($input);
36
		$io->setOutput($output);
37
		$this->getHelperSet()->set($io);
38
		
39
		// logger
40
		$logger = new ConsoleLogger($output, [
41
			LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE,
42
			LogLevel::INFO   => OutputInterface::VERBOSITY_VERBOSE,
43
		]);
44
	
45
		// services
46
		$service = new CommandService($this, $logger);
47
		$this->loadServices($service);
48
		$this->package = $service->getPackageService()->getPackage();
49
	}
50
51
	/**
52
	 * @return CommandService
53
	 */
54
	public function getService() {
55
		// this is BULLSHIT!!
56
		// this should never be public
57
		// this is only because of the UI classes, some need access to the model service
58
		// see https://github.com/keeko/tools/issues/5 how this could be made more useful
59
		return $this->service;
60
	}
61
62
63
	protected function configure() {
64
		$this->configureGlobalOptions();
65
	}
66
67
	protected function configureGenerateOptions() {
68
		$this
69
			->addOption(
70
				'schema',
71
				's',
72
				InputOption::VALUE_OPTIONAL,
73
				'Path to the database schema (if ommited, res/database/schema.xml is used)',
74
				null
75
			)
76
			->addOption(
77
				'force',
78
				'f',
79
				InputOption::VALUE_NONE,
80
				'Forces to owerwrite'
81
			)
82
		;
83
	}
84
	
85
	protected function configureGlobalOptions() {
86
		$this
87
			->addOption(
88
				'workdir',
89
				'w',
90
				InputOption::VALUE_OPTIONAL,
91
				'Specify the working directory (if ommited, current working directory is used)',
92
				null
93
			)
94
		;
95
	}
96
	
97
	protected function runCommand($name, array $input = [], Application $app = null) {
98
		// return whether command has already executed
99
		if ($app === null) {
100
			$app = $this->getApplication();
101
		}
102
		$cmd = $app->find($name);
0 ignored issues
show
Bug introduced by
It seems like $app is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
103
104
		$input = new ArrayInput($this->sanitizeInput($input));
105
		$input->setInteractive(false);
106
		
107
		$cmd->run($input, $this->io->getOutput());
108
	}
109
	
110
	/**
111
	 * Prepares input as used for running a command
112
	 * 
113
	 * @param array $input
114
	 * @return array
115
	 */
116
	private function sanitizeInput(array $input) {
117
		// add verbose level
118
		if (!isset($input['--verbose'])) {
119
			$input['--verbose'] = $this->io->getInput()->getOption('verbose');
120
		}
121
122
		// check if at least one argument is present and if not add a blank one
123
		$hasArgs = false;
124
		foreach (array_keys($input) as $key) {
125
			if (!Text::create($key)->startsWith('--')) {
126
				$hasArgs = true;
127
			}
128
		}
129
		if (!$hasArgs) {
130
			$input[] = '';
131
		}
132
	
133
		return $input;
134
	}
135
136
}