Completed
Push — master ( 84255c...a7d7e4 )
by Thomas
14:38
created

InstallerApplication::installKeeko()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 44
rs 8.8571
cc 1
eloc 25
nc 1
nop 2
1
<?php
2
namespace keeko\core\installer;
3
4
use Composer\IO\IOInterface;
5
use Composer\IO\NullIO;
6
use keeko\core\model\Application;
7
use keeko\core\model\ApplicationQuery;
8
use keeko\core\model\ApplicationUri;
9
use keeko\core\model\CountryQuery;
10
use keeko\core\model\Group;
11
use keeko\core\model\LanguageQuery;
12
use keeko\core\model\Localization;
13
use keeko\core\model\LocalizationQuery;
14
use keeko\core\model\Map\UserTableMap;
15
use keeko\core\model\Preference;
16
use keeko\core\model\User;
17
use keeko\core\model\UserQuery;
18
use keeko\core\package\AbstractApplication;
19
use keeko\core\package\ModuleManager;
20
use keeko\core\package\PackageManager;
21
use keeko\core\preferences\SystemPreferences;
22
use keeko\core\service\ServiceContainer;
23
use Propel\Runtime\Propel;
24
use Symfony\Component\HttpFoundation\Request;
25
26
class InstallerApplication extends AbstractApplication {
27
	
28
	const DEFAULT_LOCALE = 'en-GB';
29
30
	/** @var IOInterface */
31
	private $io;
32
	
33
	/** @var PackageManager */
34
	private $packageManager;
35
	
36
	/** @var AppInstaller */
37
	private $appInstaller;
38
	
39
	/** @var ModuleInstaller */
40
	private $moduleInstaller;
41
	
42
	/** @var ModuleManager */
43
	private $moduleManager;
44
45
	public function __construct(Application $model, ServiceContainer $service, IOInterface $io = null) {
46
		parent::__construct($model, $service);
47
		$this->io = $io ?: new NullIO();
48
	}
49
50
	private function initialize() {
51
		$this->packageManager = $this->service->getPackageManager();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52
		$this->appInstaller = new AppInstaller($this->service);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
53
		$this->moduleInstaller = new ModuleInstaller($this->service);
0 ignored issues
show
Unused Code introduced by
The call to ModuleInstaller::__construct() has too many arguments starting with $this->service.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
		$this->moduleManager = $this->service->getModuleManager();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55
	}
56
57
	public function install($rootUrl, $locale = self::DEFAULT_LOCALE) {
58
		if (!KEEKO_DATABASE_LOADED) {
59
			throw new \Exception('Cannot install keeko - no database defined');
60
		}
61
62
		$this->io->write('Install Log:');
63
64
		$this->installDatabase();
65
		$this->initialize();
66
		$this->installGroupsAndUsers();
67
		$this->installKeeko($rootUrl, $locale);
68
	}
69
	
70
	/**
71
	 * Runs the main setup routine
72
	 */
73
	public function run(Request $request) {
74
		$uri = $request->getUri();
0 ignored issues
show
Unused Code introduced by
$uri is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
		
76
	}
77
	
78
	/**
79
	 * Writes the database config
80
	 *
81
	 * @param string $host
82
	 * @param string $database
83
	 * @param string $user
84
	 * @param string $password
85
	 */
86
	public function writeConfig($host, $database, $user, $password) {
0 ignored issues
show
Unused Code introduced by
The parameter $host is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $database is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $password is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
		
88
	}
89
90
	/**
91
	 *
92
	 * @param string $locale
93
	 * @return Localization
94
	 */
95
	private function getLocalization($locale) {
96
		list($langCode, $countryCode) = sscanf($locale, '%2s-%s');
97
		
98
		$lang = LanguageQuery::create()->findOneByAlpha2($langCode);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
99
		$country = CountryQuery::create()->findOneByAlpha2($countryCode);
100
		
101
		$local = LocalizationQuery::create()
102
			->filterByLanguage($lang)
103
			->filterByCountry($country)
104
			->findOne();
105
		
106
		// if no locale found -> create one
107
		if ($local === null) {
108
			$local = new Localization();
109
			$local->setLanguage($lang); // de: 1546
110
			$local->setCountry($country); // ger: 276
111
			$local->setIsDefault(true);
112
			$local->save();
113
		}
114
115
		return $local;
116
	}
117
	
118
	private function installGroupsAndUsers() {
119
		$guestGroup = new Group();
120
		$guestGroup->setName('Guest');
121
		$guestGroup->setIsGuest(true);
122
		$guestGroup->save();
123
		
124
		$userGroup = new Group();
125
		$userGroup->setName('Users');
126
		$userGroup->setIsDefault(true);
127
		$userGroup->save();
128
		
129
		$adminGroup = new Group();
130
		$adminGroup->setName('Administrators');
131
		$adminGroup->save();
132
133
		
134
		$con = Propel::getConnection();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
135
		$adapter = Propel::getAdapter();
136
		
137
		// guest
138
		$guest = new User();
139
		$guest->setDisplayName('Guest');
140
		$guest->save();
141
		
142
		$stmt = $con->prepare(sprintf('UPDATE %s SET id = -1 WHERE ID = 1', $adapter->quoteIdentifierTable(UserTableMap::TABLE_NAME)));
143
		$stmt->execute();
144
		
145
		// root
146
		$root = new User();
147
		$root->setDisplayName('root');
148
		$root->setLoginName('root');
149
		$root->setPassword(password_hash('root', PASSWORD_BCRYPT));
150
		$root->save();
151
152
		$stmt = $con->prepare(sprintf('UPDATE %s SET id = 0 WHERE ID = 2', $adapter->quoteIdentifierTable(UserTableMap::TABLE_NAME)));
153
		$stmt->execute();
154
		
155
		$root = UserQuery::create()->findOneById(0);
156
		$root->addGroup($userGroup);
157
		$root->addGroup($adminGroup);
158
		$root->save();
159
		
160
		// @TODO: Cross-SQL-Server routine wanted!!
161
		$stmt = $con->prepare(sprintf('ALTER TABLE %s AUTO_INCREMENT = 1', $adapter->quoteIdentifierTable(UserTableMap::TABLE_NAME)));
162
		$stmt->execute();
163
		
164
	}
165
166
	private function installKeeko($rootUrl, $locale = self::DEFAULT_LOCALE) {
167
		// 1) apps
168
		
169
		// api
170
		$apiUrl = $rootUrl . '/api/';
171
		$this->installApp('keeko/api-app');
172
		$this->setupApp('keeko/api-app', $apiUrl, $locale);
173
		
174
		// developer
175
		$this->installApp('keeko/developer-app');
176
		$this->setupApp('keeko/developer-app', $rootUrl . '/developer/', $locale);
177
		
178
		// website
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
179
// 		$websiteApp = $this->installApp('keeko/website-app');
180
		
181
		
182
		// 2) preferences
183
		$core = $this->service->getPackageManager()->getComposerPackage('keeko/core');
184
		
185
		$pref = new Preference();
186
		$pref->setKey(SystemPreferences::VERSION);
187
		$pref->setValue($core->getPrettyVersion());
188
		$pref->save();
189
		
190
		$pref = new Preference();
191
		$pref->setKey(SystemPreferences::PLATTFORM_NAME);
192
		$pref->setValue('Keeko');
193
		$pref->save();
194
		
195
		$pref = new Preference();
196
		$pref->setKey(SystemPreferences::API_URL);
197
		$pref->setValue($apiUrl);
198
		$pref->save();
199
200
		// 3) modules
201
		$this->installModule('keeko/user');
202
		$this->activateModule('keeko/user');
203
		
204
		$this->installModule('keeko/group');
205
		$this->activateModule('keeko/group');
206
		
207
		$this->installModule('keeko/auth');
208
		$this->activateModule('keeko/auth');
209
	}
210
211
	public function installApp($packageName) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
212
		return $this->appInstaller->install($this->io, $packageName);
213
	}
