CreatePagePage::getContent()   F
last analyzed

Complexity

Conditions 20
Paths 1348

Size

Total Lines 111
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 0 Features 6
Metric Value
cc 20
eloc 56
c 10
b 0
f 6
nc 1348
nop 0
dl 0
loc 111
rs 0

How to fix   Long Method    Complexity   

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: 02.09.2018
25
 * Time: 18:45
26
 */
27
28
class CreatePagePage extends PageType {
29
30
	public function getContent(): string {
31
		$template = new DwooTemplate("pages/createpage");
32
33
		$errors = array();
34
35
		if (isset($_REQUEST['action']) && $_REQUEST['action'] === "create" && PermissionChecker::current()->hasRight("can_create_pages")) {
36
			//try to create a new page
37
38
			//first, get all values
39
			if (!isset($_REQUEST['folder']) || empty($_REQUEST['folder'])) {
40
				$errors[] = "Folder is not set!";
41
			}
42
43
			if (!isset($_REQUEST['page_alias']) || empty($_REQUEST['page_alias'])) {
44
				$errors[] = "Page alias isn't set!";
45
			}
46
47
			if (!isset($_REQUEST['title']) || empty($_REQUEST['title'])) {
48
				$errors[] = "Title is't set!";
49
			}
50
51
			if (!isset($_REQUEST['pagetype']) || empty($_REQUEST['pagetype'])) {
52
				$errors[] = "Pagetype is't set!";
53
			}
54
55
			if (empty($errors)) {
56
				$folder = $_REQUEST['folder'];
57
				$alias = $_REQUEST['page_alias'];
58
				$title = $_REQUEST['title'];
59
				$pagetype = $_REQUEST['pagetype'];
60
61
				//check, if folder exists
62
				if (!Folder::exists($folder)) {
63
					$errors[] = "Folder '" . htmlentities($folder) . "' doesn't exists!";
64
				}
65
66
				//check, if page alias already exists
67
				$page_full_alias = $folder . $alias;
68
69
				if (PHPUtils::startsWith($page_full_alias, "/")) {
70
					//remove / at beginning
71
					$page_full_alias = substr($page_full_alias, 1);
72
				}
73
74
				if (Page::exists($page_full_alias)) {
75
					$errors[] = "Page alias '" . htmlentities($page_full_alias) . "' already exists!";
76
				}
77
78
				//remove html characters in title
79
				$title = htmlentities($title);
80
81
				//check, if pagetype exists
82
				if (!PageType::exists($pagetype)) {
83
					$errors[] = "Pagetype '" . htmlentities($pagetype) . "' doesn't exists!";
84
				}
85
86
				Events::throwEvent("before_create_page", array(
87
					'folder' => &$folder,
88
					'alias' => &$alias,
89
					'full_alias' => &$page_full_alias,
90
					'title' => &$title,
91
					'pagetype' => &$pagetype
92
				));
93
94
				if (empty($errors)) {
95
					$pageID = Page::createIfAbsent($page_full_alias, htmlentities($title), $pagetype, "", $folder, -1, -1, -1, true, false, true, true, User::current()->getUsername());
96
97
					Events::throwEvent("after_create_page", array(
98
						'pageID' => $pageID
99
					));
100
101
					//redirect header
102
					header("Location: " . DomainUtils::generateURL("admin/edit_page", array("edit" => $pageID)));
103
104
					exit;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
105
				}
106
			}
107
		}
108
109
		//set form action url
110
		$template->assign("action_url", DomainUtils::generateURL($this->getPage()->getAlias(), array("action" => "create")));
111
		$template->assign("username", User::current()->getUsername());
112
113
		$template->assign("errors", $errors);
114
115
		//list page types
116
		$page_types_rows = PageType::listPageTypes();
117
		$page_types = array();
118
119
		foreach ($page_types_rows as $row) {
120
			$page_types[] = array(
121
				'title' => htmlentities(Translator::translateTitle($row['title'])),
122
				'class_name' => $row['page_type']
123
			);
124
		}
125
126
		$template->assign("pagetypes", $page_types);
127
128
		$folders = array();
129
130
		foreach (Folder::listFolders(false) as $row) {
131
			$folders[] = array(
132
				'folder' => $row['folder'],
133
				'hidden' => $row['hidden'] == 1,
134
				'is_root_folder' => $row['folder'] === "/"
135
			);
136
		}
137
138
		$template->assign("folders", $folders);
139
140
		return $template->getCode();
141
	}
142
143
	public function getFooterScripts(): string {
144
		return "";
145
	}
146
147
	public function listRequiredPermissions(): array {
148
		return array("can_create_pages");
149
	}
150
151
}
152
153
?>
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...
154