Conditions | 44 |
Paths | 0 |
Total Lines | 202 |
Code Lines | 124 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
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 |
||
303 | public function getTransmissionFromMessage(Swift_Mime_SimpleMessage $message) |
||
304 | { |
||
305 | $contentType = $this->getMessagePrimaryContentType($message); |
||
306 | |||
307 | $fromAddresses = $message->getFrom(); |
||
308 | |||
309 | $fromFirstEmail = key($fromAddresses); |
||
310 | $fromFirstName = current($fromAddresses); |
||
311 | if (SparkPostHelper::config()->override_admin_email && SparkPostHelper::isAdminEmail($fromFirstEmail)) { |
||
312 | $fromFirstEmail = SparkPostHelper::resolveDefaultFromEmail(); |
||
313 | } |
||
314 | if (SparkPostHelper::getEnvForceSender()) { |
||
315 | $fromFirstEmail = SparkPostHelper::getEnvForceSender(); |
||
316 | } |
||
317 | if (!$fromFirstName) { |
||
318 | $fromFirstName = EmailUtils::get_displayname_from_rfc_email($fromFirstEmail); |
||
319 | } |
||
320 | $this->fromEmail = $fromFirstEmail; |
||
321 | |||
322 | $toAddresses = $message->getTo(); |
||
323 | $ccAddresses = $message->getCc() ? $message->getCc() : []; |
||
324 | $bccAddresses = $message->getBcc() ? $message->getBcc() : []; |
||
325 | $replyToAddresses = $message->getReplyTo() ? $message->getReplyTo() : []; |
||
326 | |||
327 | $recipients = array(); |
||
328 | $cc = array(); |
||
329 | $bcc = array(); |
||
330 | $attachments = array(); |
||
331 | $headers = array(); |
||
332 | $tags = array(); |
||
333 | $metadata = array(); |
||
334 | $inlineCss = null; |
||
335 | |||
336 | // Mandrill compatibility |
||
337 | // Data is merge with transmission and removed from headers |
||
338 | // @link https://mailchimp.com/developer/transactional/docs/tags-metadata/#tags |
||
339 | if ($message->getHeaders()->has('X-MC-Tags')) { |
||
340 | $tagsHeader = $message->getHeaders()->get('X-MC-Tags'); |
||
341 | $tags = explode(',', self::getHeaderValue($tagsHeader)); |
||
342 | $message->getHeaders()->remove('X-MC-Tags'); |
||
343 | } |
||
344 | if ($message->getHeaders()->has('X-MC-Metadata')) { |
||
345 | $metadataHeader = $message->getHeaders()->get('X-MC-Metadata'); |
||
346 | $metadata = json_decode(self::getHeaderValue($metadataHeader), JSON_OBJECT_AS_ARRAY); |
||
347 | $message->getHeaders()->remove('X-MC-Metadata'); |
||
348 | } |
||
349 | if ($message->getHeaders()->has('X-MC-InlineCSS')) { |
||
350 | $inlineHeader = $message->getHeaders()->get('X-MC-InlineCSS'); |
||
351 | $inlineCss = self::getHeaderValue($inlineHeader); |
||
352 | $message->getHeaders()->remove('X-MC-InlineCSS'); |
||
353 | } |
||
354 | |||
355 | // Handle MSYS headers |
||
356 | // Data is merge with transmission and removed from headers |
||
357 | // @link https://developers.sparkpost.com/api/smtp-api.html |
||
358 | $msysHeader = []; |
||
359 | if ($message->getHeaders()->has('X-MSYS-API')) { |
||
360 | $msysHeaderObj = $message->getHeaders()->get('X-MSYS-API'); |
||
361 | $msysHeader = json_decode(self::getHeaderValue($msysHeaderObj), JSON_OBJECT_AS_ARRAY); |
||
362 | if (!empty($msysHeader['tags'])) { |
||
363 | $tags = array_merge($tags, $msysHeader['tags']); |
||
364 | } |
||
365 | if (!empty($msysHeader['metadata'])) { |
||
366 | $metadata = array_merge($metadata, $msysHeader['metadata']); |
||
367 | } |
||
368 | $message->getHeaders()->remove('X-MSYS-API'); |
||
369 | } |
||
370 | |||
371 | // Build recipients list |
||
372 | // @link https://developers.sparkpost.com/api/recipient-lists.html |
||
373 | $primaryEmail = null; |
||
374 | foreach ($toAddresses as $toEmail => $toName) { |
||
375 | if ($primaryEmail === null) { |
||
376 | $primaryEmail = $toEmail; |
||
377 | } |
||
378 | if (!$toName) { |
||
379 | $toName = $toEmail; |
||
380 | } |
||
381 | $recipient = array( |
||
382 | 'address' => array( |
||
383 | 'email' => $toEmail, |
||
384 | 'name' => $toName, |
||
385 | ) |
||
386 | ); |
||
387 | if (!empty($tags)) { |
||
388 | $recipient['tags'] = $tags; |
||
389 | } |
||
390 | // TODO: metadata are not valid? |
||
391 | if (!empty($metadata)) { |
||
392 | $recipient['metadata'] = $metadata; |
||
393 | } |
||
394 | $recipients[] = $recipient; |
||
395 | } |
||
396 | |||
397 | $reply_to = null; |
||
398 | foreach ($replyToAddresses as $replyToEmail => $replyToName) { |
||
399 | if ($replyToName) { |
||
400 | $reply_to = sprintf('%s <%s>', $replyToName, $replyToEmail); |
||
401 | } else { |
||
402 | $reply_to = $replyToEmail; |
||
403 | } |
||
404 | } |
||
405 | |||
406 | // @link https://www.sparkpost.com/docs/faq/cc-bcc-with-rest-api/ |
||
407 | foreach ($ccAddresses as $ccEmail => $ccName) { |
||
408 | $cc[] = array( |
||
409 | 'email' => $ccEmail, |
||
410 | 'name' => $ccName, |
||
411 | 'header_to' => $primaryEmail ? $primaryEmail : $ccEmail, |
||
412 | ); |
||
413 | } |
||
414 | |||
415 | foreach ($bccAddresses as $bccEmail => $bccName) { |
||
416 | $bcc[] = array( |
||
417 | 'email' => $bccEmail, |
||
418 | 'name' => $bccName, |
||
419 | 'header_to' => $primaryEmail ? $primaryEmail : $ccEmail, |
||
420 | ); |
||
421 | } |
||
422 | |||
423 | $bodyHtml = $bodyText = null; |
||
424 | |||
425 | if ($contentType === 'text/plain') { |
||
426 | $bodyText = $message->getBody(); |
||
427 | } elseif ($contentType === 'text/html') { |
||
428 | $bodyHtml = $message->getBody(); |
||
429 | } else { |
||
430 | $bodyHtml = $message->getBody(); |
||
431 | } |
||
432 | |||
433 | foreach ($message->getChildren() as $child) { |
||
434 | if ($child instanceof Swift_Attachment) { |
||
435 | $attachments[] = array( |
||
436 | 'type' => $child->getContentType(), |
||
437 | 'name' => $child->getFilename(), |
||
438 | 'data' => base64_encode($child->getBody()) |
||
439 | ); |
||
440 | } elseif ($child instanceof Swift_MimePart && $this->supportsContentType($child->getContentType())) { |
||
441 | if ($child->getContentType() == "text/html") { |
||
442 | $bodyHtml = $child->getBody(); |
||
443 | } elseif ($child->getContentType() == "text/plain") { |
||
444 | $bodyText = $child->getBody(); |
||
445 | } |
||
446 | } |
||
447 | } |
||
448 | |||
449 | // If we ask to provide plain, use our custom method instead of the provided one |
||
450 | if ($bodyHtml && SparkPostHelper::config()->provide_plain) { |
||
451 | $bodyText = EmailUtils::convert_html_to_text($bodyHtml); |
||
452 | } |
||
453 | |||
454 | // Should we inline css |
||
455 | if (!$inlineCss && SparkPostHelper::config()->inline_styles) { |
||
456 | $bodyHtml = EmailUtils::inline_styles($bodyHtml); |
||
457 | } |
||
458 | |||
459 | // Custom unsubscribe list |
||
460 | if ($message->getHeaders()->has('List-Unsubscribe')) { |
||
461 | $unsubHeader = $message->getHeaders()->get('List-Unsubscribe'); |
||
462 | $headers['List-Unsubscribe'] = self::getHeaderValue($unsubHeader); |
||
463 | } |
||
464 | |||
465 | $defaultParams = SparkPostHelper::config()->default_params; |
||
466 | if ($inlineCss !== null) { |
||
467 | $defaultParams['inline_css'] = $inlineCss; |
||
468 | } |
||
469 | |||
470 | // Build base transmission |
||
471 | $sparkPostMessage = array( |
||
472 | 'recipients' => $recipients, |
||
473 | 'content' => array( |
||
474 | 'from' => array( |
||
475 | 'name' => $fromFirstName, |
||
476 | 'email' => $fromFirstEmail, |
||
477 | ), |
||
478 | 'subject' => $message->getSubject(), |
||
479 | 'html' => $bodyHtml, |
||
480 | 'text' => $bodyText, |
||
481 | ), |
||
482 | ); |
||
483 | if ($reply_to) { |
||
484 | $sparkPostMessage['reply_to'] = $reply_to; |
||
485 | } |
||
486 | |||
487 | // Add default params |
||
488 | $sparkPostMessage = array_merge($defaultParams, $sparkPostMessage); |
||
489 | if ($msysHeader) { |
||
490 | $sparkPostMessage = array_merge($sparkPostMessage, $msysHeader); |
||
491 | } |
||
492 | |||
493 | // Add remaining elements |
||
494 | if (!empty($cc)) { |
||
495 | $sparkPostMessage['headers.CC'] = $cc; |
||
496 | } |
||
497 | if (!empty($headers)) { |
||
498 | $sparkPostMessage['customHeaders'] = $headers; |
||
499 | } |
||
500 | if (count($attachments) > 0) { |
||
501 | $sparkPostMessage['attachments'] = $attachments; |
||
502 | } |
||
503 | |||
504 | return $sparkPostMessage; |
||
505 | } |
||
515 |