214
	
215
	public function setupApp($packageName, $uri, $locale = self::DEFAULT_LOCALE) {
216
		$this->io->write(sprintf('[Keeko] Setup App %s at %s', $packageName, $uri));
217
		$app = ApplicationQuery::create()->findOneByName($packageName);
218
		
219
		if ($app === null) {
220
			throw new \Exception(sprintf('Application (%s) not found', $packageName));
221
		}
222
		
223
		$comps = parse_url($uri);
224
		
225
		$uri = new ApplicationUri();
226
		$uri->setApplication($app);
227
		$uri->setLocalization($this->getLocalization($locale));
228
		$uri->setHttphost($comps['host']);
229
		$uri->setBasepath($comps['path']);
230
		$uri->setSecure($comps['scheme'] == 'https');
231
		$uri->save();
232
	}
233
	
234
	public function installModule($packageName) {
235
		$this->moduleInstaller->install($this->io, $packageName);
236
	}
237
238
	public function activateModule($packageName) {
239
		$this->moduleInstaller->activate($this->io, $packageName);
240
	}
241
242
	private function installDatabase() {
243
		$files = [
244
			'sql/keeko.sql',
245
			'data/static-data.sql'
246
		];
247
		$con = Propel::getConnection();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
248
		
249
		foreach ($files as $file) {
250
			$path = KEEKO_PATH . '/packages/keeko/core/database/' . $file;
251
			
252
			if (file_exists($path)) {
253
				$sql = file_get_contents($path);
254
				
255
				try {
256
					$stmt = $con->prepare($sql);
257
					$stmt->execute();
258
				} catch (\Exception $e) {
259
					echo $e->getMessage();
260
				}
261
			}
262
		}
263
	}
264
}