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

AbstractModule::loadAction()   C

Complexity

Conditions 8
Paths 22

Size

Total Lines 83
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 83
rs 5.8855
cc 8
eloc 25
nc 22
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace keeko\core\package;
3
4
use keeko\core\exceptions\ModuleException;
5
use keeko\core\exceptions\PermissionDeniedException;
6
use keeko\core\model\Action;
7
use keeko\core\model\ActionQuery;
8
use keeko\core\model\Module;
9
use keeko\core\model\User;
10
use keeko\core\preferences\Preferences;
11
use keeko\core\service\ServiceContainer;
12
use keeko\core\utils\TwigTrait;
13
use keeko\core\schema\PackageSchema;
14
use keeko\core\schema\ActionSchema;
15
16
abstract class AbstractModule {
17
	
18
	use TwigTrait;
19
	
20
	/** @var Module */
21
	protected $model;
22
23
	protected $actions;
24
25
	/** @var User */
26
	protected $user;
27
	
28
	/** @var ServiceContainer */
29
	protected $service;
30
	
31
	/** @var Preferences */
32
	private $preferences;
33
	
34
	/** @var PackageSchema */
35
	protected $package;
36
37
	public function __construct(Module $module, ServiceContainer $service) {
38
		$this->model = $module;
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...
39
		$this->service = $service;
40
		
41
		$packageManager = $service->getPackageManager();
42
		$this->package = $packageManager->getPackage($module->getName());
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...
43
		
44
		$this->loadActions();
45
	}
46
	
47
	/**
48
	 * Returns the modules name
49
	 *
50
	 * @return string
51
	 */
52
	public function getName() {
53
		return $this->model->getName();
54
	}
55
	
56
	/**
57
	 * Returns the modules canonical name
58
	 *
59
	 * @return string
60
	 */
61
	public function getCanonicalName() {
62
		return str_replace('/', '.', $this->getName());
63
	}
64
	
65
	/**
66
	 * Returns the modules title
67
	 *
68
	 * @return string
69
	 */
70
	public function getTitle() {
71
		return $this->model->getTitle();
72
	}
73
	
74
	/**
75
	 * Returns the service container
76
	 *
77
	 * @return ServiceContainer
78
	 */
79
	public function getServiceContainer() {
80
		return $this->service;
81
	}
82
83
	/**
84
	 * Returns the associated module model
85
	 *
86
	 * @return Module
87
	 */
88
	public function getModel() {
89
		return $this->model;
90
	}
91
	
92
	
93
	
94
	public function getPath() {
95
		return sprintf('%s/%s/', KEEKO_PATH_MODULES, $this->model->getName());
96
	}
97
	
98
	public function getManagedFilesPath() {
99
		return sprintf('%s/managed/%s', KEEKO_PATH_FILES, $this->model->getName());
100
	}
101
	
102
	public function getManagedFilesUrl() {
103
		return sprintf('%s/files/managed/%s', $this->getServiceContainer()->getApplication()->getRootUrl(), $this->model->getName());
0 ignored issues
show
Bug introduced by
The method getApplication() does not seem to exist on object<keeko\core\service\ServiceContainer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
	}
105
	
106
	/**
107
	 * Returns the module's preferences
108
	 *
109
	 * @return Preferences
110
	 */
111
	public function getPreferences() {
112
		if ($this->preferences === null) {
113
			$this->preferences = $this->service->getPreferenceLoader()->getModulePreferences($this->model->getId());
114
		}
115
		
116
		return $this->preferences;
117
	}
118
119
	private function loadActions() {
120
		$models = [];
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...
121
		$actions = ActionQuery::create()->filterByModule($this->model)->find();
122
		foreach ($actions as $action) {
123
			$models[$action->getName()] = $action;
124
		}
125
		$keeko = $this->package->getKeeko();
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...
126
		$module = $keeko->getModule();
127
	
128
		
129
		$this->actions = [];
130
		foreach ($module->getActionNames() as $actionName) {
131
			if (isset($models[$actionName])) {
132
				$this->actions[$actionName] = [
133
					'model' => $models[$actionName],
134
					'action' => $module->getAction($actionName)
135
				];
136
			}
137
		}
138
	}
139
140
	/**
141
	 * Loads the given action
142
	 *
143
	 * @param Action|string $actionName
0 ignored issues
show
Bug introduced by
There is no parameter named $actionName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
144
	 * @param string $response the response type (e.g. html, json, ...)
145
	 * @return AbstractAction
146
	 */
147
	public function loadAction($nameOrAction, $response) {
148
		$model = null;
149
		if ($nameOrAction instanceof Action) {
150
			$model = $nameOrAction;
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...
151
			$actionName = $nameOrAction->getName();
152
		} else {
153
			$actionName = $nameOrAction;
154
		}
155
		
156
		if (!isset($this->actions[$actionName])) {
157
			throw new ModuleException(sprintf('Action (%s) not found in Module (%s)', $actionName, $this->model->getName()));
158
		}
159
		
160
		if ($model === null) {
161
			$model = $this->actions[$actionName]['model'];
162
		}
163
		
164
		/* @var $action ActionSchema */
165
		$action = $this->actions[$actionName]['action'];
166
		
167
		
168
		// check permission
169
		if (!$this->service->getFirewall()->hasActionPermission($model)) {
170
			throw new PermissionDeniedException(sprintf('Can\'t access Action (%s) in Module (%s)', $actionName, $this->model->getName()));
171
		}
172
		
173
		// check if a response is given
174
		if (!$action->hasResponse($response)) {
175
			throw new ModuleException(sprintf('No Response (%s) given for Action (%s) in Module (%s)', $response, $actionName, $this->model->getName()));
176
		}
177
		$responseClass = $action->getResponse($response);
178
		
179
		if (!class_exists($responseClass)) {
180
			throw new ModuleException(sprintf('Response (%s) not found in Module (%s)', $responseClass, $this->model->getName()));
181
		}
182
		$response = new $responseClass($this, $response);
183
		
184
		// gets the action class
185
		$className = $model->getClassName();
186
		
187
		if (!class_exists($className)) {
188
			throw new ModuleException(sprintf('Action (%s) not found in Module (%s)', $className, $this->model->getName()));
189
		}
190
		
191
		$class = new $className($model, $this, $response);
192
		
193
// 		// l10n
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
194
// 		$app = $this->getServiceContainer()->getApplication();
195
// 		$lang = $app->getLocalization()->getLanguage()->getAlpha2();
196
// 		$country = $app->getLocalization()->getCountry()->getAlpha2();
197
// 		$l10n = $this->getPath() . 'l10n/';
198
// 		$locale = $lang . '_' . $country;
199
		
200
// 		// load module l10n
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
201
// 		$this->addL10nFile('module', $l10n, $lang, $locale, $class);
202
		
203
// 		// load additional l10n files
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...
204
// 		if (isset($this->actions[$actionName]['l10n'])) {
205
// 			foreach ($this->actions[$actionName]['l10n'] as $file) {
206
// 				$this->addL10nFile($file, $l10n, $lang, $locale, $class);
207
// 			}
208
// 		}
209
			
210
// 		// load action l10n
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% 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
// 		$this->addL10nFile(sprintf('actions/%s', $actionName), $l10n, $lang, $locale, $class);
212
		
213
// 		// assets
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
214
// 		$page = $app->getPage();
215
// 		$moduleUrl = sprintf('%s/_keeko/modules/%s/', $app->getRootUrl(), $this->getName());
216
// 		if (isset($this->actions[$actionName]['assets']['styles'])) {
217
// 			foreach ($this->actions[$actionName]['assets']['styles'] as $style) {
218
// 				$page->addStyle($moduleUrl . $style);
219
// 			}
220
// 		}
221
		
222
// 		if (isset($this->actions[$actionName]['assets']['scripts'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% 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...
223
// 			foreach ($this->actions[$actionName]['assets']['scripts'] as $script) {
224
// 				$page->addScript($moduleUrl . $script);
225
// 			}
226
// 		}
227
228
		return $class;
229
	}
230
	
231
	private function addL10nFile($file, $dir, $lang, $locale, $class) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
232
		$translator = $this->getServiceContainer()->getTranslator();
233
		$langPath = sprintf('%s%s/%s.json', $dir, $lang, $file);
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...
234
		$localePath = sprintf('%s%s/%s.json', $dir, $locale, $file);
235
		
236
		if (file_exists($langPath)) {
237
			$translator->addResource('json', $langPath, $lang, $class->getCanonicalName());
238
		}
239
		
240
		if (file_exists($localePath)) {
241
			$translator->addResource('json', $langPath, $locale, $class->getCanonicalName());
242
		}
243
	}
244
	
245
	/**
246
	 * Shortcut for getting permission on the given action in this module
247
	 *
248
	 * @param string $action
249
	 * @param User $user
250
	 */
251
	public function hasPermission($action, User $user = null) {
252
		return $this->getServiceContainer()->getFirewall()->hasPermission($this->getName(), $action, $user);
253
	}
254
	
255
	public function getTwig() {
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...
256
		return $this->getRawTwig($this->getPath() . 'templates/');
257
	}
258
259
	abstract public function install();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
260
261
	abstract public function uninstall();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
262
263
	abstract public function update($from, $to);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
264
}
265