Total Complexity | 55 |
Total Lines | 399 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like MultipartHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MultipartHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class MultipartHelper extends AbstractHelper |
||
25 | { |
||
26 | /** |
||
27 | * @var GenericHelper a GenericHelper instance |
||
28 | */ |
||
29 | private $genericHelper; |
||
30 | |||
31 | public function __construct( |
||
32 | IMimePartFactory $mimePartFactory, |
||
33 | IUUEncodedPartFactory $uuEncodedPartFactory, |
||
34 | GenericHelper $genericHelper |
||
35 | ) { |
||
36 | parent::__construct($mimePartFactory, $uuEncodedPartFactory); |
||
37 | $this->genericHelper = $genericHelper; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Creates and returns a unique boundary. |
||
42 | * |
||
43 | * @param string $mimeType first 3 characters of a multipart type are used, |
||
44 | * e.g. REL for relative or ALT for alternative |
||
45 | * @return string |
||
46 | */ |
||
47 | public function getUniqueBoundary($mimeType) |
||
48 | { |
||
49 | $type = ltrim(strtoupper(preg_replace('/^(multipart\/(.{3}).*|.*)$/i', '$2-', $mimeType)), '-'); |
||
50 | return uniqid('----=MMP-' . $type . '-', true); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Creates a unique mime boundary and assigns it to the passed part's |
||
55 | * Content-Type header with the passed mime type. |
||
56 | * |
||
57 | * @param IMimePart $part |
||
58 | * @param string $mimeType |
||
59 | */ |
||
60 | public function setMimeHeaderBoundaryOnPart(IMimePart $part, $mimeType) |
||
61 | { |
||
62 | $part->setRawHeader( |
||
63 | HeaderConsts::CONTENT_TYPE, |
||
64 | "$mimeType;\r\n\tboundary=\"" |
||
65 | . $this->getUniqueBoundary($mimeType) . '"' |
||
66 | ); |
||
67 | $part->notify(); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Sets the passed message as multipart/mixed. |
||
72 | * |
||
73 | * If the message has content, a new part is created and added as a child of |
||
74 | * the message. The message's content and content headers are moved to the |
||
75 | * new part. |
||
76 | * |
||
77 | * @param IMessage $message |
||
78 | */ |
||
79 | public function setMessageAsMixed(IMessage $message) |
||
80 | { |
||
81 | if ($message->hasContent()) { |
||
82 | $part = $this->genericHelper->createNewContentPartFrom($message); |
||
83 | $message->addChild($part, 0); |
||
84 | } |
||
85 | $this->setMimeHeaderBoundaryOnPart($message, 'multipart/mixed'); |
||
86 | $atts = $message->getAllAttachmentParts(); |
||
87 | if (!empty($atts)) { |
||
88 | foreach ($atts as $att) { |
||
89 | $att->notify(); |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Sets the passed message as multipart/alternative. |
||
96 | * |
||
97 | * If the message has content, a new part is created and added as a child of |
||
98 | * the message. The message's content and content headers are moved to the |
||
99 | * new part. |
||
100 | * |
||
101 | * @param IMessage $message |
||
102 | */ |
||
103 | public function setMessageAsAlternative(IMessage $message) |
||
104 | { |
||
105 | if ($message->hasContent()) { |
||
106 | $part = $this->genericHelper->createNewContentPartFrom($message); |
||
107 | $message->addChild($part, 0); |
||
108 | } |
||
109 | $this->setMimeHeaderBoundaryOnPart($message, 'multipart/alternative'); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Searches the passed $alternativePart for a part with the passed mime type |
||
114 | * and returns its parent. |
||
115 | * |
||
116 | * Used for alternative mime types that have a multipart/mixed or |
||
117 | * multipart/related child containing a content part of $mimeType, where |
||
118 | * the whole mixed/related part should be removed. |
||
119 | * |
||
120 | * @param string $mimeType the content-type to find below $alternativePart |
||
121 | * @param IMimePart $alternativePart The multipart/alternative part to look |
||
122 | * under |
||
123 | * @return boolean|IMimePart false if a part is not found |
||
124 | */ |
||
125 | public function getContentPartContainerFromAlternative($mimeType, IMimePart $alternativePart) |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Removes all parts of $mimeType from $alternativePart. |
||
141 | * |
||
142 | * If $alternativePart contains a multipart/mixed or multipart/relative part |
||
143 | * with other parts of different content-types, the multipart part is |
||
144 | * removed, and parts of different content-types can optionally be moved to |
||
145 | * the main message part. |
||
146 | * |
||
147 | * @param IMessage $message |
||
148 | * @param string $mimeType |
||
149 | * @param IMimePart $alternativePart |
||
150 | * @param bool $keepOtherContent |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function removeAllContentPartsFromAlternative(IMessage $message, $mimeType, IMimePart $alternativePart, $keepOtherContent) |
||
154 | { |
||
155 | $rmPart = $this->getContentPartContainerFromAlternative($mimeType, $alternativePart); |
||
156 | if ($rmPart === false) { |
||
157 | return false; |
||
158 | } |
||
159 | if ($keepOtherContent && $rmPart->getChildCount() > 0) { |
||
160 | $this->moveAllNonMultiPartsToMessageExcept($message, $rmPart, $mimeType); |
||
161 | $alternativePart = $message->getPart(0, PartFilter::fromInlineContentType('multipart/alternative')); |
||
162 | } |
||
163 | $message->removePart($rmPart); |
||
164 | if ($alternativePart !== null) { |
||
165 | if ($alternativePart->getChildCount() === 1) { |
||
166 | $this->genericHelper->replacePart($message, $alternativePart, $alternativePart->getChild(0)); |
||
167 | } elseif ($alternativePart->getChildCount() === 0) { |
||
168 | $message->removePart($alternativePart); |
||
169 | } |
||
170 | } |
||
171 | while ($message->getChildCount() === 1) { |
||
172 | $this->genericHelper->replacePart($message, $message, $message->getChild(0)); |
||
173 | } |
||
174 | return true; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Creates a new mime part as a multipart/alternative and assigns the passed |
||
179 | * $contentPart as a part below it before returning it. |
||
180 | * |
||
181 | * @param IMessage $message |
||
182 | * @param IMessagePart $contentPart |
||
183 | * @return IMimePart the alternative part |
||
184 | */ |
||
185 | public function createAlternativeContentPart(IMessage $message, IMessagePart $contentPart) |
||
186 | { |
||
187 | $altPart = $this->mimePartFactory->newInstance(); |
||
188 | $this->setMimeHeaderBoundaryOnPart($altPart, 'multipart/alternative'); |
||
189 | $message->removePart($contentPart); |
||
190 | $message->addChild($altPart, 0); |
||
191 | $altPart->addChild($contentPart, 0); |
||
192 | return $altPart; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Moves all parts under $from into this message except those with a |
||
197 | * content-type equal to $exceptMimeType. If the message is not a |
||
198 | * multipart/mixed message, it is set to multipart/mixed first. |
||
199 | * |
||
200 | * @param IMessage $message |
||
201 | * @param IMimePart $from |
||
202 | * @param string $exceptMimeType |
||
203 | */ |
||
204 | public function moveAllNonMultiPartsToMessageExcept(IMessage $message, IMimePart $from, $exceptMimeType) |
||
205 | { |
||
206 | $parts = $from->getAllParts(function(IMessagePart $part) use ($exceptMimeType) { |
||
207 | if ($part instanceof IMimePart && $part->isMultiPart()) { |
||
208 | return false; |
||
209 | } |
||
210 | return strcasecmp($part->getContentType(), $exceptMimeType) !== 0; |
||
211 | }); |
||
212 | if (strcasecmp($message->getContentType(), 'multipart/mixed') !== 0) { |
||
213 | $this->setMessageAsMixed($message); |
||
214 | } |
||
215 | foreach ($parts as $key => $part) { |
||
216 | $from->removePart($part); |
||
217 | $message->addChild($part); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Enforces the message to be a mime message for a non-mime (e.g. uuencoded |
||
223 | * or unspecified) message. If the message has uuencoded attachments, sets |
||
224 | * up the message as a multipart/mixed message and creates a separate |
||
225 | * content part. |
||
226 | * |
||
227 | * @param IMessage $message |
||
228 | */ |
||
229 | public function enforceMime(IMessage $message) |
||
230 | { |
||
231 | if (!$message->isMime()) { |
||
232 | if ($message->getAttachmentCount()) { |
||
233 | $this->setMessageAsMixed($message); |
||
234 | } else { |
||
235 | $message->setRawHeader(HeaderConsts::CONTENT_TYPE, "text/plain;\r\n\tcharset=\"iso-8859-1\""); |
||
236 | } |
||
237 | $message->setRawHeader(HeaderConsts::MIME_VERSION, '1.0'); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Creates a multipart/related part out of 'inline' children of $parent and |
||
243 | * returns it. |
||
244 | * |
||
245 | * @param IMimePart $parent |
||
246 | * @return IMimePart |
||
247 | */ |
||
248 | public function createMultipartRelatedPartForInlineChildrenOf(IMimePart $parent) |
||
249 | { |
||
250 | $relatedPart = $this->mimePartFactory->newInstance(); |
||
251 | $this->setMimeHeaderBoundaryOnPart($relatedPart, 'multipart/related'); |
||
252 | foreach ($parent->getChildParts(PartFilter::fromDisposition('inline')) as $part) { |
||
253 | $parent->removePart($part); |
||
254 | $relatedPart->addChild($part); |
||
255 | } |
||
256 | $parent->addChild($relatedPart, 0); |
||
257 | return $relatedPart; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Finds an alternative inline part in the message and returns it if one |
||
262 | * exists. |
||
263 | * |
||
264 | * If the passed $mimeType is text/plain, searches for a text/html part. |
||
265 | * Otherwise searches for a text/plain part to return. |
||
266 | * |
||
267 | * @param IMessage $message |
||
268 | * @param string $mimeType |
||
269 | * @return \ZBateson\MailMimeParser\Message\MimeType or null if not |
||
270 | * found |
||
271 | */ |
||
272 | public function findOtherContentPartFor(IMessage $message, $mimeType) |
||
273 | { |
||
274 | $altPart = $message->getPart( |
||
275 | 0, |
||
276 | PartFilter::fromInlineContentType(($mimeType === 'text/plain') ? 'text/html' : 'text/plain') |
||
277 | ); |
||
278 | if ($altPart !== null && $altPart->getParent() !== null && $altPart->getParent()->isMultiPart()) { |
||
279 | $altPartParent = $altPart->getParent(); |
||
280 | if ($altPartParent->getChildCount(PartFilter::fromDisposition('inline')) !== 1) { |
||
281 | $altPart = $this->createMultipartRelatedPartForInlineChildrenOf($altPartParent); |
||
282 | } |
||
283 | } |
||
284 | return $altPart; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Creates a new content part for the passed mimeType and charset, making |
||
289 | * space by creating a multipart/alternative if needed |
||
290 | * |
||
291 | * @param IMessage $message |
||
292 | * @param string $mimeType |
||
293 | * @param string $charset |
||
294 | * @return \ZBateson\MailMimeParser\Message\IMimePart |
||
295 | */ |
||
296 | public function createContentPartForMimeType(IMessage $message, $mimeType, $charset) |
||
297 | { |
||
298 | $mimePart = $this->mimePartFactory->newInstance(); |
||
299 | $mimePart->setRawHeader(HeaderConsts::CONTENT_TYPE, "$mimeType;\r\n\tcharset=\"$charset\""); |
||
300 | $mimePart->setRawHeader(HeaderConsts::CONTENT_TRANSFER_ENCODING, 'quoted-printable'); |
||
301 | |||
302 | $this->enforceMime($message); |
||
303 | $altPart = $this->findOtherContentPartFor($message, $mimeType); |
||
304 | |||
305 | if ($altPart === $message) { |
||
306 | $this->setMessageAsAlternative($message); |
||
307 | $message->addChild($mimePart); |
||
308 | } elseif ($altPart !== null) { |
||
309 | $mimeAltPart = $this->createAlternativeContentPart($message, $altPart); |
||
310 | $mimeAltPart->addChild($mimePart, 1); |
||
311 | } else { |
||
312 | $message->addChild($mimePart, 0); |
||
313 | } |
||
314 | |||
315 | return $mimePart; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Creates and adds a IMimePart for the passed content and options as an |
||
320 | * attachment. |
||
321 | * |
||
322 | * @param IMessage $message |
||
323 | * @param string|resource|Psr\Http\Message\StreamInterface\StreamInterface |
||
324 | * $resource |
||
325 | * @param string $mimeType |
||
326 | * @param string $disposition |
||
327 | * @param string $filename |
||
328 | * @param string $encoding |
||
329 | * @return \ZBateson\MailMimeParser\Message\IMimePart |
||
330 | */ |
||
331 | public function createAndAddPartForAttachment(IMessage $message, $resource, $mimeType, $disposition, $filename = null, $encoding = 'base64') |
||
332 | { |
||
333 | if ($filename === null) { |
||
334 | $filename = 'file' . uniqid(); |
||
335 | } |
||
336 | |||
337 | $safe = iconv('UTF-8', 'US-ASCII//translit//ignore', $filename); |
||
338 | if ($message->isMime()) { |
||
339 | $part = $this->mimePartFactory->newInstance(); |
||
340 | $part->setRawHeader(HeaderConsts::CONTENT_TRANSFER_ENCODING, $encoding); |
||
341 | if (strcasecmp($message->getContentType(), 'multipart/mixed') !== 0) { |
||
342 | $this->setMessageAsMixed($message); |
||
343 | } |
||
344 | $part->setRawHeader(HeaderConsts::CONTENT_TYPE, "$mimeType;\r\n\tname=\"$safe\""); |
||
345 | $part->setRawHeader(HeaderConsts::CONTENT_DISPOSITION, "$disposition;\r\n\tfilename=\"$safe\""); |
||
346 | } else { |
||
347 | $part = $this->uuEncodedPartFactory->newInstance(); |
||
348 | $part->setFilename($safe); |
||
349 | } |
||
350 | $part->setContent($resource); |
||
351 | $message->addChild($part); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Removes the content part of the message with the passed mime type. If |
||
356 | * there is a remaining content part and it is an alternative part of the |
||
357 | * main message, the content part is moved to the message part. |
||
358 | * |
||
359 | * If the content part is part of an alternative part beneath the message, |
||
360 | * the alternative part is replaced by the remaining content part, |
||
361 | * optionally keeping other parts if $keepOtherContent is set to true. |
||
362 | * |
||
363 | * @param IMessage $message |
||
364 | * @param string $mimeType |
||
365 | * @param bool $keepOtherContent |
||
366 | * @return boolean true on success |
||
367 | */ |
||
368 | public function removeAllContentPartsByMimeType(IMessage $message, $mimeType, $keepOtherContent = false) |
||
369 | { |
||
370 | $alt = $message->getPart(0, PartFilter::fromInlineContentType('multipart/alternative')); |
||
371 | if ($alt !== null) { |
||
372 | return $this->removeAllContentPartsFromAlternative($message, $mimeType, $alt, $keepOtherContent); |
||
373 | } |
||
374 | $message->removeAllParts(PartFilter::fromInlineContentType($mimeType)); |
||
375 | return true; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Removes the 'inline' part with the passed contentType, at the given index |
||
380 | * defaulting to the first |
||
381 | * |
||
382 | * @param IMessage $message |
||
383 | * @param string $mimeType |
||
384 | * @param int $index |
||
385 | * @return boolean true on success |
||
386 | */ |
||
387 | public function removePartByMimeType(IMessage $message, $mimeType, $index = 0) |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Either creates a mime part or sets the existing mime part with the passed |
||
406 | * mimeType to $strongOrHandle. |
||
407 | * |
||
408 | * @param IMessage $message |
||
409 | * @param string $mimeType |
||
410 | * @param string|resource $stringOrHandle |
||
411 | * @param string $charset |
||
412 | */ |
||
413 | public function setContentPartForMimeType(IMessage $message, $mimeType, $stringOrHandle, $charset) |
||
423 | } |
||
424 | } |
||
425 |