Conditions | 15 |
Paths | 115 |
Total Lines | 85 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
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 |
||
21 | public function addImages(ComposerEvent $event) |
||
|
|||
22 | { |
||
23 | $parts = $event->getBody()->getParts(); |
||
24 | |||
25 | $htmlBody = null; |
||
26 | $htmlPart = null; |
||
27 | $textBody = null; |
||
28 | |||
29 | // locate HTML body |
||
30 | foreach ($parts as $part) { |
||
31 | foreach ($part->getHeadersArray() as $header) { |
||
32 | if ($header[0] == 'Content-Type' && strpos($header[1], 'text/html') === 0) { |
||
33 | $htmlPart = $part; |
||
34 | $htmlBody = $part->getRawContent(); |
||
35 | } elseif ($header[0] == 'Content-Type' && strpos($header[1], 'text/plain') === 0) { |
||
36 | $textBody = $part->getRawContent(); |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | |||
41 | if (!$htmlBody) { |
||
42 | // can only work if HTML body exists |
||
43 | return; |
||
44 | } |
||
45 | |||
46 | $doc = new \DOMDocument(); |
||
47 | $doc->loadHTML($htmlBody); |
||
48 | $elements = $doc->getElementsByTagName('img'); |
||
49 | $elements = (array) $elements; |
||
50 | |||
51 | if (count($elements ) > 0) { |
||
52 | $attachement = []; |
||
53 | foreach ($elements as $key => $element) { |
||
54 | // traitement du nom de fichiers pour apprécier le contexte |
||
55 | $file = $element->getAttribute("src"); |
||
56 | $filename = $file; |
||
57 | $pos = strpos($file , "~"); |
||
58 | if ($pos !== false) { |
||
59 | $finUser = strpos($file , "/", $pos); |
||
60 | $filename = $_SERVER["CONTEXT_DOCUMENT_ROOT"].substr($file, $finUser); |
||
61 | } else { |
||
62 | $pos = strpos($file, $_SERVER['HTTP_HOST']); |
||
63 | if ($pos !== false) { |
||
64 | $filename = $_SERVER["DOCUMENT_ROOT"].substr($file, $pos + strlen($_SERVER['HTTP_HOST'])); |
||
65 | } |
||
66 | } |
||
67 | if (is_readable($filename)) { |
||
68 | $at = new MimePart(file_get_contents($filename)); |
||
69 | $at->type = $this->mimeByExtension($filename); |
||
70 | $at->disposition = Mime::DISPOSITION_INLINE; |
||
71 | $at->encoding = Mime::ENCODING_BASE64; |
||
72 | $at->id = 'cid_' . md5_file($filename); |
||
73 | $htmlBody = str_replace($file, 'cid:' . $at->id, $htmlBody); |
||
74 | $attachement[] = $at; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | if (sizeof($attachement) > 0) { |
||
79 | $textPart = new MimePart($textBody); |
||
80 | $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; |
||
81 | $textPart->type = "text/plain; charset=UTF-8"; |
||
82 | |||
83 | $htmlPart = new MimePart($htmlBody); |
||
84 | $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE; |
||
85 | $htmlPart->type = "text/html; charset=UTF-8"; |
||
86 | |||
87 | $content = new MimeMessage(); |
||
88 | $content->addPart($textPart); |
||
89 | $content->addPart($htmlPart); |
||
90 | |||
91 | $contentPart = new Part($content->generateMessage()); |
||
92 | $contentPart->type = "multipart/alternative;\n boundary=\"" . $content->getMime()->boundary() . '"'; |
||
93 | |||
94 | $event->getBody()->setParts([$contentPart]); |
||
95 | |||
96 | foreach ($attachement as $at) { |
||
97 | $event->getBody()->addPart($at); |
||
98 | } |
||
99 | |||
100 | // force multipart/alternative content type |
||
101 | $event->getMessage()->getHeaders()->get('content-type')->setType('multipart/related') |
||
102 | ->addParameter('boundary', $event->getBody()->getMime()->boundary()); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
135 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: