| Conditions | 22 |
| Paths | 750 |
| Total Lines | 96 |
| Code Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 106 | private function getPayload(Email $email, Envelope $envelope): array |
||
| 107 | { |
||
| 108 | $message = array_merge(MandrillHelper::config()->default_params, [ |
||
| 109 | 'html' => $email->getHtmlBody(), |
||
| 110 | 'text' => $email->getTextBody(), |
||
| 111 | 'subject' => $email->getSubject(), |
||
| 112 | 'from_email' => $envelope->getSender()->getAddress(), |
||
| 113 | 'to' => $this->getRecipients($email, $envelope), |
||
| 114 | ]); |
||
| 115 | |||
| 116 | if ('' !== $envelope->getSender()->getName()) { |
||
| 117 | $message['from_name'] = $envelope->getSender()->getName(); |
||
| 118 | } |
||
| 119 | |||
| 120 | foreach ($email->getAttachments() as $attachment) { |
||
| 121 | $headers = $attachment->getPreparedHeaders(); |
||
| 122 | $disposition = $headers->getHeaderBody('Content-Disposition'); |
||
| 123 | $att = [ |
||
| 124 | 'content' => $attachment->bodyToString(), |
||
| 125 | 'type' => $headers->get('Content-Type')->getBody(), |
||
| 126 | ]; |
||
| 127 | if ($name = $headers->getHeaderParameter('Content-Disposition', 'name')) { |
||
| 128 | $att['name'] = $name; |
||
| 129 | } |
||
| 130 | if ('inline' === $disposition) { |
||
| 131 | $message['images'][] = $att; |
||
| 132 | } else { |
||
| 133 | $message['attachments'][] = $att; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | $headersToBypass = ['from', 'to', 'cc', 'bcc', 'subject', 'content-type']; |
||
| 138 | foreach ($email->getHeaders()->all() as $name => $header) { |
||
| 139 | if (\in_array($name, $headersToBypass, true)) { |
||
| 140 | continue; |
||
| 141 | } |
||
| 142 | if ($header instanceof TagHeader) { |
||
| 143 | $message['tags'] = array_merge( |
||
| 144 | $message['tags'] ?? [], |
||
| 145 | explode(',', $header->getValue()) |
||
| 146 | ); |
||
| 147 | continue; |
||
| 148 | } |
||
| 149 | if ($header instanceof MetadataHeader) { |
||
| 150 | $message['metadata'][$header->getKey()] = $header->getValue(); |
||
| 151 | continue; |
||
| 152 | } |
||
| 153 | $message['headers'][$header->getName()] = $header->getBodyAsString(); |
||
| 154 | } |
||
| 155 | |||
| 156 | foreach ($email->getHeaders()->all() as $name => $header) { |
||
| 157 | if (!($header instanceof UnstructuredHeader)) { |
||
| 158 | continue; |
||
| 159 | } |
||
| 160 | $headerValue = $header->getValue(); |
||
| 161 | switch ($name) { |
||
| 162 | case 'List-Unsubscribe': |
||
| 163 | $message['headers']['List-Unsubscribe'] = $headerValue; |
||
| 164 | break; |
||
| 165 | case 'X-MC-InlineCSS': |
||
| 166 | $message['inline_css'] = $headerValue; |
||
| 167 | break; |
||
| 168 | case 'X-MC-Tags': |
||
| 169 | $tags = $headerValue; |
||
| 170 | if (!is_array($tags)) { |
||
| 171 | $tags = explode(',', $tags); |
||
| 172 | } |
||
| 173 | $message['tags'] = $tags; |
||
| 174 | break; |
||
| 175 | case 'X-MC-Autotext': |
||
| 176 | $autoText = $headerValue; |
||
| 177 | if (in_array($autoText, array('true', 'on', 'yes', 'y', true), true)) { |
||
| 178 | $message['auto_text'] = true; |
||
| 179 | } |
||
| 180 | if (in_array($autoText, array('false', 'off', 'no', 'n', false), true)) { |
||
| 181 | $message['auto_text'] = false; |
||
| 182 | } |
||
| 183 | break; |
||
| 184 | case 'X-MC-GoogleAnalytics': |
||
| 185 | $analyticsDomains = explode(',', $headerValue); |
||
| 186 | if (is_array($analyticsDomains)) { |
||
| 187 | $message['google_analytics_domains'] = $analyticsDomains; |
||
| 188 | } |
||
| 189 | break; |
||
| 190 | case 'X-MC-GoogleAnalyticsCampaign': |
||
| 191 | $message['google_analytics_campaign'] = $headerValue; |
||
| 192 | break; |
||
| 193 | default: |
||
| 194 | if (strncmp($header->getName(), 'X-', 2) === 0) { |
||
| 195 | $message['headers'][$header->getName()] = $headerValue; |
||
| 196 | } |
||
| 197 | break; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | return $message; |
||
| 202 | } |
||
| 293 |