Passed
Push — master ( 358905...02a0dd )
by Justin
25:34 queued 21:45
created

SendMailPage::getContent()   C

Complexity

Conditions 14
Paths 12

Size

Total Lines 66
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 3
Metric Value
c 6
b 2
f 3
dl 0
loc 66
rs 5.8815
cc 14
eloc 35
nc 12
nop 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: JuKuCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 24.04.2018
25
 * Time: 21:25
26
 */
27
28
class SendMailPage extends PageType {
29
30
	public function getAdditionalHeaderCode(): string {
31
		$base_url = DomainUtils::getBaseURL() . "/";
32
33
		return "<!-- iCheck -->
34
  				<link rel=\"stylesheet\" href=\"" . $base_url . "styles/admin/plugins/iCheck/flat/blue.css\">
35
		
36
				<!-- bootstrap wysihtml5 - text editor -->
37
  				<link rel=\"stylesheet\" href=\"" . $base_url . "styles/admin/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css\">";
38
	}
39
40
	public function getFooterScripts(): string {
41
		$base_url = DomainUtils::getBaseURL() . "/";
42
43
		return "<!-- iCheck -->
44
				<script src=\"" . $base_url . "styles/admin/plugins/iCheck/icheck.min.js\"></script>
45
				<!-- Bootstrap WYSIHTML5 -->
46
				<script src=\"" . $base_url . "styles/admin/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js\"></script>
47
				<!-- Page Script -->
48
				<script>
49
				  $(function () {
50
					//Add text editor
51
					$(\"#compose-textarea\").wysihtml5({
52
						\"font-styles\": true, /*Font styling, e.g. h1, h2, etc. Default true*/
53
						\"emphasis\": true, /*Italics, bold, etc. Default true*/
54
						\"lists\": true, /*(Un)ordered lists, e.g. Bullets, Numbers. Default true*/
55
						\"html\": true, /*Button which allows you to edit the generated HTML. Default false*/
56
						\"link\": true, /*Button to insert a link. Default true*/
57
						\"image\": false, /*Button to insert an image. Default true,*/
58
						\"color\": true /*Button to change color of font*/
59
					});
60
				  });
61
				</script>";
62
	}
63
64
	public function getContent(): string {
65
		$template = new DwooTemplate("pages/sendmail");
66
67
		$template->assign("form_action", DomainUtils::generateURL("admin/sendmail"));
68
		$template->assign("content", "");
69
70
		if (isset($_REQUEST['submit'])) {
71
			//first, check csrf token
72
			if (!Security::checkCSRFToken()) {
73
				$template->assign("error_message", "Wrong CSRF token!");
74
75
				if (isset($_POST['content'])) {
76
					$template->assign("content", $_POST['content']);
77
				}
78
			} else {
79
				$required_fields = array("to_mail", "subject", "content");
80
81
				foreach ($required_fields as $field) {
82
					if (!isset($_POST[$field]) || empty($_POST[$field])) {
83
						$template->assign("error_message", "Please complete form!");
84
85
						if (isset($_POST['content'])) {
86
							$template->assign("content", $_POST['content']);
87
						}
88
89
						return $template->getCode();
90
					}
91
				}
92
93
				//form is complete
94
95
				//get values
96
				$to_mail = $_POST['to_mail'];
97
				$subject = $_POST['subject'];
98
				$content = $_POST['content'];
99
100
				//check, if mail is valide
101
				if (!(new Validator_Mail())->isValide($to_mail)) {
102
					$template->assign("error_message", "Mail is not valide!");
103
104
					if (isset($_POST['content'])) {
105
						$template->assign("content", $_POST['content']);
106
					}
107
				} else if (!(new Validator_String())->isValide($subject)) {
108
					$template->assign("error_message", "Subject is not valide!");
109
110
					if (isset($_POST['content'])) {
111
						$template->assign("content", $_POST['content']);
112
					}
113
				} else {
114
					//parameters are valide, send mail
115
116
					if (MailApi::sendHTMLMail($to_mail, $subject, $content)) {
117
						$template->assign("success_message", "Mail sended successfully!");
118
					} else {
119
						$template->assign("error_message", "Sending of mail failed!");
120
121
						if (isset($_POST['content'])) {
122
							$template->assign("content", $_POST['content']);
123
						}
124
					}
125
				}
126
			}
127
		}
128
129
		return $template->getCode();
130
	}
131
132
	public function listRequiredPermissions(): array {
133
		return array("can_send_board_mails");
134
	}
135
136
}
137
138
?>
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...
139