| Conditions | 44 |
| Paths | > 20000 |
| Total Lines | 199 |
| Lines | 20 |
| Ratio | 10.05 % |
| 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 |
||
| 284 | public function buildMessage(Swift_Mime_Message $message) |
||
| 285 | { |
||
| 286 | $contentType = $this->getMessagePrimaryContentType($message); |
||
| 287 | |||
| 288 | $fromAddresses = $message->getFrom(); |
||
| 289 | $fromEmails = array_keys($fromAddresses); |
||
| 290 | $fromFirstEmail = key($fromAddresses); |
||
| 291 | $fromFirstName = current($fromAddresses); |
||
| 292 | |||
| 293 | if ($fromFirstName) { |
||
| 294 | $this->fromEmail = sprintf('%s <%s>', $fromFirstName, $fromFirstEmail); |
||
| 295 | } else { |
||
| 296 | $this->fromEmail = $fromFirstEmail; |
||
| 297 | } |
||
| 298 | |||
| 299 | $toAddresses = $message->getTo(); |
||
| 300 | $ccAddresses = $message->getCc() ? $message->getCc() : []; |
||
| 301 | $bccAddresses = $message->getBcc() ? $message->getBcc() : []; |
||
| 302 | $replyToAddresses = $message->getReplyTo() ? $message->getReplyTo() : []; |
||
| 303 | |||
| 304 | $recipients = array(); |
||
| 305 | $cc = array(); |
||
| 306 | $bcc = array(); |
||
| 307 | $attachments = array(); |
||
| 308 | $headers = array(); |
||
| 309 | $tags = array(); |
||
| 310 | $metadata = array(); |
||
| 311 | $inlineCss = null; |
||
| 312 | |||
| 313 | // Mandrill compatibility |
||
| 314 | // Data is merge with transmission and removed from headers |
||
| 315 | // @link https://mandrill.zendesk.com/hc/en-us/articles/205582467-How-to-Use-Tags-in-Mandrill |
||
| 316 | View Code Duplication | if ($message->getHeaders()->has('X-MC-Tags')) { |
|
| 317 | $tagsHeader = $message->getHeaders()->get('X-MC-Tags'); |
||
| 318 | $tags = explode(',', $tagsHeader->getValue()); |
||
| 319 | $message->getHeaders()->remove('X-MC-Tags'); |
||
| 320 | } |
||
| 321 | View Code Duplication | if ($message->getHeaders()->has('X-MC-Metadata')) { |
|
| 322 | $metadataHeader = $message->getHeaders()->get('X-MC-Metadata'); |
||
| 323 | $metadata = json_decode($metadataHeader->getValue(), JSON_OBJECT_AS_ARRAY); |
||
| 324 | $message->getHeaders()->remove('X-MC-Metadata'); |
||
| 325 | } |
||
| 326 | if ($message->getHeaders()->has('X-MC-InlineCSS')) { |
||
| 327 | $inlineCss = $message->getHeaders()->get('X-MC-InlineCSS')->getValue(); |
||
| 328 | $message->getHeaders()->remove('X-MC-InlineCSS'); |
||
| 329 | } |
||
| 330 | |||
| 331 | // Handle mailgun headers |
||
| 332 | // Data is merge with message and removed from headers |
||
| 333 | // @link https://documentation.mailgun.com/en/latest/user_manual.html#sending-via-smtp |
||
| 334 | View Code Duplication | if ($message->getHeaders()->has('X-Mailgun-Tag')) { |
|
| 335 | $tagsHeader = $message->getHeaders()->get('X-Mailgun-Tag'); |
||
| 336 | $tags = explode(',', $tagsHeader->getValue()); |
||
| 337 | $message->getHeaders()->remove('X-Mailgun-Tag'); |
||
| 338 | } |
||
| 339 | // @link https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages |
||
| 340 | View Code Duplication | if ($message->getHeaders()->has('X-Mailgun-Variables')) { |
|
| 341 | $metadataHeader = $message->getHeaders()->get('X-Mailgun-Variables'); |
||
| 342 | $metadata = json_decode($metadataHeader->getValue(), JSON_OBJECT_AS_ARRAY); |
||
| 343 | $message->getHeaders()->remove('X-Mailgun-Variables'); |
||
| 344 | } |
||
| 345 | |||
| 346 | // Build recipients |
||
| 347 | $primaryEmail = null; |
||
| 348 | foreach ($toAddresses as $toEmail => $toName) { |
||
| 349 | if ($primaryEmail === null) { |
||
| 350 | $primaryEmail = $toEmail; |
||
| 351 | } |
||
| 352 | if (!$toName) { |
||
| 353 | $toName = $toEmail; |
||
| 354 | } |
||
| 355 | if ($toName) { |
||
| 356 | $recipients[] = sprintf('%s <%s>', $toName, $toEmail); |
||
| 357 | } else { |
||
| 358 | $recipients[] = $toEmail; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | $reply_to = null; |
||
| 363 | foreach ($replyToAddresses as $replyToEmail => $replyToName) { |
||
| 364 | if ($replyToName) { |
||
| 365 | $reply_to = sprintf('%s <%s>', $replyToName, $replyToEmail); |
||
| 366 | } else { |
||
| 367 | $reply_to = $replyToEmail; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | foreach ($ccAddresses as $ccEmail => $ccName) { |
||
| 372 | if ($ccName) { |
||
| 373 | $cc[] = sprintf('%s <%s>', $ccName, $ccEmail); |
||
| 374 | } else { |
||
| 375 | $cc[] = $ccEmail; |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | foreach ($bccAddresses as $bccEmail => $bccName) { |
||
| 380 | if ($bccName) { |
||
| 381 | $bcc[] = sprintf('%s <%s>', $bccName, $bccEmail); |
||
| 382 | } else { |
||
| 383 | $bcc[] = $bccEmail; |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | $bodyHtml = $bodyText = null; |
||
| 388 | |||
| 389 | if ($contentType === 'text/plain') { |
||
| 390 | $bodyText = $message->getBody(); |
||
| 391 | } elseif ($contentType === 'text/html') { |
||
| 392 | $bodyHtml = $message->getBody(); |
||
| 393 | } else { |
||
| 394 | $bodyHtml = $message->getBody(); |
||
| 395 | } |
||
| 396 | |||
| 397 | foreach ($message->getChildren() as $child) { |
||
| 398 | // File attachment. You can post multiple attachment values. |
||
| 399 | // Important: You must use multipart/form-data encoding when sending attachments. |
||
| 400 | if ($child instanceof Swift_Attachment) { |
||
| 401 | // eg: 'attachment' => array('/path/to/file.txt', '/path/to/file.txt') |
||
| 402 | $attachments[] = $child->getFilename(); |
||
| 403 | } elseif ($child instanceof Swift_MimePart && $this->supportsContentType($child->getContentType())) { |
||
| 404 | if ($child->getContentType() == "text/html") { |
||
| 405 | $bodyHtml = $child->getBody(); |
||
| 406 | } elseif ($child->getContentType() == "text/plain") { |
||
| 407 | $bodyText = $child->getBody(); |
||
| 408 | } |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | // If we ask to provide plain, use our custom method instead of the provided one |
||
| 413 | if ($bodyHtml && MailgunHelper::config()->provide_plain) { |
||
| 414 | $bodyText = EmailUtils::convert_html_to_text($bodyHtml); |
||
| 415 | } |
||
| 416 | |||
| 417 | // Should we inline css |
||
| 418 | if (!$inlineCss && MailgunHelper::config()->inline_styles) { |
||
| 419 | $bodyHtml = EmailUtils::inline_styles($bodyHtml); |
||
| 420 | } |
||
| 421 | |||
| 422 | // Custom unsubscribe list |
||
| 423 | if ($message->getHeaders()->has('List-Unsubscribe')) { |
||
| 424 | $headers['List-Unsubscribe'] = $message->getHeaders()->get('List-Unsubscribe')->getValue(); |
||
| 425 | } |
||
| 426 | |||
| 427 | // Mailgun params format does not work well in yml, so we map them |
||
| 428 | $rawParams = MailgunHelper::config()->default_params; |
||
| 429 | $defaultParams = []; |
||
| 430 | foreach ($rawParams as $rawParamKey => $rawParamValue) { |
||
| 431 | switch ($rawParamKey) { |
||
| 432 | case 'inline': |
||
| 433 | $defaultParams['inline'] = $rawParamValue; |
||
| 434 | break; |
||
| 435 | case 'tracking_opens': |
||
| 436 | $defaultParams['o:tracking-opens'] = $rawParamValue; |
||
| 437 | break; |
||
| 438 | case 'tracking_clicks': |
||
| 439 | $defaultParams['o:tracking-clicks'] = $rawParamValue; |
||
| 440 | break; |
||
| 441 | case 'testmode': |
||
| 442 | $defaultParams['o:testmode'] = $rawParamValue; |
||
| 443 | break; |
||
| 444 | default: |
||
| 445 | $defaultParams[$rawParamKey] = $rawParamValue; |
||
| 446 | break; |
||
| 447 | } |
||
| 448 | } |
||
| 449 | |||
| 450 | // Build base transmission |
||
| 451 | $mailgunMessage = array( |
||
| 452 | 'to' => implode(',', $recipients), |
||
| 453 | 'from' => $this->fromEmail, |
||
| 454 | 'subject' => $message->getSubject(), |
||
| 455 | 'html' => $bodyHtml, |
||
| 456 | 'text' => $bodyText, |
||
| 457 | ); |
||
| 458 | if ($reply_to) { |
||
| 459 | $mailgunMessage['h:Reply-To'] = $reply_to; |
||
| 460 | } |
||
| 461 | |||
| 462 | // Add default params |
||
| 463 | $mailgunMessage = array_merge($defaultParams, $mailgunMessage); |
||
| 464 | |||
| 465 | // Add remaining elements |
||
| 466 | if (!empty($cc)) { |
||
| 467 | $mailgunMessage['cc'] = $cc; |
||
| 468 | } |
||
| 469 | if (!empty($bcc)) { |
||
| 470 | $mailgunMessage['bcc'] = $bcc; |
||
| 471 | } |
||
| 472 | if (!empty($headers)) { |
||
| 473 | foreach ($headers as $headerKey => $headerValue) { |
||
| 474 | $mailgunMessage['h:' . $headerKey] = $headerValue; |
||
| 475 | } |
||
| 476 | } |
||
| 477 | if (count($attachments) > 0) { |
||
| 478 | $mailgunMessage['attachment'] = $attachments; |
||
| 479 | } |
||
| 480 | |||
| 481 | return $mailgunMessage; |
||
| 482 | } |
||
| 483 | |||
| 492 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..