| Conditions | 38 |
| Paths | > 20000 |
| Total Lines | 202 |
| Code Lines | 121 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 183 | public function getPayload(Email $email, Envelope $envelope): array |
||
| 184 | { |
||
| 185 | $from = $envelope->getSender(); |
||
| 186 | |||
| 187 | $fromFirstEmail = $from->getAddress(); |
||
| 188 | $fromFirstName = $from->getName(); |
||
| 189 | if (SparkPostHelper::config()->override_admin_email && SparkPostHelper::isAdminEmail($fromFirstEmail)) { |
||
| 190 | $fromFirstEmail = SparkPostHelper::resolveDefaultFromEmail(); |
||
| 191 | } |
||
| 192 | if (SparkPostHelper::getEnvForceSender()) { |
||
| 193 | $fromFirstEmail = SparkPostHelper::getEnvForceSender(); |
||
| 194 | } |
||
| 195 | if (!$fromFirstName) { |
||
| 196 | $fromFirstName = EmailUtils::get_displayname_from_rfc_email($fromFirstEmail); |
||
| 197 | } |
||
| 198 | |||
| 199 | $toAddresses = []; |
||
| 200 | $ccAddresses = []; |
||
| 201 | $bccAddresses = []; |
||
| 202 | |||
| 203 | foreach ($envelope->getRecipients() as $recipient) { |
||
| 204 | $type = 'to'; |
||
| 205 | if (\in_array($recipient, $email->getBcc(), true)) { |
||
| 206 | $type = 'bcc'; |
||
| 207 | } elseif (\in_array($recipient, $email->getCc(), true)) { |
||
| 208 | $type = 'cc'; |
||
| 209 | } |
||
| 210 | |||
| 211 | $recipientEmail = $recipient->getAddress(); |
||
| 212 | |||
| 213 | // This is always going to be empty because of Envelope:77 |
||
| 214 | // $this->recipients[] = new Address($recipient->getAddress()); |
||
| 215 | $recipientName = $recipient->getName(); |
||
| 216 | if (!$recipientName) { |
||
| 217 | $recipientName = EmailUtils::get_displayname_from_rfc_email($recipientEmail); |
||
| 218 | } |
||
| 219 | |||
| 220 | switch ($type) { |
||
| 221 | case 'to': |
||
| 222 | $toAddresses[$recipientEmail] = $recipientName; |
||
| 223 | break; |
||
| 224 | case 'cc': |
||
| 225 | $ccAddresses[$recipientEmail] = $recipientName; |
||
| 226 | break; |
||
| 227 | case 'bcc': |
||
| 228 | $bccAddresses[$recipientEmail] = $recipientName; |
||
| 229 | break; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | $recipients = []; |
||
| 234 | $cc = []; |
||
| 235 | $bcc = []; |
||
| 236 | $headers = []; |
||
| 237 | $tags = []; |
||
| 238 | $metadata = []; |
||
| 239 | $inlineCss = null; |
||
| 240 | |||
| 241 | // Mandrill compatibility |
||
| 242 | // Data is merged with transmission and removed from headers |
||
| 243 | // @link https://mailchimp.com/developer/transactional/docs/tags-metadata/#tags |
||
| 244 | $emailHeaders = $email->getHeaders(); |
||
| 245 | if ($emailHeaders->has('X-MC-Tags')) { |
||
| 246 | $tagsHeader = $emailHeaders->get('X-MC-Tags'); |
||
| 247 | $tags = explode(',', self::getHeaderValue($tagsHeader)); |
||
| 248 | $emailHeaders->remove('X-MC-Tags'); |
||
| 249 | } |
||
| 250 | if ($emailHeaders->has('X-MC-Metadata')) { |
||
| 251 | $metadataHeader = $emailHeaders->get('X-MC-Metadata'); |
||
| 252 | $metadata = json_decode(self::getHeaderValue($metadataHeader), true); |
||
| 253 | $emailHeaders->remove('X-MC-Metadata'); |
||
| 254 | } |
||
| 255 | if ($emailHeaders->has('X-MC-InlineCSS')) { |
||
| 256 | $inlineHeader = $emailHeaders->get('X-MC-InlineCSS'); |
||
| 257 | $inlineCss = self::getHeaderValue($inlineHeader); |
||
| 258 | $emailHeaders->remove('X-MC-InlineCSS'); |
||
| 259 | } |
||
| 260 | |||
| 261 | // Handle MSYS headers |
||
| 262 | // Data is merge with transmission and removed from headers |
||
| 263 | // @link https://developers.sparkpost.com/api/smtp-api.html |
||
| 264 | $msysHeader = []; |
||
| 265 | if ($emailHeaders->has('X-MSYS-API')) { |
||
| 266 | $msysHeaderObj = $emailHeaders->get('X-MSYS-API'); |
||
| 267 | $msysHeader = json_decode(self::getHeaderValue($msysHeaderObj), true); |
||
| 268 | if (!empty($msysHeader['tags'])) { |
||
| 269 | $tags = array_merge($tags, $msysHeader['tags']); |
||
| 270 | } |
||
| 271 | if (!empty($msysHeader['metadata'])) { |
||
| 272 | $metadata = array_merge($metadata, $msysHeader['metadata']); |
||
| 273 | } |
||
| 274 | $emailHeaders->remove('X-MSYS-API'); |
||
| 275 | } |
||
| 276 | |||
| 277 | // Build recipients list |
||
| 278 | // @link https://developers.sparkpost.com/api/recipient-lists.html |
||
| 279 | $primaryEmail = null; |
||
| 280 | foreach ($toAddresses as $toEmail => $toName) { |
||
| 281 | if ($primaryEmail === null) { |
||
| 282 | $primaryEmail = $toEmail; |
||
| 283 | } |
||
| 284 | if (!$toName) { |
||
| 285 | $toName = $toEmail; |
||
| 286 | } |
||
| 287 | $recipient = array( |
||
| 288 | 'address' => array( |
||
| 289 | 'email' => $toEmail, |
||
| 290 | 'name' => $toName, |
||
| 291 | ) |
||
| 292 | ); |
||
| 293 | if (!empty($tags)) { |
||
| 294 | $recipient['tags'] = $tags; |
||
| 295 | } |
||
| 296 | if (!empty($metadata)) { |
||
| 297 | // For each recipient, you can specify metadata and substitution data to personalize each email |
||
| 298 | // Recipient metadata takes precedence over transmission metadata |
||
| 299 | $recipient['metadata'] = $metadata; |
||
| 300 | } |
||
| 301 | $recipients[] = $recipient; |
||
| 302 | } |
||
| 303 | |||
| 304 | // @link https://www.sparkpost.com/docs/faq/cc-bcc-with-rest-api/ |
||
| 305 | foreach ($ccAddresses as $ccEmail => $ccName) { |
||
| 306 | $cc[] = array( |
||
| 307 | 'email' => $ccEmail, |
||
| 308 | 'name' => $ccName, |
||
| 309 | 'header_to' => $primaryEmail ? $primaryEmail : $ccEmail, |
||
| 310 | ); |
||
| 311 | } |
||
| 312 | |||
| 313 | foreach ($bccAddresses as $bccEmail => $bccName) { |
||
| 314 | $bcc[] = array( |
||
| 315 | 'email' => $bccEmail, |
||
| 316 | 'name' => $bccName, |
||
| 317 | 'header_to' => $primaryEmail ? $primaryEmail : $bccEmail, |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | |||
| 321 | $bodyHtml = (string)$email->getHtmlBody(); |
||
| 322 | $bodyText = (string)$email->getTextBody(); |
||
| 323 | |||
| 324 | if ($bodyHtml) { |
||
| 325 | // If we ask to provide plain, use our custom method instead of the provided one |
||
| 326 | if (SparkPostHelper::config()->provide_plain) { |
||
| 327 | $bodyText = EmailUtils::convert_html_to_text($bodyHtml); |
||
| 328 | } |
||
| 329 | |||
| 330 | // Should we inline css |
||
| 331 | if (!$inlineCss && SparkPostHelper::config()->inline_styles) { |
||
| 332 | $bodyHtml = EmailUtils::inline_styles($bodyHtml); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | // Custom unsubscribe list |
||
| 337 | if ($emailHeaders->has('List-Unsubscribe')) { |
||
| 338 | $unsubHeader = $emailHeaders->get('List-Unsubscribe'); |
||
| 339 | $headers['List-Unsubscribe'] = self::getHeaderValue($unsubHeader); |
||
| 340 | } |
||
| 341 | |||
| 342 | $defaultParams = SparkPostHelper::config()->default_params; |
||
| 343 | if ($inlineCss !== null) { |
||
| 344 | $defaultParams['inline_css'] = $inlineCss; |
||
| 345 | } |
||
| 346 | |||
| 347 | // Build base transmission. Keep in mind that parameters are mapped by the sdk |
||
| 348 | // @link @link https://developers.sparkpost.com/api/transmissions/#transmissions-post-send-inline-content |
||
| 349 | $sparkPostMessage = [ |
||
| 350 | 'recipients' => $recipients, |
||
| 351 | 'content' => [ |
||
| 352 | 'from' => [ |
||
| 353 | 'name' => $fromFirstName, |
||
| 354 | 'email' => $fromFirstEmail, |
||
| 355 | ], |
||
| 356 | 'subject' => $email->getSubject(), |
||
| 357 | 'html' => $bodyHtml, |
||
| 358 | 'text' => $bodyText, |
||
| 359 | ], |
||
| 360 | ]; |
||
| 361 | if ($email->getReplyTo()) { |
||
| 362 | $sparkPostMessage['reply_to'] = $email->getReplyTo(); |
||
| 363 | } |
||
| 364 | |||
| 365 | // Add default params |
||
| 366 | $sparkPostMessage = array_merge($defaultParams, $sparkPostMessage); |
||
| 367 | if ($msysHeader) { |
||
| 368 | $sparkPostMessage = array_merge($sparkPostMessage, $msysHeader); |
||
| 369 | } |
||
| 370 | |||
| 371 | // Add remaining elements |
||
| 372 | if (!empty($cc)) { |
||
| 373 | $sparkPostMessage['headers.CC'] = $cc; |
||
| 374 | } |
||
| 375 | if (!empty($headers)) { |
||
| 376 | $sparkPostMessage['customHeaders'] = $headers; |
||
| 377 | } |
||
| 378 | |||
| 379 | $attachments = $this->buildAttachments($email); |
||
| 380 | if (count($attachments) > 0) { |
||
| 381 | $sparkPostMessage['attachments'] = $attachments; |
||
| 382 | } |
||
| 383 | |||
| 384 | return $sparkPostMessage; |
||
| 385 | } |
||
| 471 |