Completed
Push — master ( af5658...fe61ae )
by Thomas
06:08
created

InitUI::show()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 39
rs 8.8571
cc 1
eloc 27
nc 1
nop 0
1
<?php
2
namespace keeko\tools\ui;
3
4
use keeko\tools\helpers\InitCommandHelperTrait;
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Question\Question;
7
8
class InitUI extends AbstractUI {
9
	
10
	use InitCommandHelperTrait;
11
	
12
	public function show() {
13
		$input = $this->io->getInput();
14
		$output = $this->io->getOutput();
15
		$formatter = $this->command->getHelperSet()->get('formatter');
16
		
17
		// send welcome
18
		$output->writeln([
19
			'',
20
			$formatter->formatBlock('Welcome to the Keeko initializer', 'bg=blue;fg=white', true),
21
			''
22
		]);
23
		$output->writeln([
24
			'',
25
			'This command will guide you through creating your Keeko composer package.',
26
			'',
27
		]);
28
		
29
		// asking for a options
30
		$type = $this->getType();
31
		$name = $this->getName();
32
		$package = $this->getService()->getPackageService()->getPackage();
33
		$package->setFullName($name);
34
		
35
		$input->setOption('name', $name);
36
		$input->setOption('description', $this->getDescription());
37
		$input->setOption('author', $this->getAuthor());
38
		$input->setOption('type', $type);
39
		$input->setOption('license', $this->getLicense());
40
41
		// KEEKO values
42
		$output->writeln([
43
			'',
44
			'Information for Keeko ' . ucfirst($type),
45
			''
46
		]);
47
		
48
		$input->setOption('title', $this->getTitle());
49
		$input->setOption('classname', $this->getClass());
50
	}
51
	
52
	private function getName() {
0 ignored issues
show
Coding Style introduced by
getName uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
53
		$input = $this->io->getInput();
54
		$force = $input->getOption('force');
55
		$package = $this->getService()->getPackageService()->getPackage();
56
		
57
		$name = $this->getPackageName();
58
		$askName = $name === null;
59
		if ($name === null) {
60
			$git = $this->getGitConfig();
61
			$cwd = realpath('.');
62
			$name = basename($cwd);
63
			$name = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $name);
64
			$name = strtolower($name);
65
			$localName = $package->getFullName();
66
			if (!empty($localName)) {
67
				$name = $package->getFullName();
68
			} else if (isset($git['github.user'])) {
69
				$name = $git['github.user'] . '/' . $name;
70
			} elseif (!empty($_SERVER['USERNAME'])) {
71
				$name = $_SERVER['USERNAME'] . '/' . $name;
72
			} elseif (get_current_user()) {
73
				$name = get_current_user() . '/' . $name;
74
			} else {
75
				// package names must be in the format foo/bar
76
				$name = $name . '/' . $name;
77
			}
78
		} else {
79
			$this->validateName($name);
80
		}
81
		
82
		// asking for the name
83
		if ($askName || $force) {
84
			$name = $this->askQuestion(new Question('Package name (<vendor>/<name>)', $name));
85
			$this->validateName($name);
86
		}
87
		
88
		return $name;
89
	}
90
	
91
	private function getDescription() {
92
		$force = $this->io->getInput()->getOption('force');
93
		$desc = $this->getPackageDescription();
94
		if ($desc === null || $force) {
95
			$desc = $this->askQuestion(new Question('Description', $desc));
96
		}
97
		return $desc;
98
	}
99
	
100
	private function getAuthor() {
101
		$input = $this->io->getInput();
102
		$force = $input->getOption('force');
103
		$package = $this->getService()->getPackageService()->getPackage();
104
		$git = $this->getGitConfig();
105
		$author = null;
106
		if ($package->getAuthors()->isEmpty() || $force) {
107
			$author = $input->getOption('author');
108
			if ($author === null && isset($git['user.name'])) {
109
				$author = $git['user.name'];
110
		
111
				if (isset($git['user.email'])) {
112
					$author = sprintf('%s <%s>', $git['user.name'], $git['user.email']);
113
				}
114
			}
115
		
116
			$author = $this->askQuestion(new Question('Author', $author));
117
		}
118
		return $author;
119
	}
120
	
121
	private function getType() {
122
		$force = $this->io->getInput()->getOption('force');
123
		$type = $this->getPackageType();
124
		if ($type === null || $force) {
125
			$types = ['module', 'app'];
126
			$question = new Question('Package type (module|app)', $type);
127
			$question->setAutocompleterValues($types);
128
			$question->setValidator(function($answer) use ($types) {
129
				if (!in_array($answer, $types)) {
130
					throw new \RuntimeException('The name of the type should be one of: ' .
131
						implode(',', $types));
132
				}
133
				return $answer;
134
			});
135
			$question->setMaxAttempts(2);
136
			$type = $this->askQuestion($question);
137
		}
138
		return $type;
139
	}
140
	
141
	private function getLicense() {
142
		$force = $this->io->getInput()->getOption('force');
143
		$license = $this->getPackageLicense();
144
		if ($license === null || $force) {
145
			$license = $this->askQuestion(new Question('License', $license));
146
		}
147
		return $license;
148
	}
149
	
150
	private function getTitle() {
151
		$force = $this->io->getInput()->getOption('force');
152
		$title = $this->getPackageTitle();
153
		if ($title === null || $force) {
154
			$title = $this->askQuestion(new Question('Title', $title));		
155
		}
156
		return $title;
157
	}
158
	
159
	private function getClass() {
160
		$force = $this->io->getInput()->getOption('force');
161
		$classname = $this->getPackageClass();
162
		if ($classname === null || $force) {
163
			$classname = $this->askQuestion(new Question('Class', $classname));
164
		}
165
		return $classname;
166
	}
167
}