| Conditions | 18 |
| Paths | 482 |
| Total Lines | 113 |
| Code Lines | 60 |
| 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 |
||
| 227 | public static function adminInvitationMail($targets, $introtext, $newtoken, $idpPrettyName, $federation) { |
||
| 228 | if (!in_array($introtext, OutsideComm::INVITE_CONTEXTS)) { |
||
| 229 | throw new Exception("Unknown invite mode!"); |
||
| 230 | } |
||
| 231 | if ($introtext == OutsideComm::INVITE_CONTEXTS[1] && $federation === NULL) { // comes from fed admin, so federation must be set |
||
| 232 | throw new Exception("Invitation from a fed admin, but we do not know the corresponding federation!"); |
||
| 233 | } |
||
| 234 | $cat = new \core\CAT(); |
||
| 235 | // we have a few stock intro texts on file |
||
| 236 | $introTexts = |
||
| 237 | [ |
||
| 238 | OutsideComm::INVITE_CONTEXTS[0] => sprintf(_("a %s of the %s %s \"%s\" has invited you to manage the %s together with him."), $cat->nomenclature_fed, CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $cat->nomenclature_inst, $idpPrettyName, $cat->nomenclature_inst), |
||
| 239 | OutsideComm::INVITE_CONTEXTS[1] => sprintf(_("a %s %s has invited you to manage the future %s \"%s\" (%s)."), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $cat->nomenclature_fed, $cat->nomenclature_inst, $idpPrettyName, strtoupper($federation->tld)), |
||
| 240 | OutsideComm::INVITE_CONTEXTS[2] => sprintf(_("a %s %s has invited you to manage the %s \"%s\"."), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], $cat->nomenclature_fed, $cat->nomenclature_inst, $idpPrettyName), |
||
| 241 | ]; |
||
| 242 | $validity = sprintf(_("This invitation is valid for 24 hours from now, i.e. until %s."), strftime("%x %X", time() + 86400)); |
||
| 243 | // need some nomenclature |
||
| 244 | |||
| 245 | // are we on https? |
||
| 246 | $proto = "http://"; |
||
| 247 | if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { |
||
| 248 | $proto = "https://"; |
||
| 249 | } |
||
| 250 | // then, send out the mail |
||
| 251 | $message = _("Hello,") . " |
||
| 252 | |||
| 253 | " . wordwrap($introTexts[$introtext]." ".$validity, 72) . " |
||
| 254 | |||
| 255 | "; |
||
| 256 | |||
| 257 | if ($federation !== NULL) { // see if we are supposed to add a custom message |
||
| 258 | $customtext = $federation->getAttributes('fed:custominvite'); |
||
| 259 | if (count($customtext) > 0) { |
||
| 260 | $message .= wordwrap(sprintf(_("Additional message from your %s administrator:"), $cat->nomenclature_fed), 72) . " |
||
| 261 | --------------------------------- |
||
| 262 | " |
||
| 263 | . wordwrap($customtext[0]['value'], 72) . " |
||
| 264 | --------------------------------- |
||
| 265 | |||
| 266 | "; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | $message .= wordwrap(sprintf(_("To enlist as an administrator for that %s, please click on the following link:"), $cat->nomenclature_inst), 72) . " |
||
| 271 | |||
| 272 | $proto" . $_SERVER['SERVER_NAME'] . dirname(dirname($_SERVER['SCRIPT_NAME'])) . "/admin/action_enrollment.php?token=$newtoken |
||
| 273 | |||
| 274 | " . wordwrap(sprintf(_("If clicking the link doesn't work, you can also go to the %s Administrator Interface at"), CONFIG['APPEARANCE']['productname']), 72) . " |
||
| 275 | |||
| 276 | $proto" . $_SERVER['SERVER_NAME'] . dirname(dirname($_SERVER['SCRIPT_NAME'])) . "/admin/ |
||
| 277 | |||
| 278 | " . |
||
| 279 | _("and enter the invitation token") . " |
||
| 280 | $newtoken |
||
| 281 | " . ( /* $new_idp_authorized_fedadmin */ FALSE ? |
||
| 282 | wordwrap(sprintf(_("manually. If you reply to this mail, you will reach your %s administrators."), $cat->nomenclature_fed), 72) : |
||
| 283 | wordwrap(_("manually. Please do not reply to this mail; this is a send-only address.")) ) . " |
||
| 284 | |||
| 285 | " . wordwrap(_("Do NOT forward the mail before the token has expired - or the recipients may be able to consume the token on your behalf!"), 72) . " |
||
| 286 | |||
| 287 | " . wordwrap(sprintf(_("We wish you a lot of fun with the %s."), CONFIG['APPEARANCE']['productname']), 72) . " |
||
| 288 | |||
| 289 | " . sprintf(_("Sincerely, |
||
| 290 | |||
| 291 | Your friendly folks from %s Operations"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']); |
||
| 292 | |||
| 293 | $mail = OutsideComm::mailHandle(); |
||
| 294 | // who to whom? |
||
| 295 | $mail->FromName = CONFIG['APPEARANCE']['productname'] . " Invitation System"; |
||
| 296 | if ($federation !== NULL) { |
||
| 297 | foreach ($federation->listFederationAdmins() as $fedadmin_id) { |
||
| 298 | $fedadmin = new \core\User($fedadmin_id); |
||
| 299 | $mailaddrAttrib = $fedadmin->getAttributes("user:email"); |
||
| 300 | $nameAttrib = $fedadmin->getAttributes("user:realname"); |
||
| 301 | $name = $nameAttrib[0]['value'] ?? sprintf(_("%s administrator"), $cat->nomenclature_fed); |
||
| 302 | if (count($mailaddrAttrib) > 0) { |
||
| 303 | $mail->addReplyTo($mailaddrAttrib[0]['value'], $name); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | if (isset(CONFIG['APPEARANCE']['invitation-bcc-mail']) && CONFIG['APPEARANCE']['invitation-bcc-mail'] !== NULL) { |
||
| 308 | $mail->addBCC(CONFIG['APPEARANCE']['invitation-bcc-mail']); |
||
| 309 | } |
||
| 310 | |||
| 311 | // all addresses are wrapped in a string, but PHPMailer needs a structured list of addressees |
||
| 312 | // sigh... so convert as needed |
||
| 313 | // first split multiple into one if needed |
||
| 314 | $recipients = explode(", ", $targets); |
||
| 315 | |||
| 316 | $secStatus = TRUE; |
||
| 317 | $domainStatus = TRUE; |
||
| 318 | |||
| 319 | // fill the destinations in PHPMailer API |
||
| 320 | foreach ($recipients as $recipient) { |
||
| 321 | $mail->addAddress($recipient); |
||
| 322 | $status = OutsideComm::mailAddressValidSecure($recipient); |
||
| 323 | if ($status < OutsideComm::MAILDOMAIN_STARTTLS) { |
||
| 324 | $secStatus = FALSE; |
||
| 325 | } |
||
| 326 | if ($status < 0) { |
||
| 327 | $domainStatus = FALSE; |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | if (!$domainStatus) { |
||
| 332 | return FALSE; |
||
| 333 | } |
||
| 334 | |||
| 335 | // what do we want to say? |
||
| 336 | $mail->Subject = sprintf(_("%s: you have been invited to manage an %s"), CONFIG['APPEARANCE']['productname'], $cat->nomenclature_inst); |
||
| 337 | $mail->Body = $message; |
||
| 338 | |||
| 339 | return $mail->send(); |
||
| 340 | } |
||
| 343 |