Passed
Push — master ( fee628...0561d0 )
by Justin
07:31
created

SendMailPage::getContent()   C

Complexity

Conditions 14
Paths 12

Size

Total Lines 63
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 63
rs 6.0952
cc 14
eloc 34
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 getContent(): string {
31
		$template = new DwooTemplate("pages/sendmail");
32
33
		$template->assign("action_url", DomainUtils::generateURL("pages/sendmail"));
34
35
		if (isset($_REQUEST['submit'])) {
36
			//first, check csrf token
37
			if (!Security::checkCSRFToken()) {
38
				$template->assign("error_message", "Wrong CSRF token!");
39
40
				if (isset($_POST['content'])) {
41
					$template->assign("content", $_POST['content']);
42
				}
43
			} else {
44
				$required_fields = array("to_mail", "subject", "content");
45
46
				foreach ($required_fields as $field) {
47
					if (!isset($_POST[$field]) || empty($_POST['field'])) {
48
						$template->assign("error_message", "Please complete form!");
49
50
						if (isset($_POST['content'])) {
51
							$template->assign("content", $_POST['content']);
52
						}
53
54
						return $template->getCode();
55
					}
56
				}
57
58
				//form is complete
59
				$to_mail = $_POST['to_mail'];
60
				$subject = $_POST['subject'];
61
				$content = $_POST['content'];
62
63
				//check, if mail is valide
64
				if (!(new Validator_Mail())->isValide($to_mail)) {
65
					$template->assign("error_message", "Mail is not valide!");
66
67
					if (isset($_POST['content'])) {
68
						$template->assign("content", $_POST['content']);
69
					}
70
				} else if (!(new Validator_String())->isValide($subject)) {
71
					$template->assign("error_message", "Subject is not valide!");
72
73
					if (isset($_POST['content'])) {
74
						$template->assign("content", $_POST['content']);
75
					}
76
				} else {
77
					//parameters are valide, send mail
78
79
					if (MailApi::sendHTMLMail($to_mail, $subject, $content)) {
80
						$template->assign("success_message", "Mail sended successfully!");
81
					} else {
82
						$template->assign("error_message", "Sending of mail failed!");
83
84
						if (isset($_POST['content'])) {
85
							$template->assign("content", $_POST['content']);
86
						}
87
					}
88
				}
89
			}
90
		}
91
92
		return $template->getCode();
93
	}
94
95
}
96
97
?>
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...
98