| Total Complexity | 40 |
| Total Lines | 196 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MessagePart 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 MessagePart, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class MessagePart implements IMessagePart |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var IMimePart parent part |
||
| 26 | */ |
||
| 27 | protected $parent; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var PartStreamContainer holds 'stream' and 'contentStream'. |
||
| 31 | */ |
||
| 32 | protected $partStreamContainer; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string can be used to set an override for content's charset in cases |
||
| 36 | * where a user knows the charset on the content is not what it claims |
||
| 37 | * to be. |
||
| 38 | */ |
||
| 39 | protected $charsetOverride; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool set to true when a user attaches a stream manually, it's |
||
| 43 | * assumed to already be decoded or to have relevant transfer encoding |
||
| 44 | * decorators attached already. |
||
| 45 | */ |
||
| 46 | protected $ignoreTransferEncoding; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * SplObjectStorage attached observers |
||
| 50 | */ |
||
| 51 | protected $observers; |
||
| 52 | |||
| 53 | public function __construct(PartStreamContainer $streamContainer, IMimePart $parent = null) |
||
| 54 | { |
||
| 55 | $this->partStreamContainer = $streamContainer; |
||
| 56 | $this->parent = $parent; |
||
| 57 | $this->observers = new SplObjectStorage(); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function attach(SplObserver $observer) |
||
| 61 | { |
||
| 62 | $this->observers->attach($observer); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function detach(SplObserver $observer) |
||
| 66 | { |
||
| 67 | $this->observers->detach($observer); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function notify() |
||
| 71 | { |
||
| 72 | foreach ($this->observers as $observer) { |
||
| 73 | $observer->update($this); |
||
| 74 | } |
||
| 75 | if ($this->parent !== null) { |
||
| 76 | $this->parent->notify(); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | public function hasContent() |
||
| 81 | { |
||
| 82 | return $this->partStreamContainer->hasContent(); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function getFilename() |
||
| 86 | { |
||
| 87 | return null; |
||
| 88 | } |
||
| 89 | |||
| 90 | public function getResourceHandle() |
||
| 91 | { |
||
| 92 | return StreamWrapper::getResource($this->getStream()); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function getStream() |
||
| 96 | { |
||
| 97 | return $this->partStreamContainer->getStream(); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function setCharsetOverride($charsetOverride, $onlyIfNoCharset = false) |
||
| 101 | { |
||
| 102 | if (!$onlyIfNoCharset || $this->getCharset() === null) { |
||
| 103 | $this->charsetOverride = $charsetOverride; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | public function getContentStream($charset = MailMimeParser::DEFAULT_CHARSET) |
||
| 108 | { |
||
| 109 | if ($this->hasContent()) { |
||
| 110 | $tr = ($this->ignoreTransferEncoding) ? '' : $this->getContentTransferEncoding(); |
||
| 111 | $ch = ($this->charsetOverride !== null) ? $this->charsetOverride : $this->getCharset(); |
||
| 112 | return $this->partStreamContainer->getContentStream( |
||
| 113 | $tr, |
||
| 114 | $ch, |
||
| 115 | $charset |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | return null; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function getBinaryContentStream() |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getBinaryContentResourceHandle() |
||
| 131 | { |
||
| 132 | $stream = $this->getBinaryContentStream(); |
||
| 133 | if ($stream !== null) { |
||
| 134 | return StreamWrapper::getResource($stream); |
||
| 135 | } |
||
| 136 | return null; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function saveContent($filenameResourceOrStream) |
||
| 140 | { |
||
| 141 | $resourceOrStream = $filenameResourceOrStream; |
||
| 142 | if (is_string($filenameResourceOrStream)) { |
||
| 143 | $resourceOrStream = fopen($filenameResourceOrStream, 'w+'); |
||
| 144 | } |
||
| 145 | |||
| 146 | $stream = Psr7\stream_for($resourceOrStream); |
||
|
|
|||
| 147 | Psr7\copy_to_stream($this->getBinaryContentStream(), $stream); |
||
| 148 | |||
| 149 | if (!is_string($filenameResourceOrStream) |
||
| 150 | && !($filenameResourceOrStream instanceof StreamInterface)) { |
||
| 151 | // only detach if it wasn't a string or StreamInterface, so the |
||
| 152 | // fopen call can be properly closed if it was |
||
| 153 | $stream->detach(); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | public function getContent($charset = MailMimeParser::DEFAULT_CHARSET) |
||
| 158 | { |
||
| 159 | $stream = $this->getContentStream($charset); |
||
| 160 | if ($stream !== null) { |
||
| 161 | return $stream->getContents(); |
||
| 162 | } |
||
| 163 | return null; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getParent() |
||
| 167 | { |
||
| 168 | return $this->parent; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function attachContentStream(StreamInterface $stream, $streamCharset = MailMimeParser::DEFAULT_CHARSET) |
||
| 172 | { |
||
| 173 | $ch = ($this->charsetOverride !== null) ? $this->charsetOverride : $this->getCharset(); |
||
| 174 | if ($ch !== null && $streamCharset !== $ch) { |
||
| 175 | $this->charsetOverride = $streamCharset; |
||
| 176 | } |
||
| 177 | $this->ignoreTransferEncoding = true; |
||
| 178 | $this->partStreamContainer->setContentStream($stream); |
||
| 179 | $this->notify(); |
||
| 180 | } |
||
| 181 | |||
| 182 | public function detachContentStream() |
||
| 183 | { |
||
| 184 | $this->partStreamContainer->setContentStream(null); |
||
| 185 | $this->notify(); |
||
| 186 | } |
||
| 187 | |||
| 188 | public function setContent($resource, $charset = MailMimeParser::DEFAULT_CHARSET) |
||
| 189 | { |
||
| 190 | $stream = Psr7\stream_for($resource); |
||
| 191 | $this->attachContentStream($stream, $charset); |
||
| 192 | // this->notify() called in attachContentStream |
||
| 193 | } |
||
| 194 | |||
| 195 | public function save($filenameResourceOrStream) |
||
| 196 | { |
||
| 197 | $resourceOrStream = $filenameResourceOrStream; |
||
| 198 | if (is_string($filenameResourceOrStream)) { |
||
| 199 | $resourceOrStream = fopen($filenameResourceOrStream, 'w+'); |
||
| 200 | } |
||
| 201 | |||
| 202 | $partStream = $this->getStream(); |
||
| 203 | $partStream->rewind(); |
||
| 204 | $stream = Psr7\stream_for($resourceOrStream); |
||
| 205 | Psr7\copy_to_stream($partStream, $stream); |
||
| 206 | |||
| 207 | if (!is_string($filenameResourceOrStream) |
||
| 208 | && !($filenameResourceOrStream instanceof StreamInterface)) { |
||
| 209 | // only detach if it wasn't a string or StreamInterface, so the |
||
| 210 | // fopen call can be properly closed if it was |
||
| 211 | $stream->detach(); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | public function __toString() |
||
| 218 | } |
||
| 219 | } |
||
| 220 |