| Conditions | 11 |
| Paths | 320 |
| Total Lines | 76 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 97 | public function sendContactForm() |
||
| 98 | { |
||
| 99 | $this->onlyPost(); |
||
| 100 | |||
| 101 | //verify input values (html special chars ?) |
||
| 102 | $to = $this->config["admin_email_address"]; |
||
| 103 | $message = $this->request->getDataFull(); |
||
| 104 | |||
| 105 | //Error checking |
||
| 106 | |||
| 107 | //check all the fields |
||
| 108 | $error = false; |
||
| 109 | $contactErrors = new \stdClass(); |
||
| 110 | |||
| 111 | if ($message["contactName"] == "") { |
||
| 112 | $error = true; |
||
| 113 | $contactErrors->contactName = "Name must not be empty"; |
||
| 114 | } |
||
| 115 | if ($message["contactEmail"] == "") { |
||
| 116 | $error = true; |
||
| 117 | $contactErrors->contactEmail = "Email must not be empty"; |
||
| 118 | } |
||
| 119 | if ($message["contactSubject"] == "") { |
||
| 120 | $error = true; |
||
| 121 | $contactErrors->contactSubject = "Subject must not be empty"; |
||
| 122 | } |
||
| 123 | if ($message["contactMessage"] == "") { |
||
| 124 | $error = true; |
||
| 125 | $contactErrors->contactMessage = "Message must not be empty"; |
||
| 126 | } |
||
| 127 | if (!$this->isEmail($message["contactEmail"])) { |
||
| 128 | $error = true; |
||
| 129 | $contactErrors->contactEmail = "email is not valid"; |
||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | if(Config::GOOGLE_RECAPCHA_PUBLIC_KEY !== "" && Config::GOOGLE_RECAPCHA_SECRET_KEY !== "") |
||
| 134 | { |
||
| 135 | if(empty($message["g-recaptcha-response"])) |
||
| 136 | { |
||
| 137 | $error = true; |
||
| 138 | $this->alertBox->setAlert('Capcha not set', 'error'); |
||
| 139 | } |
||
| 140 | //check the capcha |
||
| 141 | $grequest = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.Config::GOOGLE_RECAPCHA_SECRET_KEY.'&response='.$message["g-recaptcha-response"]); |
||
| 142 | // The result is in a JSON format. Decoding.. |
||
| 143 | $gresponse = json_decode($grequest); |
||
| 144 | if(!$gresponse->success) |
||
| 145 | { |
||
| 146 | $error = true; |
||
| 147 | $this->alertBox->setAlert('Capcha Error', 'error'); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | //If we found an error, return data to the register form and no create |
||
| 152 | if ($error) { |
||
| 153 | $this->session->set("contactInfo", $message); |
||
| 154 | $this->session->set("contactErrors", $contactErrors); |
||
| 155 | $this->response->redirect("/home/contact"); |
||
| 156 | } |
||
| 157 | |||
| 158 | $config = $this->siteConfig->getSiteConfig(); |
||
| 159 | |||
| 160 | //from here all is good, send mail |
||
| 161 | $userName = htmlspecialchars($message["contactName"]); |
||
| 162 | $subject = "Contact from ".$config["site_name"]." : "; |
||
| 163 | $subject .= htmlspecialchars($message["contactSubject"]); |
||
| 164 | $textMessage = "<h1>message sent by ".$userName."</h1>"; |
||
| 165 | $textMessage .= "<p>from : <a href='mailto:".$message["contactEmail"]."'>".$message["contactEmail"]."</a></p>"; |
||
| 166 | $textMessage .= htmlspecialchars($message["contactMessage"]); |
||
| 167 | $from = $config["SMTP_from"]; |
||
| 168 | |||
| 169 | $this->sendMail->send($to, $subject, $textMessage, $from); |
||
| 170 | |||
| 171 | $this->alertBox->setAlert('Email sent'); |
||
| 172 | $this->response->redirect(); |
||
| 173 | } |
||
| 174 | } |