Completed
Push — master ( f0b491...11ee78 )
by Thomas
09:37
created

AbstractModule::update()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 1
nc 1
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\schema\PackageSchema;
13
use keeko\core\schema\ActionSchema;
14
15
abstract class AbstractModule {
16
	
17
	/** @var Module */
18
	protected $model;
19
20
	protected $actions;
21
22
	/** @var User */
23
	protected $user;
24
	
25
	/** @var ServiceContainer */
26
	protected $service;
27
	
28
	/** @var Preferences */
29
	private $preferences;
30
	
31
	/** @var PackageSchema */
32
	protected $package;
33
34
	public function __construct(Module $module, ServiceContainer $service) {
35
		$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...
36
		$this->service = $service;
37
		
38
		$packageManager = $service->getPackageManager();
39
		$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...
40
		
41
		$this->loadActions();
42
	}
43
	
44
	/**
45
	 * Returns the modules name
46
	 *
47
	 * @return string
48
	 */
49
	public function getName() {
50
		return $this->model->getName();
51
	}
52
	
53
	/**
54
	 * Returns the modules canonical name
55
	 *
56
	 * @return string
57
	 */
58
	public function getCanonicalName() {
59
		return str_replace('/', '.', $this->getName());
60
	}
61
	
62
	/**
63
	 * Returns the modules title
64
	 *
65
	 * @return string
66
	 */
67
	public function getTitle() {
68
		return $this->model->getTitle();
69
	}
70
	
71
	/**
72
	 * Returns the service container
73
	 *
74
	 * @return ServiceContainer
75
	 */
76
	public function getServiceContainer() {
77
		return $this->service;
78
	}
79
80
	/**
81
	 * Returns the associated module model
82
	 *
83
	 * @return Module
84
	 */
85
	public function getModel() {
86
		return $this->model;
87
	}
88
	
89
	
90
	
91
	public function getPath() {
92
		return sprintf('%s/%s/', KEEKO_PATH_MODULES, $this->model->getName());
93
	}
94
	
95
	public function getManagedFilesPath() {
96
		return sprintf('%s/managed/%s', KEEKO_PATH_FILES, $this->model->getName());
97
	}
98
	
99
	public function getManagedFilesUrl() {
100
		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...
101
	}
102
	
103
	/**
104
	 * Returns the module's preferences
105
	 *
106
	 * @return Preferences
107
	 */
108
	public function getPreferences() {
109
		if ($this->preferences === null) {
110
			$this->preferences = $this->service->getPreferenceLoader()->getModulePreferences($this->model->getId());
111
		}
112
		
113
		return $this->preferences;
114
	}
115
116
	private function loadActions() {
117
		$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...
118
		$actions = ActionQuery::create()->filterByModule($this->model)->find();
119
		foreach ($actions as $action) {
120
			$models[$action->getName()] = $action;
121
		}
122
		$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...
123
		$module = $keeko->getModule();
124
	
125
		
126
		$this->actions = [];
127
		foreach ($module->getActionNames() as $actionName) {
128
			if (isset($models[$actionName])) {
129
				$this->actions[$actionName] = [
130
					'model' => $models[$actionName],
131
					'action' => $module->getAction($actionName)
132
				];
133
			}
134
		}
135
	}
136
137
	/**
138
	 * Loads the given action
139
	 *
140
	 * @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...
141
	 * @param string $response the response type (e.g. html, json, ...)
142
	 * @return AbstractAction
143
	 */
144
	public function loadAction($nameOrAction, $response) {
145
		$model = null;
146
		if ($nameOrAction instanceof Action) {
147
			$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...
148
			$actionName = $nameOrAction->getName();
149
		} else {
150
			$actionName = $nameOrAction;
151
		}
152
		
153
		if (!isset($this->actions[$actionName])) {
154
			throw new ModuleException(sprintf('Action (%s) not found in Module (%s)', $actionName, $this->model->getName()));
155
		}
156
		
157
		if ($model === null) {
158
			$model = $this->actions[$actionName]['model'];
159
		}
160
		
161
		/* @var $action ActionSchema */
162
		$action = $this->actions[$actionName]['action'];
163
		
164
		
165
		// check permission
166
		if (!$this->service->getFirewall()->hasActionPermission($model)) {
167
			throw new PermissionDeniedException(sprintf('Can\'t access Action (%s) in Module (%s)', $actionName, $this->model->getName()));
168
		}
169
		
170
		// check if a response is given
171
		if (!$action->hasResponse($response)) {
172
			throw new ModuleException(sprintf('No Response (%s) given for Action (%s) in Module (%s)', $response, $actionName, $this->model->getName()));
173
		}
174
		$responseClass = $action->getResponse($response);
175
		
176
		if (!class_exists($responseClass)) {
177
			throw new ModuleException(sprintf('Response (%s) not found in Module (%s)', $responseClass, $this->model->getName()));
178
		}
179
		$response = new $responseClass($this, $response);
180
		
181
		// gets the action class
182
		$className = $model->getClassName();
183
		
184
		if (!class_exists($className)) {
185
			throw new ModuleException(sprintf('Action (%s) not found in Module (%s)', $className, $this->model->getName()));
186
		}
187
		
188
		$class = new $className($model, $this, $response);
189
		
190
// 		// 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...
191
// 		$app = $this->getServiceContainer()->getApplication();
192
// 		$lang = $app->getLocalization()->getLanguage()->getAlpha2();
193
// 		$country = $app->getLocalization()->getCountry()->getAlpha2();
194
// 		$l10n = $this->getPath() . 'l10n/';
195
// 		$locale = $lang . '_' . $country;
196
		
197
// 		// 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...
198
// 		$this->addL10nFile('module', $l10n, $lang, $locale, $class);
199
		
200
// 		// 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...
201
// 		if (isset($this->actions[$actionName]['l10n'])) {
202
// 			foreach ($this->actions[$actionName]['l10n'] as $file) {
203
// 				$this->addL10nFile($file, $l10n, $lang, $locale, $class);
204
// 			}
205
// 		}
206
			
207
// 		// 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...
208
// 		$this->addL10nFile(sprintf('actions/%s', $actionName), $l10n, $lang, $locale, $class);
209
		
210
// 		// 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...
211
// 		$page = $app->getPage();
212
// 		$moduleUrl = sprintf('%s/_keeko/modules/%s/', $app->getRootUrl(), $this->getName());
213
// 		if (isset($this->actions[$actionName]['assets']['styles'])) {
214
// 			foreach ($this->actions[$actionName]['assets']['styles'] as $style) {
215
// 				$page->addStyle($moduleUrl . $style);
216
// 			}
217
// 		}
218
		
219
// 		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...
220
// 			foreach ($this->actions[$actionName]['assets']['scripts'] as $script) {
221
// 				$page->addScript($moduleUrl . $script);
222
// 			}
223
// 		}
224
225
		return $class;
226
	}
227
	
228
	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...
229
		$translator = $this->getServiceContainer()->getTranslator();
230
		$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...
231
		$localePath = sprintf('%s%s/%s.json', $dir, $locale, $file);
232
		
233
		if (file_exists($langPath)) {
234
			$translator->addResource('json', $langPath, $lang, $class->getCanonicalName());
235
		}
236
		
237
		if (file_exists($localePath)) {
238
			$translator->addResource('json', $langPath, $locale, $class->getCanonicalName());
239
		}
240
	}
241
	
242
	/**
243
	 * Shortcut for getting permission on the given action in this module
244
	 *
245
	 * @param string $action
246
	 * @param User $user
247
	 */
248
	public function hasPermission($action, User $user = null) {
249
		return $this->getServiceContainer()->getFirewall()->hasPermission($this->getName(), $action, $user);
250
	}
251
252
	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...
253
254
	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...
255
256
	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...
257
}
258