Completed
Push — master ( fa4a65...9c9e24 )
by Justin
03:06
created

SettingsPage::getContent()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 66
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 2 Features 1
Metric Value
cc 8
eloc 35
nc 8
nop 0
dl 0
loc 66
rs 8.1155
c 7
b 2
f 1

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
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: RocketCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 28.08.2018
25
 * Time: 17:04
26
 */
27
28
class SettingsPage extends PageType {
29
30
	public function getContent(): string {
31
		$template = new DwooTemplate("pages/settings");
32
33
		$template->assign("form_action", DomainUtils::generateURL("admin/settings", array("option" => "save")));
34
		$template->assign("content", "");
35
36
		$categories = array();
37
38
		$all_settings_by_category = Settings::listAllSettingsByCategory();
39
40
		$save = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $save is dead and can be removed.
Loading history...
41
		$save_success = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $save_success is dead and can be removed.
Loading history...
42
43
		if (isset($_REQUEST['option']) && $_REQUEST['option'] == "save") {
44
			$save = true;
45
		}
46
47
		foreach (SettingsCategory::listAllCategories() as $category) {
48
			$category = SettingsCategory::cast($category);
49
50
			$settings = array();
51
52
			if (isset($all_settings_by_category[$category->getCategory()])) {
53
				//list settings
54
				$rows = $all_settings_by_category[$category->getCategory()];
55
56
				foreach ($rows as $key=>$row) {
57
					$datatype = $row['datatype'];
58
					$datatype_params = unserialize($row['datatype_params']);
59
60
					$obj = new $datatype();
61
62
					if (!($obj instanceof DataType_Base)) {
63
						throw new IllegalArgumentException("obj of class name '" . $datatype . "' has to be an instance of DataType_Base.");
64
					}
65
66
					//load instance
67
					$obj->load($row, $datatype_params);
68
69
					//try to validate
70
					if (!$obj->val()) {
71
						$save_success = false;
72
					} else {
73
						//save object
74
						$obj->save();
75
					}
76
77
					$settings[] = array(
78
						'title' => Translator::translateTitle($row['title']),
79
						'description' => Translator::translateTitle($row['description']),
80
						'code' => $obj->getFormCode()
81
					);
82
				}
83
			}
84
85
			$categories[] = array(
86
				'title' => $category->getTitle(),
87
				'settings' => $settings
88
			);
89
		}
90
91
		Settings::saveAsync();
92
93
		$template->assign("categories", $categories);
94
95
		return $template->getCode();
96
	}
97
98
	public function listRequiredPermissions(): array {
99
		return array("can_see_global_settings", "can_edit_global_settings");
100
	}
101
102
}
103
104
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
105