Conditions | 11 |
Paths | 144 |
Total Lines | 56 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
15 | public function send($email) |
||
16 | { |
||
17 | // Detect body type |
||
18 | $htmlContent = null; |
||
19 | $plainContent = null; |
||
20 | if ($email->getSwiftMessage()->getContentType() === 'text/plain') { |
||
21 | $type = 'plain'; |
||
22 | $plainContent = $email->getBody(); |
||
23 | } else { |
||
24 | $type = 'html'; |
||
25 | $htmlContent = $email->getBody(); |
||
26 | $plainPart = $email->findPlainPart(); |
||
27 | if ($plainPart) { |
||
28 | $plainContent = $plainPart->getBody(); |
||
29 | } |
||
30 | } |
||
31 | |||
32 | // Get attachments |
||
33 | $attachedFiles = []; |
||
34 | foreach ($email->getSwiftMessage()->getChildren() as $child) { |
||
35 | if ($child instanceof Swift_Attachment) { |
||
36 | $attachedFiles[] = [ |
||
37 | 'contents' => $child->getBody(), |
||
38 | 'filename' => $child->getFilename(), |
||
39 | 'mimetype' => $child->getContentType(), |
||
40 | ]; |
||
41 | } |
||
42 | } |
||
43 | |||
44 | $headers = $email->getSwiftMessage()->getHeaders(); |
||
45 | $cc = $headers->get('CC') ? $headers->get('CC')->getFieldBody() : ''; |
||
46 | $bcc = $headers->get('BCC') ? $headers->get('BCC')->getFieldBody() : ''; |
||
47 | |||
48 | // Serialise email |
||
49 | $serialised = [ |
||
50 | 'Type' => $type, |
||
51 | 'To' => implode(';', array_keys($email->getTo() ?: [])), |
||
52 | 'From' => implode(';', array_keys($email->getFrom() ?: [])), |
||
53 | 'Subject' => $email->getSubject(), |
||
54 | 'Content' => $email->getBody(), |
||
55 | 'AttachedFiles' => $attachedFiles, |
||
56 | 'Headers' => [ |
||
57 | 'Cc' => $cc, |
||
58 | 'Bcc' => $bcc, |
||
59 | ], |
||
60 | ]; |
||
61 | if ($plainContent) { |
||
62 | $serialised['PlainContent'] = $plainContent; |
||
63 | } |
||
64 | if ($htmlContent) { |
||
65 | $serialised['HtmlContent'] = $htmlContent; |
||
66 | } |
||
67 | |||
68 | $this->saveEmail($serialised); |
||
69 | |||
70 | return true; |
||
71 | } |
||
148 |