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