Complex classes like Swift_Mime_SimpleMimeEntity 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Swift_Mime_SimpleMimeEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Swift_Mime_SimpleMimeEntity implements Swift_Mime_MimeEntity |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * A collection of Headers for this mime entity |
||
| 20 | * |
||
| 21 | * @var Swift_Mime_HeaderSet |
||
| 22 | */ |
||
| 23 | private $_headers; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The body as a string, or a stream |
||
| 27 | */ |
||
| 28 | private $_body; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The encoder that encodes the body into a streamable format |
||
| 32 | * |
||
| 33 | * @var Swift_Mime_ContentEncoder |
||
| 34 | */ |
||
| 35 | private $_encoder; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Message ID generator |
||
| 39 | * |
||
| 40 | * @var Swift_IdGenerator |
||
| 41 | */ |
||
| 42 | private $_idGenerator; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * A mime boundary, if any is used |
||
| 46 | */ |
||
| 47 | private $_boundary; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Mime types to be used based on the nesting level |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $_compositeRanges = array( |
||
| 55 | 'multipart/mixed' => array(self::LEVEL_TOP, self::LEVEL_MIXED), |
||
| 56 | 'multipart/alternative' => array(self::LEVEL_MIXED, self::LEVEL_ALTERNATIVE), |
||
| 57 | 'multipart/related' => array(self::LEVEL_ALTERNATIVE, self::LEVEL_RELATED), |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A set of filter rules to define what level an entity should be nested at |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $_compoundLevelFilters = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The nesting level of this entity |
||
| 69 | * |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | private $_nestingLevel = self::LEVEL_ALTERNATIVE; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * A KeyCache instance used during encoding and streaming |
||
| 76 | * |
||
| 77 | * @var Swift_KeyCache |
||
| 78 | */ |
||
| 79 | private $_cache; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Direct descendants of this entity |
||
| 83 | * |
||
| 84 | * @var Swift_Mime_MimeEntity[]|array |
||
| 85 | */ |
||
| 86 | private $_immediateChildren = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * All descendants of this entity |
||
| 90 | * |
||
| 91 | * @var Swift_Mime_MimeEntity[] |
||
| 92 | */ |
||
| 93 | private $_children = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The maximum line length of the body of this entity |
||
| 97 | * |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | private $_maxLineLength = 78; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The order in which alternative mime types should appear |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | private $_alternativePartOrder = array( |
||
| 108 | 'text/plain' => 1, |
||
| 109 | 'text/html' => 2, |
||
| 110 | 'multipart/related' => 3, |
||
| 111 | ); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The CID of this entity |
||
| 115 | * |
||
| 116 | * @var string |
||
| 117 | */ |
||
| 118 | private $_id; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * The key used for accessing the cache |
||
| 122 | * |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | private $_cacheKey; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | protected $_userContentType; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Create a new SimpleMimeEntity with $headers, $encoder and $cache. |
||
| 134 | * |
||
| 135 | * @param Swift_Mime_HeaderSet $headers |
||
| 136 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 137 | * @param Swift_KeyCache $cache |
||
| 138 | * @param Swift_IdGenerator $idGenerator |
||
| 139 | */ |
||
| 140 | 538 | public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_IdGenerator $idGenerator) |
|
| 141 | { |
||
| 142 | 538 | $this->_cacheKey = $this->_generateNewCacheKey(); |
|
| 143 | 538 | $this->_cache = $cache; |
|
| 144 | 538 | $this->_headers = $headers; |
|
| 145 | 538 | $this->_idGenerator = $idGenerator; |
|
| 146 | 538 | $this->setEncoder($encoder); |
|
| 147 | 538 | $this->_headers->defineOrdering(array('Content-Type', 'Content-Transfer-Encoding')); |
|
| 148 | |||
| 149 | // This array specifies that, when the entire MIME document contains |
||
| 150 | // $compoundLevel, then for each child within $level, if its Content-Type |
||
| 151 | // is $contentType then it should be treated as if it's level is |
||
| 152 | // $neededLevel instead. I tried to write that unambiguously! :-\ |
||
| 153 | // Data Structure: |
||
| 154 | // array ( |
||
| 155 | // $compoundLevel => array( |
||
| 156 | // $level => array( |
||
| 157 | // $contentType => $neededLevel |
||
| 158 | // ) |
||
| 159 | // ) |
||
| 160 | // ) |
||
| 161 | |||
| 162 | 538 | $this->_compoundLevelFilters = array( |
|
| 163 | 538 | (self::LEVEL_ALTERNATIVE + self::LEVEL_RELATED) => array( |
|
| 164 | 538 | self::LEVEL_ALTERNATIVE => array( |
|
| 165 | 538 | 'text/plain' => self::LEVEL_ALTERNATIVE, |
|
| 166 | 538 | 'text/html' => self::LEVEL_RELATED, |
|
| 167 | 538 | ), |
|
| 168 | 538 | ), |
|
| 169 | ); |
||
| 170 | |||
| 171 | 538 | $this->_id = $this->_idGenerator->generateId(); |
|
| 172 | 538 | } |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Generate a new Content-ID or Message-ID for this MIME entity. |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | 16 | public function generateId() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get the {@link Swift_Mime_HeaderSet} for this entity. |
||
| 188 | * |
||
| 189 | * @return Swift_Mime_HeaderSet |
||
| 190 | */ |
||
| 191 | 399 | public function getHeaders() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Get the nesting level of this entity. |
||
| 198 | * |
||
| 199 | * @see LEVEL_TOP, LEVEL_MIXED, LEVEL_RELATED, LEVEL_ALTERNATIVE |
||
| 200 | * |
||
| 201 | * @return int |
||
| 202 | */ |
||
| 203 | 24 | public function getNestingLevel() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Get the Content-type of this entity. |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 63 | public function getContentType() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Set the Content-type of this entity. |
||
| 220 | * |
||
| 221 | * @param string $type |
||
| 222 | * |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | 492 | public function setContentType($type) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get the CID of this entity. |
||
| 237 | * |
||
| 238 | * The CID will only be present in headers if a Content-ID header is present. |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | 345 | public function getId() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Set the CID of this entity. |
||
| 251 | * |
||
| 252 | * @param string $id |
||
| 253 | * |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | 348 | public function setId($id) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Get the description of this entity. |
||
| 268 | * |
||
| 269 | * This value comes from the Content-Description header if set. |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | 5 | public function getDescription() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Set the description of this entity. |
||
| 280 | * |
||
| 281 | * This method sets a value in the Content-ID header. |
||
| 282 | * |
||
| 283 | * @param string $description |
||
| 284 | * |
||
| 285 | * @return $this |
||
| 286 | */ |
||
| 287 | 15 | public function setDescription($description) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Get the maximum line length of the body of this entity. |
||
| 298 | * |
||
| 299 | * @return int |
||
| 300 | */ |
||
| 301 | 126 | public function getMaxLineLength() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Set the maximum line length of lines in this body. |
||
| 308 | * |
||
| 309 | * Though not enforced by the library, lines should not exceed 1000 chars. |
||
| 310 | * |
||
| 311 | * @param int $length |
||
| 312 | * |
||
| 313 | * @return $this |
||
| 314 | */ |
||
| 315 | 34 | public function setMaxLineLength($length) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get all children added to this entity. |
||
| 324 | * |
||
| 325 | * @return Swift_Mime_MimeEntity[] |
||
| 326 | */ |
||
| 327 | 185 | public function getChildren() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Set all children of this entity. |
||
| 334 | * |
||
| 335 | * @param Swift_Mime_MimeEntity[] $children |
||
| 336 | * @param int $compoundLevel For internal use only |
||
| 337 | * |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | 132 | public function setChildren(array $children, $compoundLevel = null) |
|
| 341 | { |
||
| 342 | // TODO: Try to refactor this logic |
||
| 343 | |||
| 344 | 132 | $compoundLevel = isset($compoundLevel) ? $compoundLevel : $this->_getCompoundLevel($children); |
|
| 345 | 132 | $immediateChildren = array(); |
|
| 346 | 132 | $grandchildren = array(); |
|
| 347 | 132 | $newContentType = $this->_userContentType; |
|
| 348 | |||
| 349 | 132 | foreach ($children as $child) { |
|
| 350 | 118 | $level = $this->_getNeededChildLevel($child, $compoundLevel); |
|
| 351 | 118 | if (empty($immediateChildren)) { |
|
| 352 | // first iteration |
||
| 353 | 118 | $immediateChildren = array($child); |
|
| 354 | 118 | } else { |
|
| 355 | 71 | $nextLevel = $this->_getNeededChildLevel($immediateChildren[0], $compoundLevel); |
|
| 356 | 71 | if ($nextLevel === $level) { |
|
| 357 | 52 | $immediateChildren[] = $child; |
|
| 358 | 71 | } elseif ($level < $nextLevel) { |
|
| 359 | // re-assign immediateChildren to grandchildren |
||
| 360 | 16 | $grandchildren = array_merge($grandchildren, $immediateChildren); |
|
| 361 | // set new children |
||
| 362 | 16 | $immediateChildren = array($child); |
|
| 363 | 16 | } else { |
|
| 364 | 15 | $grandchildren[] = $child; |
|
| 365 | } |
||
| 366 | } |
||
| 367 | 132 | } |
|
| 368 | |||
| 369 | 132 | if ($immediateChildren) { |
|
| 370 | 118 | $lowestLevel = $this->_getNeededChildLevel($immediateChildren[0], $compoundLevel); |
|
| 371 | |||
| 372 | // Determine which composite media type is needed to accommodate the immediate children. |
||
| 373 | 118 | foreach ($this->_compositeRanges as $mediaType => $range) { |
|
| 374 | 118 | if ($lowestLevel > $range[0] && $lowestLevel <= $range[1]) { |
|
| 375 | 92 | $newContentType = $mediaType; |
|
| 376 | |||
| 377 | 92 | break; |
|
| 378 | } |
||
| 379 | 118 | } |
|
| 380 | |||
| 381 | // Put any grandchildren in a sub-part. |
||
| 382 | 118 | if (!empty($grandchildren)) { |
|
| 383 | 24 | $subentity = $this->_createChild(); |
|
| 384 | 24 | $subentity->_setNestingLevel($lowestLevel); |
|
| 385 | 24 | $subentity->setChildren($grandchildren, $compoundLevel); |
|
| 386 | 24 | array_unshift($immediateChildren, $subentity); |
|
| 387 | 24 | } |
|
| 388 | 118 | } |
|
| 389 | |||
| 390 | 132 | $this->_immediateChildren = $immediateChildren; |
|
| 391 | 132 | $this->_children = $children; |
|
| 392 | 132 | $this->_setContentTypeInHeaders($newContentType); |
|
| 393 | 132 | $this->_fixHeaders(); |
|
| 394 | 132 | $this->_sortChildren(); |
|
| 395 | |||
| 396 | 132 | return $this; |
|
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get the body of this entity as a string. |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | 138 | public function getBody() |
|
| 405 | { |
||
| 406 | 138 | return $this->_body instanceof Swift_OutputByteStream ? $this->_readStream($this->_body) : $this->_body; |
|
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Set the body of this entity, either as a string, or as an instance of |
||
| 411 | * {@link Swift_OutputByteStream}. |
||
| 412 | * |
||
| 413 | * @param mixed $body |
||
| 414 | * @param string|null $contentType optional |
||
| 415 | * |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | 254 | public function setBody($body, $contentType = null) |
|
| 419 | { |
||
| 420 | 254 | if ($body !== $this->_body) { |
|
| 421 | 170 | $this->_clearCache(); |
|
| 422 | 170 | } |
|
| 423 | |||
| 424 | 254 | $this->_body = $body; |
|
| 425 | |||
| 426 | 254 | if ($contentType) { |
|
| 427 | 17 | $this->setContentType($contentType); |
|
| 428 | 17 | } |
|
| 429 | |||
| 430 | 254 | return $this; |
|
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get the encoder used for the body of this entity. |
||
| 435 | * |
||
| 436 | * @return Swift_Mime_ContentEncoder |
||
| 437 | */ |
||
| 438 | 30 | public function getEncoder() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Set the encoder used for the body of this entity. |
||
| 445 | * |
||
| 446 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | 538 | public function setEncoder(Swift_Mime_ContentEncoder $encoder) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Get the boundary used to separate children in this entity. |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | 133 | public function getBoundary() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Set the boundary used to separate children in this entity. |
||
| 479 | * |
||
| 480 | * @param string $boundary |
||
| 481 | * |
||
| 482 | * @throws Swift_RfcComplianceException |
||
| 483 | * |
||
| 484 | * @return $this |
||
| 485 | */ |
||
| 486 | 34 | public function setBoundary($boundary) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Receive notification that the charset of this entity, or a parent entity |
||
| 496 | * has changed. |
||
| 497 | * |
||
| 498 | * @param string|null $charset |
||
| 499 | */ |
||
| 500 | 114 | public function charsetChanged($charset) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Receive notification that the encoder of this entity or a parent entity |
||
| 507 | * has changed. |
||
| 508 | * |
||
| 509 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 510 | */ |
||
| 511 | 6 | public function encoderChanged(Swift_Mime_ContentEncoder $encoder) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Get this entire entity as a string. |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | 224 | public function toString() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Get this entire entity as a string. |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | 224 | protected function _bodyToString() |
|
| 535 | { |
||
| 536 | 224 | $string = ''; |
|
| 537 | |||
| 538 | if ( |
||
| 539 | 224 | isset($this->_body) |
|
| 540 | 224 | && |
|
| 541 | empty($this->_immediateChildren) |
||
| 542 | 224 | ) { |
|
| 543 | 107 | if ($this->_cache->hasKey($this->_cacheKey, 'body')) { |
|
| 544 | 7 | $body = $this->_cache->getString($this->_cacheKey, 'body'); |
|
| 545 | 7 | } else { |
|
| 546 | 102 | $body = "\r\n" . $this->_encoder->encodeString($this->getBody(), 0, $this->getMaxLineLength()); |
|
| 547 | 102 | $this->_cache->setString($this->_cacheKey, 'body', $body, Swift_KeyCache::MODE_WRITE); |
|
| 548 | } |
||
| 549 | 107 | $string .= $body; |
|
| 550 | 107 | } |
|
| 551 | |||
| 552 | 224 | if (!empty($this->_immediateChildren)) { |
|
| 553 | 53 | foreach ($this->_immediateChildren as $child) { |
|
| 554 | 53 | $string .= "\r\n\r\n--" . $this->getBoundary() . "\r\n"; |
|
| 555 | 53 | $string .= $child->toString(); |
|
| 556 | 53 | } |
|
| 557 | 53 | $string .= "\r\n\r\n--" . $this->getBoundary() . "--\r\n"; |
|
| 558 | 53 | } |
|
| 559 | |||
| 560 | 224 | return $string; |
|
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Returns a string representation of this object. |
||
| 565 | * |
||
| 566 | * @see toString() |
||
| 567 | * |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function __toString() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Write this entire entity to a {@see Swift_InputByteStream}. |
||
| 577 | * |
||
| 578 | * @param Swift_InputByteStream |
||
| 579 | */ |
||
| 580 | 37 | public function toByteStream(Swift_InputByteStream $is) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Write this entire entity to a {@link Swift_InputByteStream}. |
||
| 590 | * |
||
| 591 | * @param Swift_InputByteStream |
||
| 592 | */ |
||
| 593 | 37 | protected function _bodyToByteStream(Swift_InputByteStream $is) |
|
| 594 | { |
||
| 595 | 37 | if (empty($this->_immediateChildren)) { |
|
| 596 | 37 | if (isset($this->_body)) { |
|
| 597 | 27 | if ($this->_cache->hasKey($this->_cacheKey, 'body')) { |
|
| 598 | 8 | $this->_cache->exportToByteStream($this->_cacheKey, 'body', $is); |
|
| 599 | 8 | } else { |
|
| 600 | 22 | $cacheIs = $this->_cache->getInputByteStream($this->_cacheKey, 'body'); |
|
| 601 | 22 | if ($cacheIs) { |
|
| 602 | 17 | $is->bind($cacheIs); |
|
| 603 | 17 | } |
|
| 604 | |||
| 605 | 22 | $is->write("\r\n"); |
|
| 606 | |||
| 607 | 22 | if ($this->_body instanceof Swift_OutputByteStream) { |
|
| 608 | 14 | $this->_body->setReadPointer(0); |
|
| 609 | |||
| 610 | 14 | $this->_encoder->encodeByteStream($this->_body, $is, 0, $this->getMaxLineLength()); |
|
| 611 | 14 | } else { |
|
| 612 | 22 | $is->write($this->_encoder->encodeString($this->getBody(), 0, $this->getMaxLineLength())); |
|
| 613 | } |
||
| 614 | |||
| 615 | 22 | if ($cacheIs) { |
|
| 616 | 17 | $is->unbind($cacheIs); |
|
| 617 | 17 | } |
|
| 618 | } |
||
| 619 | 27 | } |
|
| 620 | 37 | } |
|
| 621 | |||
| 622 | 37 | if (!empty($this->_immediateChildren)) { |
|
| 623 | 9 | foreach ($this->_immediateChildren as $child) { |
|
| 624 | 9 | $is->write("\r\n\r\n--" . $this->getBoundary() . "\r\n"); |
|
| 625 | 9 | $child->toByteStream($is); |
|
| 626 | 9 | } |
|
| 627 | 9 | $is->write("\r\n\r\n--" . $this->getBoundary() . "--\r\n"); |
|
| 628 | 9 | } |
|
| 629 | 37 | } |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Get the name of the header that provides the ID of this entity. |
||
| 633 | */ |
||
| 634 | 110 | protected function _getIdField() |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Get the model data (usually an array or a string) for $field. |
||
| 641 | * |
||
| 642 | * @param string $field |
||
| 643 | * |
||
| 644 | * @return null|string[]|string |
||
| 645 | */ |
||
| 646 | 355 | protected function _getHeaderFieldModel($field) |
|
| 647 | { |
||
| 648 | 355 | if ($this->_headers->has($field)) { |
|
| 649 | 188 | return $this->_headers->get($field)->getFieldBodyModel(); |
|
| 650 | } |
||
| 651 | |||
| 652 | 338 | return null; |
|
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Set the model data for $field. |
||
| 657 | * |
||
| 658 | * @param string $field |
||
| 659 | * @param $model |
||
| 660 | * |
||
| 661 | * @return bool |
||
| 662 | */ |
||
| 663 | 538 | protected function _setHeaderFieldModel($field, $model) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Get the parameter value of $parameter on $field header. |
||
| 676 | * @param string $field |
||
| 677 | * @param string $parameter |
||
| 678 | * |
||
| 679 | * @return string|false |
||
| 680 | */ |
||
| 681 | 120 | protected function _getHeaderParameter($field, $parameter) |
|
| 682 | { |
||
| 683 | 120 | if ($this->_headers->has($field)) { |
|
| 684 | 92 | return $this->_headers->get($field)->getParameter($parameter); |
|
| 685 | } |
||
| 686 | |||
| 687 | 28 | return false; |
|
| 688 | } |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Set the parameter value of $parameter on $field header. |
||
| 692 | * @param string $field |
||
| 693 | * @param string $parameter |
||
| 694 | * @param mixed $value |
||
| 695 | * |
||
| 696 | * @return bool |
||
| 697 | */ |
||
| 698 | 279 | protected function _setHeaderParameter($field, $parameter, $value) |
|
| 699 | { |
||
| 700 | 279 | if ($this->_headers->has($field)) { |
|
| 701 | 225 | $this->_headers->get($field)->setParameter($parameter, $value); |
|
| 702 | |||
| 703 | 225 | return true; |
|
| 704 | } |
||
| 705 | |||
| 706 | 60 | return false; |
|
| 707 | } |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Re-evaluate what content type and encoding should be used on this entity. |
||
| 711 | */ |
||
| 712 | 132 | protected function _fixHeaders() |
|
| 713 | { |
||
| 714 | 132 | if (count($this->_immediateChildren)) { |
|
| 715 | 118 | $this->_setHeaderParameter('Content-Type', 'boundary', $this->getBoundary()); |
|
| 716 | 118 | $this->_headers->remove('Content-Transfer-Encoding'); |
|
| 717 | 118 | } else { |
|
| 718 | 25 | $this->_setHeaderParameter('Content-Type', 'boundary', null); |
|
| 719 | 25 | $this->_setEncoding($this->_encoder->getName()); |
|
| 720 | } |
||
| 721 | 132 | } |
|
| 722 | |||
| 723 | /** |
||
| 724 | * Get the KeyCache used in this entity. |
||
| 725 | * |
||
| 726 | * @return Swift_KeyCache |
||
| 727 | */ |
||
| 728 | 19 | protected function _getCache() |
|
| 729 | { |
||
| 730 | 19 | return $this->_cache; |
|
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * @return Swift_IdGenerator |
||
| 735 | */ |
||
| 736 | 19 | protected function _getIdGenerator() |
|
| 737 | { |
||
| 738 | 19 | return $this->_idGenerator; |
|
| 739 | } |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Empty the KeyCache for this entity. |
||
| 743 | */ |
||
| 744 | 538 | protected function _clearCache() |
|
| 748 | |||
| 749 | /** |
||
| 750 | * generate a new cache-key |
||
| 751 | * |
||
| 752 | * @return string |
||
| 753 | */ |
||
| 754 | 538 | private function _generateNewCacheKey() |
|
| 755 | { |
||
| 756 | 538 | return md5(getmypid() . '.' . time() . '.' . uniqid(mt_rand(), true)); |
|
| 757 | } |
||
| 758 | |||
| 759 | /** |
||
| 760 | * read stream |
||
| 761 | * |
||
| 762 | * @param Swift_OutputByteStream $os |
||
| 763 | * |
||
| 764 | * @return string |
||
| 765 | */ |
||
| 766 | 14 | private function _readStream(Swift_OutputByteStream $os) |
|
| 767 | { |
||
| 768 | 14 | $string = ''; |
|
| 769 | 14 | while (false !== $bytes = $os->read(8192)) { |
|
| 770 | 14 | $string .= $bytes; |
|
| 771 | 14 | } |
|
| 772 | |||
| 773 | 14 | $os->setReadPointer(0); |
|
| 774 | |||
| 775 | 14 | return $string; |
|
| 776 | } |
||
| 777 | |||
| 778 | /** |
||
| 779 | * @param string $encoding |
||
| 780 | */ |
||
| 781 | 538 | private function _setEncoding($encoding) |
|
| 782 | { |
||
| 783 | 538 | if (!$this->_setHeaderFieldModel('Content-Transfer-Encoding', $encoding)) { |
|
| 784 | 533 | $this->_headers->addTextHeader('Content-Transfer-Encoding', $encoding); |
|
| 785 | 533 | } |
|
| 786 | 538 | } |
|
| 787 | |||
| 788 | /** |
||
| 789 | * @param string $boundary |
||
| 790 | * |
||
| 791 | * @throws Swift_RfcComplianceException |
||
| 792 | */ |
||
| 793 | 34 | private function _assertValidBoundary($boundary) |
|
| 794 | { |
||
| 795 | 34 | if (!preg_match('/^[a-z0-9\'\(\)\+_\-,\.\/:=\?\ ]{0,69}[a-z0-9\'\(\)\+_\-,\.\/:=\?]$/Di', $boundary)) { |
|
| 796 | throw new Swift_RfcComplianceException('Mime boundary set is not RFC 2046 compliant.'); |
||
| 797 | } |
||
| 798 | 34 | } |
|
| 799 | |||
| 800 | /** |
||
| 801 | * @param string $type |
||
| 802 | */ |
||
| 803 | 504 | private function _setContentTypeInHeaders($type) |
|
| 804 | { |
||
| 805 | 504 | if (!$this->_setHeaderFieldModel('Content-Type', $type)) { |
|
| 806 | 443 | $this->_headers->addParameterizedHeader('Content-Type', $type); |
|
| 807 | 443 | } |
|
| 808 | 504 | } |
|
| 809 | |||
| 810 | /** |
||
| 811 | * @param int $level |
||
| 812 | */ |
||
| 813 | 24 | private function _setNestingLevel($level) |
|
| 814 | { |
||
| 815 | 24 | $this->_nestingLevel = $level; |
|
| 816 | 24 | } |
|
| 817 | |||
| 818 | /** |
||
| 819 | * @param Swift_Mime_MimeEntity[] $children |
||
| 820 | * |
||
| 821 | * @return int |
||
| 822 | */ |
||
| 823 | 132 | private function _getCompoundLevel($children) |
|
| 824 | { |
||
| 825 | 132 | $level = 0; |
|
| 826 | 132 | foreach ($children as $child) { |
|
| 827 | 118 | $level |= $child->getNestingLevel(); |
|
| 828 | 132 | } |
|
| 829 | |||
| 830 | 132 | return $level; |
|
| 831 | } |
||
| 832 | |||
| 833 | /** |
||
| 834 | * @param Swift_Mime_MimeEntity $child |
||
| 835 | * @param integer $compoundLevel |
||
| 836 | * |
||
| 837 | * @return int |
||
| 838 | */ |
||
| 839 | 118 | private function _getNeededChildLevel($child, $compoundLevel) |
|
| 840 | { |
||
| 841 | 118 | $filter = array(); |
|
| 842 | 118 | foreach ($this->_compoundLevelFilters as $bitmask => $rules) { |
|
| 843 | 118 | if (($compoundLevel & $bitmask) === $bitmask) { |
|
| 844 | 15 | $filter = $rules + $filter; |
|
| 845 | 15 | } |
|
| 846 | 118 | } |
|
| 847 | |||
| 848 | 118 | $realLevel = $child->getNestingLevel(); |
|
| 849 | 118 | $lowercaseType = Swift::strtolowerWithStaticCache((string)$child->getContentType()); |
|
| 850 | |||
| 851 | 118 | if (isset($filter[$realLevel], $filter[$realLevel][$lowercaseType])) { |
|
| 852 | 10 | return (int)$filter[$realLevel][$lowercaseType]; |
|
| 853 | } |
||
| 854 | |||
| 855 | 118 | return (int)$realLevel; |
|
| 856 | } |
||
| 857 | |||
| 858 | /** |
||
| 859 | * @return $this |
||
| 860 | */ |
||
| 861 | 24 | private function _createChild() |
|
| 862 | { |
||
| 863 | 24 | return new self($this->_headers->newInstance(), $this->_encoder, $this->_cache, $this->_idGenerator); |
|
| 864 | } |
||
| 865 | |||
| 866 | /** |
||
| 867 | * @param Swift_Mime_ContentEncoder $encoder |
||
| 868 | */ |
||
| 869 | 538 | private function _notifyEncoderChanged(Swift_Mime_ContentEncoder $encoder) |
|
| 870 | { |
||
| 871 | 538 | foreach ($this->_immediateChildren as $child) { |
|
| 872 | 11 | $child->encoderChanged($encoder); |
|
| 873 | 538 | } |
|
| 874 | 538 | } |
|
| 875 | |||
| 876 | /** |
||
| 877 | * @param string $charset |
||
| 878 | */ |
||
| 879 | 114 | private function _notifyCharsetChanged($charset) |
|
| 880 | { |
||
| 881 | 114 | $this->_encoder->charsetChanged($charset); |
|
| 882 | 114 | $this->_headers->charsetChanged($charset); |
|
| 883 | 114 | foreach ($this->_immediateChildren as $child) { |
|
| 884 | 7 | $child->charsetChanged($charset); |
|
| 885 | 114 | } |
|
| 886 | 114 | } |
|
| 887 | |||
| 888 | 132 | private function _sortChildren() |
|
| 889 | { |
||
| 890 | 132 | $shouldSort = false; |
|
| 891 | 132 | foreach ($this->_immediateChildren as $child) { |
|
| 892 | // NOTE: This include alternative parts moved into a related part |
||
| 893 | 118 | if ($child->getNestingLevel() === self::LEVEL_ALTERNATIVE) { |
|
| 894 | 66 | $shouldSort = true; |
|
| 895 | 66 | break; |
|
| 896 | } |
||
| 897 | 132 | } |
|
| 898 | |||
| 899 | // Sort in order of preference, if there is one |
||
| 900 | 132 | if ($shouldSort) { |
|
| 901 | // Group the messages by order of preference |
||
| 902 | 66 | $sorted = array(); |
|
| 903 | 66 | foreach ($this->_immediateChildren as $child) { |
|
| 904 | 66 | $type = $child->getContentType(); |
|
| 905 | 66 | $level = array_key_exists($type, $this->_alternativePartOrder) ? |
|
| 906 | 66 | $this->_alternativePartOrder[$type] : |
|
| 907 | 66 | max($this->_alternativePartOrder) + 1; |
|
| 908 | |||
| 909 | 66 | if (empty($sorted[$level])) { |
|
| 910 | 66 | $sorted[$level] = array(); |
|
| 911 | 66 | } |
|
| 912 | |||
| 913 | 66 | $sorted[$level][] = $child; |
|
| 914 | 66 | } |
|
| 915 | |||
| 916 | 66 | ksort($sorted); |
|
| 917 | |||
| 918 | 66 | $this->_immediateChildren = array_reduce($sorted, 'array_merge', array()); |
|
| 919 | 66 | } |
|
| 920 | 132 | } |
|
| 921 | |||
| 922 | /** |
||
| 923 | * Empties it's own contents from the cache. |
||
| 924 | */ |
||
| 925 | 536 | public function __destruct() |
|
| 926 | { |
||
| 927 | 536 | if ($this->_cache instanceof Swift_KeyCache) { |
|
| 928 | 536 | $this->_cache->clearAll($this->_cacheKey); |
|
| 929 | 536 | } |
|
| 930 | 536 | } |
|
| 931 | |||
| 932 | /** |
||
| 933 | * Make a deep copy of object. |
||
| 934 | */ |
||
| 935 | 5 | public function __clone() |
|
| 954 | } |
||
| 955 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.