Total Complexity | 40 |
Total Lines | 197 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
21 | abstract class MessagePart implements IMessagePart |
||
22 | { |
||
23 | /** |
||
24 | * @var IMimePart parent part |
||
25 | */ |
||
26 | protected $parent; |
||
27 | |||
28 | /** |
||
29 | * @var PartStreamContainer holds 'stream' and 'content stream'. |
||
30 | */ |
||
31 | protected $partStreamContainer; |
||
32 | |||
33 | /** |
||
34 | * @var string can be used to set an override for content's charset in cases |
||
35 | * where a user knows the charset on the content is not what it claims |
||
36 | * to be. |
||
37 | */ |
||
38 | protected $charsetOverride; |
||
39 | |||
40 | /** |
||
41 | * @var bool set to true when a user attaches a stream manually, it's |
||
42 | * assumed to already be decoded or to have relevant transfer encoding |
||
43 | * decorators attached already. |
||
44 | */ |
||
45 | protected $ignoreTransferEncoding; |
||
46 | |||
47 | /** |
||
48 | * @var SplObjectStorage attached observers that need to be notified of |
||
49 | * modifications to this part. |
||
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 getParent() |
||
81 | { |
||
82 | return $this->parent; |
||
83 | } |
||
84 | |||
85 | public function hasContent() |
||
86 | { |
||
87 | return $this->partStreamContainer->hasContent(); |
||
88 | } |
||
89 | |||
90 | public function getFilename() |
||
91 | { |
||
92 | return null; |
||
93 | } |
||
94 | |||
95 | public function setCharsetOverride($charsetOverride, $onlyIfNoCharset = false) |
||
96 | { |
||
97 | if (!$onlyIfNoCharset || $this->getCharset() === null) { |
||
98 | $this->charsetOverride = $charsetOverride; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | public function getContentStream($charset = MailMimeParser::DEFAULT_CHARSET) |
||
103 | { |
||
104 | if ($this->hasContent()) { |
||
105 | $tr = ($this->ignoreTransferEncoding) ? '' : $this->getContentTransferEncoding(); |
||
106 | $ch = ($this->charsetOverride !== null) ? $this->charsetOverride : $this->getCharset(); |
||
107 | return $this->partStreamContainer->getContentStream( |
||
108 | $tr, |
||
109 | $ch, |
||
110 | $charset |
||
111 | ); |
||
112 | } |
||
113 | return null; |
||
114 | } |
||
115 | |||
116 | public function getBinaryContentStream() |
||
117 | { |
||
118 | if ($this->hasContent()) { |
||
119 | $tr = ($this->ignoreTransferEncoding) ? '' : $this->getContentTransferEncoding(); |
||
120 | return $this->partStreamContainer->getBinaryContentStream($tr); |
||
121 | } |
||
122 | return null; |
||
123 | } |
||
124 | |||
125 | public function getBinaryContentResourceHandle() |
||
126 | { |
||
127 | $stream = $this->getBinaryContentStream(); |
||
128 | if ($stream !== null) { |
||
129 | return StreamWrapper::getResource($stream); |
||
130 | } |
||
131 | return null; |
||
132 | } |
||
133 | |||
134 | public function saveContent($filenameResourceOrStream) |
||
135 | { |
||
136 | $resourceOrStream = $filenameResourceOrStream; |
||
137 | if (is_string($filenameResourceOrStream)) { |
||
138 | $resourceOrStream = fopen($filenameResourceOrStream, 'w+'); |
||
139 | } |
||
140 | |||
141 | $stream = Utils::streamFor($resourceOrStream); |
||
142 | Utils::copyToStream($this->getBinaryContentStream(), $stream); |
||
|
|||
143 | |||
144 | if (!is_string($filenameResourceOrStream) |
||
145 | && !($filenameResourceOrStream instanceof StreamInterface)) { |
||
146 | // only detach if it wasn't a string or StreamInterface, so the |
||
147 | // fopen call can be properly closed if it was |
||
148 | $stream->detach(); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | public function getContent($charset = MailMimeParser::DEFAULT_CHARSET) |
||
153 | { |
||
154 | $stream = $this->getContentStream($charset); |
||
155 | if ($stream !== null) { |
||
156 | return $stream->getContents(); |
||
157 | } |
||
158 | return null; |
||
159 | } |
||
160 | |||
161 | public function attachContentStream(StreamInterface $stream, $streamCharset = MailMimeParser::DEFAULT_CHARSET) |
||
162 | { |
||
163 | $ch = ($this->charsetOverride !== null) ? $this->charsetOverride : $this->getCharset(); |
||
164 | if ($ch !== null && $streamCharset !== $ch) { |
||
165 | $this->charsetOverride = $streamCharset; |
||
166 | } |
||
167 | $this->ignoreTransferEncoding = true; |
||
168 | $this->partStreamContainer->setContentStream($stream); |
||
169 | $this->notify(); |
||
170 | } |
||
171 | |||
172 | public function detachContentStream() |
||
173 | { |
||
174 | $this->partStreamContainer->setContentStream(null); |
||
175 | $this->notify(); |
||
176 | } |
||
177 | |||
178 | public function setContent($resource, $charset = MailMimeParser::DEFAULT_CHARSET) |
||
179 | { |
||
180 | $stream = Utils::streamFor($resource); |
||
181 | $this->attachContentStream($stream, $charset); |
||
182 | // this->notify() called in attachContentStream |
||
183 | } |
||
184 | |||
185 | public function getResourceHandle() |
||
186 | { |
||
187 | return StreamWrapper::getResource($this->getStream()); |
||
188 | } |
||
189 | |||
190 | public function getStream() |
||
191 | { |
||
192 | return $this->partStreamContainer->getStream(); |
||
193 | } |
||
194 | |||
195 | public function save($filenameResourceOrStream, $filemode = 'w+') |
||
196 | { |
||
197 | $resourceOrStream = $filenameResourceOrStream; |
||
198 | if (is_string($filenameResourceOrStream)) { |
||
199 | $resourceOrStream = fopen($filenameResourceOrStream, $filemode); |
||
200 | } |
||
201 | |||
202 | $partStream = $this->getStream(); |
||
203 | $partStream->rewind(); |
||
204 | $stream = Utils::streamFor($resourceOrStream); |
||
205 | Utils::copyToStream($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 |