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

ModuleInstaller::getGroup()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 21
Code Lines 14

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 21
loc 21
rs 7.551
ccs 0
cts 0
cp 0
cc 7
eloc 14
nc 7
nop 1
crap 56
1
<?php
2
namespace keeko\core\installer;
3
4
use Composer\IO\IOInterface;
5
use gossi\swagger\Swagger;
6
use keeko\core\events\ModuleEvent;
7
use keeko\core\model\Action;
8
use keeko\core\model\ActionQuery;
9
use keeko\core\model\Api;
10
use keeko\core\model\ApiQuery;
11
use keeko\core\model\Group;
12
use keeko\core\model\GroupQuery;
13
use keeko\core\model\Module;
14
use keeko\core\model\ModuleQuery;
15
use keeko\core\package\ModuleManager;
16
use keeko\core\schema\ModuleSchema;
17
18
class ModuleInstaller extends AbstractPackageInstaller {
19
	
20
	/** @var ModuleManager */
21
	private $manager;
22
	
23
	/** @var Group */
24
	private $guestGroup;
25
	
26
	/** @var Group */
27
	private $userGroup;
28
	
29
	/** @var Group */
30
	private $adminGroup;
31
	
32
	public function __construct() {
33
		parent::__construct();
34
		$this->manager = $this->service->getModuleManager();
35
	}
36
37
	public function install(IOInterface $io, $packageName) {
38
		$io->write('[Keeko] Install Module: ' . $packageName);
39
		
40
		$package = $this->getPackageSchema($packageName);
41
		$keeko = $package->getKeeko();
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...
42
		
43
		if ($keeko->isModule()) {
44
			$pkg = $keeko->getModule();
45
46
			// create module
47
			$model = new Module();
48
			$model->setClassName($pkg->getClass());
49
			$model->setSlug($pkg->getSlug());
50
			$this->updatePackage($model, $pkg);
51
			
52
			// run module -> install
53
			$className = $pkg->getClass();
54
			$class = new $className($model, $this->service);
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...
55
			$class->install();
56
			
57
			$this->dispatcher->dispatch(ModuleEvent::INSTALLED, new ModuleEvent($model));
58
		}
59
	}
60
61
	public function update(IOInterface $io, $packageName, $from, $to) {
62
		$io->write(sprintf('[Keeko] Update Module: %s from %s to %s', $packageName, $from, $to));
63
		
64
		// retrieve module
65
		$model = ModuleQuery::create()->findOneByName($packageName);
66
		$this->updatePackage($model, $packageName);
67
		
68
		// run module -> update
69
		$className = $model->getClass();
70
		$class = new $className($model, $this->service);
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...
71
		$class->update($from, $to);
72
		
73
		// update api and actions
74
		if ($this->manager->isActivated($packageName)) {
75
			$this->updateModule($model);
76
		}
77
		
78
		$this->dispatcher->dispatch(ModuleEvent::UPDATED, new ModuleEvent($model));
79
	}
80
81
	public function uninstall(IOInterface $io, $packageName) {
82
		$io->write('[Keeko] Uninstall Module: ' . $packageName);
83
84
		// retrieve module
85
		$model = ModuleQuery::create()->findOneByName($packageName);
86
87
		// delete if found
88
		if ($model !== null) {
89
			$model->delete();
90
			
91
			// TODO: Check if api and actions are also deleted (by the call above)
92
		}
93
94
		$this->dispatcher->dispatch(ModuleEvent::UNINSTALLED, new ModuleEvent($model));
95
	}
96
	
97
	public function activate(IOInterface $io, $packageName) {
98
		$io->write('[Keeko] Activate Module: ' . $packageName);
99
		
100
		$package = $this->service->getPackageManager()->getComposerPackage($packageName);
101
		
102
		$model = ModuleQuery::create()->findOneByName($packageName);
103
		$model->setActivatedVersion($package->getPrettyVersion());
104
		$model->save();
105
		
106
		$this->updateModule($model);
107
	}
108
	
109
	private function updateModule(Module $model) {
110
		$package = $this->service->getPackageManager()->getPackage($model->getName());
111
		$keeko = $package->getKeeko();
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...
112
113
		if ($keeko->isModule()) {
114
			$module = $keeko->getModule();
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...
115
			$actions = $this->updateActions($model, $module);
116
			$this->updateApi($model, $module, $actions);
0 ignored issues
show
Unused Code introduced by
The call to ModuleInstaller::updateApi() has too many arguments starting with $actions.

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...
117
		}
118
	}
119
	
120
	private function updateActions(Module $model, ModuleSchema $module) {
121
		$actions = [];
122
	
123
		foreach ($module->getActionNames() as $name) {
124
			$action = $module->getAction($name);
125
			$a = new Action();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
126
			$a->setName($name);
127
			$a->setModule($model);
128
			$a->setTitle($action->getTitle());
129
			$a->setDescription($action->getDescription());
130
			$a->setClassName($action->getClass());
131
				
132
			// add acl
133
			foreach ($action->getAcl() as $group) {
134
				$a->addGroup($this->getGroup($group));
135
			}
136
				
137
			$a->save();
138
			$actions[$name] = $a->getId();
139
		}
140
		
141
		// remove obsolete actions
142
		ActionQuery::create()
143
			->filterByModule($model)
144
			->where('Action.Name NOT IN ?', $module->getActionNames()->toArray())
145
			->delete();
146
147
		return $actions;
148
	}
149
	
150
	/**
151
	 * @param string $name
152
	 * @return Group
153
	 */
154 View Code Duplication
	private function getGroup($name) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
155
		switch ($name) {
156
			case 'guest':
157
				if ($this->guestGroup === null) {
158
					$this->guestGroup = GroupQuery::create()->filterByIsGuest(true)->findOne();
159
				}
160
				return $this->guestGroup;
161
	
162
			case 'user':
163
				if ($this->userGroup === null) {
164
					$this->userGroup = GroupQuery::create()->filterByIsDefault(true)->findOne();
165
				}
166
				return $this->userGroup;
167
	
168
			case 'admin':
169
				if ($this->adminGroup === null) {
170
					$this->adminGroup = GroupQuery::create()->findOneById(3);
171
				}
172
				return $this->adminGroup;
173
		}
174
	}
175
	
176
	private function updateApi(Module $model, $actions) {
0 ignored issues
show
Unused Code introduced by
The parameter $actions 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...
177
		$repo = $this->service->getResourceRepository();
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...
178
		$filename = sprintf('/packages/%s/api.json', $model->getName());
179
		if (!$repo->contains($filename)) {
180
			return;
181
		}
182
183
		// delete every api existent for the given module prior to create the new ones
184
// 		$q = ApiQuery::create()
185
// 			->useActionQuery()
186
// 				->filterByModule($model)
187
// 			->endUse();
188
		
189
// 		echo $q->toString();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
190
		
191
// 		$q->delete();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
192
	
193
		// TODO: Work out extension system before defining parent endpoints
194
		
195
// 		$swagger = new Swagger($repo->get($fileName)->getBody());
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
196
197
// 		foreach ($data['api']['apis'] as $apis) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
198
// 			$path = $apis['path'];
199
// 			foreach ($apis['operations'] as $op) {
200
// 				// fetch required params
201
// 				$required = [];
202
// 				if (isset($op['parameters'])) {
203
// 					foreach ($op['parameters'] as $param) {
204
// 						if (isset($param['paramType']) && $param['paramType'] == 'path') {
205
// 							$required[] = $param['name'];
206
// 						}
207
// 					}
208
// 				}
209
	
210
// 				// create record
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
211
// 				$fullPath = str_replace('//', '/', $base . '/' . $path);
212
// 				$api = new Api();
213
// 				$api->setMethod($op['method']);
214
// 				$api->setRoute($fullPath);
215
// 				$api->setActionId($actionMap[$op['nickname']]);
216
// 				$api->setRequiredParams(implode(',', $required));
217
// 				$api->save();
218
// 			}
219
// 		}
220
	
221
		$model->setApi(true);
222
		$model->save();
223
	}
224
	
225
	public function deactivate(IOInterface $io, $packageName) {
226
		$io->write('[Keeko] Deactivate Module: ' . $packageName);
227
		
228
		$mod = ModuleQuery::create()->filterByName($packageName)->findOne();
229
		$mod->setActivatedVersion(null);
230
		$mod->save();
231
	}
232
}