Total Complexity | 46 |
Total Lines | 389 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Query { |
||
28 | |||
29 | /** @var array $query */ |
||
30 | protected $query; |
||
31 | |||
32 | /** @var string $raw_query */ |
||
33 | protected $raw_query; |
||
34 | |||
35 | /** @var string $charset */ |
||
36 | protected $charset; |
||
37 | |||
38 | /** @var Client $client */ |
||
39 | protected $client; |
||
40 | |||
41 | /** @var int $limit */ |
||
42 | protected $limit = null; |
||
43 | |||
44 | /** @var int $page */ |
||
45 | protected $page = 1; |
||
46 | |||
47 | /** @var int $fetch_options */ |
||
48 | protected $fetch_options = null; |
||
49 | |||
50 | /** @var int $fetch_body */ |
||
51 | protected $fetch_body = true; |
||
52 | |||
53 | /** @var int $fetch_attachment */ |
||
54 | protected $fetch_attachment = true; |
||
55 | |||
56 | /** @var int $fetch_flags */ |
||
57 | protected $fetch_flags = true; |
||
58 | |||
59 | /** |
||
60 | * Query constructor. |
||
61 | * @param Client $client |
||
62 | * @param string $charset |
||
63 | */ |
||
64 | public function __construct(Client $client, $charset = 'UTF-8') { |
||
65 | $this->setClient($client); |
||
66 | |||
67 | if(config('imap.options.fetch') === FT_PEEK) $this->leaveUnread(); |
||
68 | |||
69 | $this->charset = $charset; |
||
70 | $this->query = collect(); |
||
|
|||
71 | $this->boot(); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Instance boot method for additional functionality |
||
76 | */ |
||
77 | protected function boot(){} |
||
78 | |||
79 | /** |
||
80 | * Parse a given value |
||
81 | * @param mixed $value |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | protected function parse_value($value){ |
||
86 | switch(true){ |
||
87 | case $value instanceof \Carbon\Carbon: |
||
88 | $value = $value->format('d M y'); |
||
89 | break; |
||
90 | } |
||
91 | |||
92 | return (string) $value; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Check if a given date is a valid carbon object and if not try to convert it |
||
97 | * @param $date |
||
98 | * |
||
99 | * @return Carbon |
||
100 | * @throws MessageSearchValidationException |
||
101 | */ |
||
102 | protected function parse_date($date) { |
||
103 | if($date instanceof \Carbon\Carbon) return $date; |
||
104 | |||
105 | try { |
||
106 | $date = Carbon::parse($date); |
||
107 | } catch (\Exception $e) { |
||
108 | throw new MessageSearchValidationException(); |
||
109 | } |
||
110 | |||
111 | return $date; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Don't mark messages as read when fetching |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function leaveUnread() { |
||
120 | $this->setFetchOptions(FT_PEEK); |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Mark all messages as read when fetching |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function markAsRead() { |
||
131 | $this->setFetchOptions(FT_UID); |
||
132 | |||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Fetch the current query and return all found messages |
||
138 | * |
||
139 | * @return MessageCollection |
||
140 | * @throws GetMessagesFailedException |
||
141 | */ |
||
142 | public function get() { |
||
143 | $messages = MessageCollection::make([]); |
||
144 | |||
145 | try { |
||
146 | $this->generate_query(); |
||
147 | |||
148 | /** |
||
149 | * Don't set the charset if it isn't used - prevent strange outlook mail server errors |
||
150 | * @see https://github.com/Webklex/laravel-imap/issues/100 |
||
151 | */ |
||
152 | if($this->getCharset() === null){ |
||
153 | $available_messages = imap_search($this->getClient()->getConnection(), $this->getRawQuery(), SE_UID); |
||
154 | }else{ |
||
155 | $available_messages = imap_search($this->getClient()->getConnection(), $this->getRawQuery(), SE_UID, $this->getCharset()); |
||
156 | } |
||
157 | |||
158 | if ($available_messages !== false) { |
||
159 | |||
160 | $available_messages = collect($available_messages); |
||
161 | $options = config('imap.options'); |
||
162 | |||
163 | if(strtolower($options['fetch_order']) === 'desc'){ |
||
164 | $available_messages = $available_messages->reverse(); |
||
165 | } |
||
166 | |||
167 | $available_messages->forPage($this->page, $this->limit)->each(function($msgno, $msglist) use(&$messages, $options) { |
||
168 | $oMessage = new Message($msgno, $msglist, $this->getClient(), $this->getFetchOptions(), $this->getFetchBody(), $this->getFetchAttachment()); |
||
169 | switch ($options['message_key']){ |
||
170 | case 'number': |
||
171 | $message_key = $oMessage->getMessageNo(); |
||
172 | break; |
||
173 | case 'list': |
||
174 | $message_key = $msglist; |
||
175 | break; |
||
176 | default: |
||
177 | $message_key = $oMessage->getMessageId(); |
||
178 | break; |
||
179 | |||
180 | } |
||
181 | $messages->put($message_key, $oMessage); |
||
182 | }); |
||
183 | } |
||
184 | |||
185 | return $messages; |
||
186 | } catch (\Exception $e) { |
||
187 | $message = $e->getMessage(); |
||
188 | |||
189 | throw new GetMessagesFailedException($message); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Get the raw IMAP search query |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function generate_query() { |
||
199 | $query = ''; |
||
200 | $this->query->each(function($statement) use(&$query) { |
||
201 | if (count($statement) == 1) { |
||
202 | $query .= $statement[0]; |
||
203 | } else { |
||
204 | if($statement[1] === null){ |
||
205 | $query .= $statement[0]; |
||
206 | }else{ |
||
207 | $query .= $statement[0].' "'.$statement[1].'"'; |
||
208 | } |
||
209 | } |
||
210 | $query .= ' '; |
||
211 | |||
212 | }); |
||
213 | |||
214 | $this->raw_query = trim($query); |
||
215 | |||
216 | return $this->raw_query; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @return Client |
||
221 | */ |
||
222 | public function getClient() { |
||
223 | $this->client->checkConnection(); |
||
224 | return $this->client; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Set the limit and page for the current query |
||
229 | * @param int $limit |
||
230 | * @param int $page |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function limit($limit, $page = 1) { |
||
235 | if($page >= 1) $this->page = $page; |
||
236 | $this->limit = $limit; |
||
237 | |||
238 | return $this; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return array |
||
243 | */ |
||
244 | public function getQuery() { |
||
245 | return $this->query; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param array $query |
||
250 | * @return Query |
||
251 | */ |
||
252 | public function setQuery($query) { |
||
253 | $this->query = $query; |
||
254 | return $this; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @return string |
||
259 | */ |
||
260 | public function getRawQuery() { |
||
261 | return $this->raw_query; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param string $raw_query |
||
266 | * @return Query |
||
267 | */ |
||
268 | public function setRawQuery($raw_query) { |
||
269 | $this->raw_query = $raw_query; |
||
270 | return $this; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getCharset() { |
||
277 | return $this->charset; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @param string $charset |
||
282 | * @return Query |
||
283 | */ |
||
284 | public function setCharset($charset) { |
||
285 | $this->charset = $charset; |
||
286 | return $this; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @param Client $client |
||
291 | * @return Query |
||
292 | */ |
||
293 | public function setClient(Client $client) { |
||
294 | $this->client = $client; |
||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @return int |
||
300 | */ |
||
301 | public function getLimit() { |
||
302 | return $this->limit; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @param int $limit |
||
307 | * @return Query |
||
308 | */ |
||
309 | public function setLimit($limit) { |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @return int |
||
316 | */ |
||
317 | public function getPage() { |
||
318 | return $this->page; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @param int $page |
||
323 | * @return Query |
||
324 | */ |
||
325 | public function setPage($page) { |
||
326 | $this->page = $page; |
||
327 | return $this; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * @param boolean $fetch_options |
||
332 | * @return Query |
||
333 | */ |
||
334 | public function setFetchOptions($fetch_options) { |
||
335 | $this->fetch_options = $fetch_options; |
||
336 | return $this; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @param boolean $fetch_options |
||
341 | * @return Query |
||
342 | */ |
||
343 | public function fetchOptions($fetch_options) { |
||
344 | return $this->setFetchOptions($fetch_options); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @return int |
||
349 | */ |
||
350 | public function getFetchOptions() { |
||
351 | return $this->fetch_options; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @return boolean |
||
356 | */ |
||
357 | public function getFetchBody() { |
||
358 | return $this->fetch_body; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param boolean $fetch_body |
||
363 | * @return Query |
||
364 | */ |
||
365 | public function setFetchBody($fetch_body) { |
||
366 | $this->fetch_body = $fetch_body; |
||
367 | return $this; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @param boolean $fetch_body |
||
372 | * @return Query |
||
373 | */ |
||
374 | public function fetchBody($fetch_body) { |
||
375 | return $this->setFetchBody($fetch_body); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * @return boolean |
||
380 | */ |
||
381 | public function getFetchAttachment() { |
||
382 | return $this->fetch_attachment; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @param boolean $fetch_attachment |
||
387 | * @return Query |
||
388 | */ |
||
389 | public function setFetchAttachment($fetch_attachment) { |
||
390 | $this->fetch_attachment = $fetch_attachment; |
||
391 | return $this; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * @param boolean $fetch_attachment |
||
396 | * @return Query |
||
397 | */ |
||
398 | public function fetchAttachment($fetch_attachment) { |
||
399 | return $this->setFetchAttachment($fetch_attachment); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * @return int |
||
404 | */ |
||
405 | public function getFetchFlags() { |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * @param int $fetch_flags |
||
411 | * @return Query |
||
412 | */ |
||
413 | public function setFetchFlags($fetch_flags) { |
||
414 | $this->fetch_flags = $fetch_flags; |
||
415 | return $this; |
||
416 | } |
||
417 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..