Total Complexity | 51 |
Total Lines | 288 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Attachment 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 Attachment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class Attachment { |
||
51 | |||
52 | /** @var Message $oMessage */ |
||
53 | protected $oMessage; |
||
54 | |||
55 | /** @var array $config */ |
||
56 | protected $config = []; |
||
57 | |||
58 | /** @var object $structure */ |
||
59 | protected $structure; |
||
60 | |||
61 | /** @var array $attributes */ |
||
62 | protected $attributes = [ |
||
63 | 'part_number' => 1, |
||
64 | 'content' => null, |
||
65 | 'type' => null, |
||
66 | 'content_type' => null, |
||
67 | 'id' => null, |
||
68 | 'name' => null, |
||
69 | 'disposition' => null, |
||
70 | 'img_src' => null, |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * Default mask |
||
75 | * @var string $mask |
||
76 | */ |
||
77 | protected $mask = AttachmentMask::class; |
||
78 | |||
79 | /** |
||
80 | * Attachment constructor. |
||
81 | * |
||
82 | * @param Message $oMessage |
||
83 | * @param object $structure |
||
84 | * @param integer $part_number |
||
85 | * |
||
86 | * @throws Exceptions\ConnectionFailedException |
||
87 | */ |
||
88 | public function __construct(Message $oMessage, $structure, $part_number = 1) { |
||
89 | $this->config = config('imap.options'); |
||
90 | |||
91 | $this->oMessage = $oMessage; |
||
92 | $this->structure = $structure; |
||
93 | $this->part_number = ($part_number) ? $part_number : $this->part_number; |
||
94 | |||
95 | $default_mask = $this->oMessage->getClient()->getDefaultAttachmentMask(); |
||
96 | if($default_mask != null) { |
||
97 | $this->mask = $default_mask; |
||
98 | } |
||
99 | |||
100 | $this->findType(); |
||
101 | $this->fetch(); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Call dynamic attribute setter and getter methods |
||
106 | * @param string $method |
||
107 | * @param array $arguments |
||
108 | * |
||
109 | * @return mixed |
||
110 | * @throws MethodNotFoundException |
||
111 | */ |
||
112 | public function __call($method, $arguments) { |
||
113 | if(strtolower(substr($method, 0, 3)) === 'get') { |
||
114 | $name = snake_case(substr($method, 3)); |
||
|
|||
115 | |||
116 | if(isset($this->attributes[$name])) { |
||
117 | return $this->attributes[$name]; |
||
118 | } |
||
119 | |||
120 | return null; |
||
121 | }elseif (strtolower(substr($method, 0, 3)) === 'set') { |
||
122 | $name = snake_case(substr($method, 3)); |
||
123 | |||
124 | $this->attributes[$name] = array_pop($arguments); |
||
125 | |||
126 | return $this->attributes[$name]; |
||
127 | } |
||
128 | |||
129 | throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param $name |
||
134 | * @param $value |
||
135 | * |
||
136 | * @return mixed |
||
137 | */ |
||
138 | public function __set($name, $value) { |
||
139 | $this->attributes[$name] = $value; |
||
140 | |||
141 | return $this->attributes[$name]; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param $name |
||
146 | * |
||
147 | * @return mixed|null |
||
148 | */ |
||
149 | public function __get($name) { |
||
150 | if(isset($this->attributes[$name])) { |
||
151 | return $this->attributes[$name]; |
||
152 | } |
||
153 | |||
154 | return null; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Determine the structure type |
||
159 | */ |
||
160 | protected function findType() { |
||
161 | switch ($this->structure->type) { |
||
162 | case IMAP::ATTACHMENT_TYPE_MESSAGE: |
||
163 | $this->type = 'message'; |
||
164 | break; |
||
165 | case IMAP::ATTACHMENT_TYPE_APPLICATION: |
||
166 | $this->type = 'application'; |
||
167 | break; |
||
168 | case IMAP::ATTACHMENT_TYPE_AUDIO: |
||
169 | $this->type = 'audio'; |
||
170 | break; |
||
171 | case IMAP::ATTACHMENT_TYPE_IMAGE: |
||
172 | $this->type = 'image'; |
||
173 | break; |
||
174 | case IMAP::ATTACHMENT_TYPE_VIDEO: |
||
175 | $this->type = 'video'; |
||
176 | break; |
||
177 | case IMAP::ATTACHMENT_TYPE_MODEL: |
||
178 | $this->type = 'model'; |
||
179 | break; |
||
180 | case IMAP::ATTACHMENT_TYPE_TEXT: |
||
181 | $this->type = 'text'; |
||
182 | break; |
||
183 | case IMAP::ATTACHMENT_TYPE_MULTIPART: |
||
184 | $this->type = 'multipart'; |
||
185 | break; |
||
186 | default: |
||
187 | $this->type = 'other'; |
||
188 | break; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Fetch the given attachment |
||
194 | * |
||
195 | * @throws Exceptions\ConnectionFailedException |
||
196 | */ |
||
197 | protected function fetch() { |
||
198 | |||
199 | $content = imap_fetchbody($this->oMessage->getClient()->getConnection(), $this->oMessage->getUid(), $this->part_number, $this->oMessage->getFetchOptions() | FT_UID); |
||
200 | |||
201 | $this->content_type = $this->type.'/'.strtolower($this->structure->subtype); |
||
202 | $this->content = $this->oMessage->decodeString($content, $this->structure->encoding); |
||
203 | |||
204 | if (property_exists($this->structure, 'id')) { |
||
205 | $this->id = str_replace(['<', '>'], '', $this->structure->id); |
||
206 | } |
||
207 | |||
208 | if (property_exists($this->structure, 'dparameters')) { |
||
209 | foreach ($this->structure->dparameters as $parameter) { |
||
210 | if (strtolower($parameter->attribute) == "filename") { |
||
211 | $this->setName($parameter->value); |
||
212 | $this->disposition = property_exists($this->structure, 'disposition') ? $this->structure->disposition : null; |
||
213 | break; |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | |||
218 | if (IMAP::ATTACHMENT_TYPE_MESSAGE == $this->structure->type) { |
||
219 | if ($this->structure->ifdescription) { |
||
220 | $this->setName($this->structure->description); |
||
221 | } else { |
||
222 | $this->setName($this->structure->subtype); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | if (!$this->name && property_exists($this->structure, 'parameters')) { |
||
227 | foreach ($this->structure->parameters as $parameter) { |
||
228 | if (strtolower($parameter->attribute) == "name") { |
||
229 | $this->setName($parameter->value); |
||
230 | $this->disposition = property_exists($this->structure, 'disposition') ? $this->structure->disposition : null; |
||
231 | break; |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Save the attachment content to your filesystem |
||
239 | * |
||
240 | * @param string|null $path |
||
241 | * @param string|null $filename |
||
242 | * |
||
243 | * @return boolean |
||
244 | */ |
||
245 | public function save($path = null, $filename = null) { |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @param $name |
||
256 | */ |
||
257 | public function setName($name) { |
||
262 | } |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @return null|string |
||
267 | * |
||
268 | * @deprecated 1.4.0:2.0.0 No longer needed. Use AttachmentMask::getImageSrc() instead |
||
269 | */ |
||
270 | public function getImgSrc() { |
||
271 | if ($this->type == 'image' && $this->img_src == null) { |
||
272 | $this->img_src = 'data:'.$this->content_type.';base64,'.base64_encode($this->content); |
||
273 | } |
||
274 | return $this->img_src; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @return string|null |
||
279 | */ |
||
280 | public function getMimeType(){ |
||
281 | return (new \finfo())->buffer($this->getContent(), FILEINFO_MIME_TYPE); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @return string|null |
||
286 | */ |
||
287 | public function getExtension(){ |
||
288 | return ExtensionGuesser::getInstance()->guess($this->getMimeType()); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @return array |
||
293 | */ |
||
294 | public function getAttributes(){ |
||
295 | return $this->attributes; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @return Message |
||
300 | */ |
||
301 | public function getMessage(){ |
||
302 | return $this->oMessage; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @param $mask |
||
307 | * @return $this |
||
308 | */ |
||
309 | public function setMask($mask){ |
||
310 | if(class_exists($mask)){ |
||
311 | $this->mask = $mask; |
||
312 | } |
||
313 | |||
314 | return $this; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * @return string |
||
319 | */ |
||
320 | public function getMask(){ |
||
321 | return $this->mask; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Get a masked instance by providing a mask name |
||
326 | * @param string|null $mask |
||
327 | * |
||
328 | * @return mixed |
||
329 | * @throws MaskNotFoundException |
||
330 | */ |
||
331 | public function mask($mask = null){ |
||
338 | } |
||
339 | } |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.