| Conditions | 21 |
| Paths | 1940 |
| Total Lines | 100 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 |
||
| 66 | public function mailAction(array $msg = array()) |
||
| 67 | { |
||
| 68 | $recipient = $this->widgetConfiguration['recipient']; |
||
| 69 | $sender = $this->widgetConfiguration['sender']; |
||
| 70 | $required = $this->widgetConfiguration['required']; |
||
| 71 | $cc = $this->widgetConfiguration['cc']; |
||
| 72 | $bcc = $this->widgetConfiguration['bcc']; |
||
| 73 | |||
| 74 | $missing = array(); |
||
| 75 | /* example: $required = [ 'name', 'email,phone' ] => name and (phone or email) are required.*/ |
||
| 76 | foreach ($required as $orFieldList) { |
||
| 77 | $orFields = explode(',', $orFieldList); |
||
| 78 | $found = false; |
||
| 79 | foreach ($orFields as $field) { |
||
| 80 | if (array_key_exists($field, $msg) && strlen($msg[$field]) != 0) { |
||
| 81 | $found = true; |
||
| 82 | break; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | if (!$found) { |
||
| 87 | foreach ($orFields as $field) { |
||
| 88 | $missing[] = $field; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | if (count($missing)) { |
||
| 94 | return json_encode(array( |
||
| 95 | 'status' => 'fields-missing', |
||
| 96 | 'missing' => $missing |
||
| 97 | )); |
||
| 98 | } |
||
| 99 | |||
| 100 | if (!is_array($recipient) || !array_key_exists('email', $recipient)) { |
||
| 101 | /* TODO: Throw exception instead. */ |
||
| 102 | return json_encode(array( |
||
| 103 | 'status' => 'internal-error', |
||
| 104 | 'error' => '$recipient is not valid' |
||
| 105 | )); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (isset($recipient['name']) && strlen($recipient['name']) > 0) { |
||
| 109 | $tmp = $recipient; |
||
| 110 | $recipient = array(); |
||
| 111 | foreach (GeneralUtility::trimExplode(',', $tmp['email']) as $email) { |
||
| 112 | $recipient[$email] = $tmp['name']; |
||
| 113 | } |
||
| 114 | } else { |
||
| 115 | $recipient = GeneralUtility::trimExplode(',', $recipient['email']); |
||
| 116 | } |
||
| 117 | |||
| 118 | $sender = ($sender !== null) ? array($sender['email'] => $sender['name']) : MailUtility::getSystemFrom(); |
||
| 119 | |||
| 120 | $recipientSave = $recipient; |
||
| 121 | $recipientOverwrite = $this->widgetConfiguration['receiver_overwrite_email']; |
||
| 122 | if ($recipientOverwrite !== null) { |
||
| 123 | $recipient = $recipientOverwrite; |
||
| 124 | $cc = null; |
||
| 125 | $bcc = null; |
||
| 126 | } |
||
| 127 | |||
| 128 | $params = array( |
||
| 129 | 'to' => $recipient, |
||
| 130 | 'from' => $sender, |
||
| 131 | 'msg' => $msg |
||
| 132 | ); |
||
| 133 | |||
| 134 | $view = GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView'); |
||
| 135 | $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($this->widgetConfiguration['mailTemplate'])); |
||
| 136 | $view->assignMultiple($params); |
||
| 137 | $text = $view->render(); |
||
| 138 | |||
| 139 | list($subject, $body) = explode("\n", $text, 2); |
||
| 140 | |||
| 141 | if ($recipientOverwrite !== null) { |
||
| 142 | $subject .= ' – Recipient: ' . implode(',', $recipientSave); |
||
| 143 | } |
||
| 144 | |||
| 145 | $mail = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage'); |
||
| 146 | $mail->setFrom($sender); |
||
| 147 | $mail->setTo($recipient); |
||
| 148 | if ($cc) { |
||
| 149 | $mail->setCc(array($cc['email'] => $cc['name'])); |
||
| 150 | } |
||
| 151 | if ($bcc) { |
||
| 152 | $mail->setBcc(array($bcc['email'] => $bcc['name'])); |
||
| 153 | } |
||
| 154 | $mail->setSubject($subject); |
||
| 155 | $mail->setBody(trim($body)); |
||
| 156 | if (isset($msg['email']) && strlen($msg['email']) > 0) { |
||
| 157 | try { |
||
| 158 | $mail->setReplyTo($msg['email']); |
||
| 159 | } catch (\Swift_RfcComplianceException $e) { |
||
| 160 | // ignore for now |
||
| 161 | } |
||
| 162 | } |
||
| 163 | $mail->send(); |
||
| 164 | |||
| 165 | return json_encode(array('status' => 'ok')); |
||
| 166 | } |
||
| 185 |