Conditions | 17 |
Paths | 3330 |
Total Lines | 89 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
58 | public function sendMessage($message) |
||
59 | { |
||
60 | try { |
||
61 | if (($this->token === null)) { |
||
62 | throw new InvalidConfigException('Token or login/password are missing'); |
||
63 | } |
||
64 | $client = null; |
||
65 | if ($this->token !== null) { |
||
66 | $client = new SendGrid($this->token, $this->options); |
||
67 | } |
||
68 | if ($client === null) { |
||
69 | throw new InvalidArgumentException('Email transport must be configured'); |
||
70 | } |
||
71 | $sendGridMail = new Mail(); |
||
72 | $replyTo = $message->getReplyTo(); |
||
73 | if ($replyTo !== null) { |
||
|
|||
74 | $sendGridMail->setReplyTo($replyTo); |
||
75 | } |
||
76 | $sendGridMail->setFrom($message->getFrom(), $message->getFromName()); |
||
77 | |||
78 | foreach($message->getTo() as $email => $name) { |
||
79 | $sendGridMail->addTo($email, $name); |
||
80 | } |
||
81 | foreach($message->getCc() as $email => $name) { |
||
82 | $sendGridMail->addCc($email, $name); |
||
83 | } |
||
84 | foreach($message->getBcc() as $email => $name) { |
||
85 | $sendGridMail->addBcc($email, $name); |
||
86 | } |
||
87 | $sendGridMail->setSubject($message->getSubject()); |
||
88 | foreach($message->getHeaders() as $header) { |
||
89 | foreach($header as $key => $value) { |
||
90 | $sendGridMail->addHeader($key, $value); |
||
91 | } |
||
92 | } |
||
93 | foreach($message->getAttachments() as $attachment) { |
||
94 | $cid = isset($attachment['ContentID']) ? $attachment['ContentID'] : null; |
||
95 | $sendGridMail->addAttachment($attachment['File'], $attachment['Name'], $cid); |
||
96 | } |
||
97 | |||
98 | $templateId = $message->getTemplateId(); |
||
99 | |||
100 | if ($templateId === null) { |
||
101 | $data = $message->getHtmlBody(); |
||
102 | if ($data !== null) { |
||
103 | $sendGridMail->addContent('text/html',$data); |
||
104 | } |
||
105 | $data = $message->getTextBody(); |
||
106 | if ($data !== null) { |
||
107 | $sendGridMail->addContent('text/plain',$data); |
||
108 | } |
||
109 | } else { |
||
110 | $sendGridMail->setTemplateId($templateId); |
||
111 | |||
112 | // trigger html template |
||
113 | $sendGridMail->addContent('text/html',' '); |
||
114 | // trigger text template |
||
115 | $sendGridMail->addContent('text/plain',' '); |
||
116 | $templateModel = $message->getTemplateModel(); |
||
117 | |||
118 | if (empty($templateModel) === false) { |
||
119 | $sendGridMail->addDynamicTemplateDatas($templateModel); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | $result = $client->send($sendGridMail); |
||
124 | /* @var \SendGrid\Response $result */ |
||
125 | |||
126 | return [ |
||
127 | 'success' => $result->statusCode()==202, |
||
128 | 'statusCode' => $result->statusCode(), |
||
129 | 'body' => json_decode($result->body()), |
||
130 | 'headers' => $result->headers(), |
||
131 | 'params' => [ |
||
132 | 'from' => $message->getFrom(), |
||
133 | 'to' => $message->getTo(), |
||
134 | 'cc' => $message->getCc(), |
||
135 | 'bcc' => $message->getBcc(), |
||
136 | 'subject' => $message->getSubject(), |
||
137 | 'replyTo' => $message->getReplyTo(), |
||
138 | 'templateId' => $message->getTemplateId(), |
||
139 | 'templateModel' => $message->getTemplateModel(), |
||
140 | 'attachments' => $message->getAttachments(), |
||
141 | 'headers' => $message->getHeaders(), |
||
142 | ], |
||
143 | ]; |
||
144 | } catch (Exception $e) { |
||
145 | Yii::error($e->getMessage(), __METHOD__); |
||
146 | throw $e; |
||
147 | } |
||
149 | } |