Mailer::sendMessage()   F
last analyzed

Complexity

Conditions 21
Paths 16893

Size

Total Lines 71
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 379.5109

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 4
cts 60
cp 0.0667
rs 2.5491
c 0
b 0
f 0
cc 21
eloc 50
nc 16893
nop 1
crap 379.5109

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Mail.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version XXX
11
 * @link http://www.sweelix.net
12
 * @package sweelix\sendgrid
13
 */
14
15
namespace sweelix\sendgrid;
16
17
18
use SendGrid\Exception as SendGridException;
19
use SendGrid\Email as SendGridMail;
20
use SendGrid as SendGridClient;
21
use yii\base\InvalidConfigException;
22
use yii\base\InvalidParamException;
23
use yii\mail\BaseMailer;
24
25
/**
26
 * This component allow user to send an email
27
 *
28
 * @author Philippe Gaultier <[email protected]>
29
 * @copyright 2010-2017 Philippe Gaultier
30
 * @license http://www.sweelix.net/license license
31
 * @version XXX
32
 * @link http://www.sweelix.net
33
 * @package sweelix\sendgrid
34
 * @since XXX
35
 * @todo implement batch messages using API
36
 */
37
class Mailer extends BaseMailer
38
{
39
    /**
40
     * @var string Sendgrid API Key
41
     */
42
    public $token;
43
44
    /**
45
     * @var string Sendgrid login
46
     */
47
    public $user;
48
49
    /**
50
     * @var string Sendgrid password
51
     */
52
    public $password;
53
54
    /**
55
     * @var array options as defined in https://github.com/sendgrid/sendgrid-php#usage
56
     */
57
    public $options;
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public $messageClass = 'sweelix\sendgrid\Message';
63
    /**
64
     * @param Message $message
65
     * @since XXX
66
     * @throws InvalidConfigException
67
     */
68 2
    public function sendMessage($message)
69
    {
70
        try {
71 2
            if (($this->token === null) && ($this->user === null && $this->password === null)) {
72 2
                throw new InvalidConfigException('Token or login/password are missing');
73
            }
74
            $client = null;
75
            if ($this->token !== null) {
76
                $client = new SendGridClient($this->token, $this->options);
77
            } elseif(($this->user !== null) && ($this->password !== null)) {
78
                $client = new SendGridClient($this->user, $this->password, $this->options);
79
            }
80
            if ($client === null) {
81
                throw new InvalidParamException('Email transport must be configured');
82
            }
83
            $sendGridMail = new SendGridMail();
84
            $replyTo = $message->getReplyTo();
85
            if ($replyTo !== null) {
86
                $sendGridMail->setReplyTo($replyTo);
87
            }
88
            $sendGridMail->setFrom($message->getFrom());
89
            if ($message->getFromName() !== null) {
90
                $sendGridMail->setFromName($message->getFromName());
91
            }
92
            foreach($message->getTo() as $email => $name) {
0 ignored issues
show
Bug introduced by
The expression $message->getTo() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
93
                $sendGridMail->addTo($email, $name);
94
            }
95
            foreach($message->getCc() as $email => $name) {
96
                $sendGridMail->addCc($email, $name);
97
            }
98
            foreach($message->getBcc() as $email => $name) {
99
                $sendGridMail->addBcc($email, $name);
100
            }
101
            $sendGridMail->setSubject($message->getSubject());
102
            foreach($message->getHeaders() as $header) {
0 ignored issues
show
Bug introduced by
The expression $message->getHeaders() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
103
                list($key, $value) = each($header);
104
                $sendGridMail->addHeader($key, $value);
105
            }
106
            foreach($message->getAttachments() as $attachment) {
0 ignored issues
show
Bug introduced by
The expression $message->getAttachments() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
107
                $cid = isset($attachment['ContentID']) ? $attachment['ContentID'] : null;
108
                $sendGridMail->addAttachment($attachment['File'], $attachment['Name'], $cid);
109
            }
110
111
            $templateId = $message->getTemplateId();
112
            if ($templateId === null) {
113
                $data = $message->getHtmlBody();
114
                if ($data !== null) {
115
                    $sendGridMail->setHtml($data);
116
                }
117
                $data = $message->getTextBody();
118
                if ($data !== null) {
119
                    $sendGridMail->setText($data);
120
                }
121
            } else {
122
                $sendGridMail->setTemplateId($templateId);
123
                // trigger html template
124
                $sendGridMail->setHtml(' ');
125
                // trigger text template
126
                $sendGridMail->setText(' ');
127
                $templateModel = $message->getTemplateModel();
128
                if (empty($templateModel) === false) {
129
                    $sendGridMail->setSubstitutions($message->getTemplateModel());
130
                }
131
            }
132
            $result = $client->send($sendGridMail);
133
            /* @var \SendGrid\Response $result */
134
            return $result->code == 200;
135 2
        } catch (SendGridException $e) {
136
            throw $e;
137
        }
138
    }
139
}