Total Complexity | 43 |
Total Lines | 402 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ContactContactHandler 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 ContactContactHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class ContactContactHandler extends XoopsPersistableObjectHandler |
||
32 | { |
||
33 | /** |
||
34 | * ContactContactHandler constructor. |
||
35 | * @param null|\XoopsDatabase $db |
||
36 | */ |
||
37 | public function __construct(\XoopsDatabase $db) |
||
38 | { |
||
39 | parent::__construct($db, 'contact', Contact::class, 'contact_id', 'contact_mail'); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Get variables passed by GET or POST method |
||
44 | * @param $global |
||
45 | * @param $key |
||
46 | * @param string $default |
||
47 | * @param string $type |
||
48 | * @return false|int|mixed|string |
||
49 | */ |
||
50 | /* |
||
51 | public function contactCleanVars(&$global, $key, $default = '', $type = 'int') |
||
52 | { |
||
53 | switch ($type) { |
||
54 | case 'array': |
||
55 | $ret = (isset($global[$key]) && is_array($global[$key])) ? $global[$key] : $default; |
||
56 | break; |
||
57 | case 'date': |
||
58 | $ret = isset($global[$key]) ? strtotime($global[$key]) : $default; |
||
59 | break; |
||
60 | case 'string': |
||
61 | $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_SANITIZE_MAGIC_QUOTES) : $default; |
||
62 | break; |
||
63 | case 'mail': |
||
64 | $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_VALIDATE_EMAIL) : $default; |
||
65 | break; |
||
66 | case 'url': |
||
67 | $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) : $default; |
||
68 | break; |
||
69 | case 'ip': |
||
70 | $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_VALIDATE_IP) : $default; |
||
71 | break; |
||
72 | case 'amp': |
||
73 | $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_FLAG_ENCODE_AMP) : $default; |
||
74 | break; |
||
75 | case 'text': |
||
76 | $ret = isset($global[$key]) ? htmlentities($global[$key], ENT_QUOTES, 'UTF-8') : $default; |
||
77 | break; |
||
78 | case 'platform': |
||
79 | $ret = isset($global[$key]) ? $this->contactPlatform($global[$key]) : $this->contactPlatform($default); |
||
80 | break; |
||
81 | case 'type': |
||
82 | $ret = isset($global[$key]) ? $this->contactType($global[$key]) : $this->contactType($default); |
||
83 | break; |
||
84 | case 'int': |
||
85 | default: |
||
86 | $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_SANITIZE_NUMBER_INT) : $default; |
||
87 | break; |
||
88 | } |
||
89 | if ($ret === false) { |
||
90 | return $default; |
||
91 | } |
||
92 | |||
93 | return $ret; |
||
94 | } |
||
95 | */ |
||
96 | |||
97 | /** |
||
98 | * @return array |
||
99 | */ |
||
100 | public function contactInfoProcessing() |
||
101 | { |
||
102 | $contact = []; |
||
103 | $contact['contact_cid'] = Request::getInt('contact_id', 0, 'POST'); |
||
104 | $contact['contact_uid'] = Request::getInt('contact_uid', 0, 'POST'); |
||
105 | $contact['contact_name'] = Request::getString('contact_name', '', 'POST'); |
||
106 | $contact['contact_nameto'] = Request::getString('contact_nameto', '', 'POST'); |
||
107 | $contact['contact_subject'] = Request::getString('contact_subject', '', 'POST'); |
||
108 | $contact['contact_mail'] = Request::getString('contact_mail', '', 'POST'); |
||
109 | $contact['contact_mailto'] = Request::getEmail('contact_mailto', '', 'POST'); |
||
110 | $contact['contact_url'] = Request::getUrl('contact_url', '', 'POST'); |
||
111 | $contact['contact_create'] = time(); |
||
112 | $contact['contact_icq'] = Request::getString('contact_icq', '', 'POST'); |
||
113 | $contact['contact_company'] = Request::getString('contact_company', '', 'POST'); |
||
114 | $contact['contact_location'] = Request::getString('contact_location', '', 'POST'); |
||
115 | $contact['contact_phone'] = Request::getString('contact_phone', '', 'int'); |
||
116 | $contact['contact_department'] = Request::getString('contact_department', _MD_CONTACT_DEFULTDEP, 'POST'); |
||
117 | $contact['contact_ip'] = getenv('REMOTE_ADDR'); |
||
118 | $contact['contact_message'] = Request::getText('contact_message', '', 'POST'); |
||
119 | $contact['contact_address'] = Request::getString('contact_address', '', 'POST'); |
||
120 | $contact['contact_platform'] = Request::getString('contact_platform', 'Web', 'POST'); |
||
121 | $contact['contact_type'] = Request::getString('contact_type', 'Contact', 'POST'); |
||
122 | $contact['contact_reply'] = Request::getInt('contact_reply', 0, 'POST'); |
||
123 | |||
124 | return $contact; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param $contact |
||
129 | * @return string |
||
130 | */ |
||
131 | public function contactSendMail($contact) |
||
132 | { |
||
133 | $xoopsMailer = xoops_getMailer(); |
||
134 | $xoopsMailer->useMail(); |
||
135 | $xoopsMailer->setToEmails($this->contactToEmails($contact['contact_department'])); |
||
136 | $xoopsMailer->setFromEmail($contact['contact_mail']); |
||
137 | $xoopsMailer->setFromName(html_entity_decode($contact['contact_name'], ENT_QUOTES, 'UTF-8')); |
||
138 | |||
139 | $subjectPrefix = ''; |
||
140 | if ($GLOBALS['xoopsModuleConfig']['form_dept'] && $GLOBALS['xoopsModuleConfig']['subject_prefix'] && $GLOBALS['xoopsModuleConfig']['contact_dept']) { |
||
141 | $subjectPrefix = '[' . $GLOBALS['xoopsModuleConfig']['prefix_text'] . ' ' . $contact['contact_department'] . ']: '; |
||
142 | } |
||
143 | $xoopsMailer->setSubject($subjectPrefix . html_entity_decode($contact['contact_subject'], ENT_QUOTES, 'UTF-8')); |
||
144 | $xoopsMailer->setBody(html_entity_decode($contact['contact_message'], ENT_QUOTES, 'UTF-8')); |
||
145 | if ($xoopsMailer->send()) { |
||
146 | $message = _MD_CONTACT_MES_SEND; |
||
147 | } else { |
||
148 | $message = $xoopsMailer->getErrors(); |
||
149 | } |
||
150 | |||
151 | return $message; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param $contact |
||
156 | * @return string |
||
157 | */ |
||
158 | public function contactSendMailConfirm($contact) |
||
159 | { |
||
160 | $xoopsMailer = xoops_getMailer(); |
||
161 | $xoopsMailer->useMail(); |
||
162 | $xoopsMailer->setToEmails($contact['contact_mail']); |
||
163 | $xoopsMailer->setFromEmail($this->contactToEmails($contact['contact_department'])); |
||
164 | $xoopsMailer->setFromName(html_entity_decode($GLOBALS['xoopsConfig']['sitename'], ENT_QUOTES, 'UTF-8')); |
||
165 | |||
166 | $xoopsMailer->setSubject(_MD_CONTACT_MAILCONFIRM_SUBJECT); |
||
167 | $body = str_replace('{NAME}', html_entity_decode($contact['contact_name'], ENT_QUOTES, 'UTF-8'), _MD_CONTACT_MAILCONFIRM_BODY); |
||
168 | $body = str_replace('{SUBJECT}', html_entity_decode($contact['contact_subject'], ENT_QUOTES, 'UTF-8'), $body); |
||
169 | $body = str_replace('{BODY}', html_entity_decode($contact['contact_message'], ENT_QUOTES, 'UTF-8'), $body); |
||
170 | $xoopsMailer->setBody($body); |
||
171 | if ($xoopsMailer->send()) { |
||
172 | $message = _MD_CONTACT_MES_SEND; |
||
173 | } else { |
||
174 | $message = $xoopsMailer->getErrors(); |
||
175 | } |
||
176 | |||
177 | return $message; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param $contact |
||
182 | * @return string |
||
183 | */ |
||
184 | public function contactReplyMail($contact) |
||
185 | { |
||
186 | $xoopsMailer = xoops_getMailer(); |
||
187 | $xoopsMailer->useMail(); |
||
188 | $xoopsMailer->setToEmails($contact['contact_mailto']); |
||
189 | $xoopsMailer->setFromEmail($contact['contact_mail']); |
||
190 | $xoopsMailer->setFromName($contact['contact_name']); |
||
191 | $xoopsMailer->setSubject($contact['contact_subject']); |
||
192 | $xoopsMailer->setBody($contact['contact_message']); |
||
193 | if ($xoopsMailer->send()) { |
||
194 | $message = _MD_CONTACT_MES_SEND; |
||
195 | } else { |
||
196 | $message = $xoopsMailer->getErrors(); |
||
197 | } |
||
198 | |||
199 | return $message; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param null $department |
||
204 | * @return array |
||
205 | */ |
||
206 | public function contactToEmails($department = null) |
||
207 | { |
||
208 | // global $xoopsConfig; |
||
209 | $department_mail[] = xoops_getModuleOption('contact_recipient_std', 'contact'); |
||
210 | if (!empty($department)) { |
||
211 | $departments = xoops_getModuleOption('contact_dept', 'contact'); |
||
212 | foreach ($departments as $vals) { |
||
213 | $vale = explode(',', $vals); |
||
214 | if ($department == $vale[0]) { |
||
215 | $department_mail[] = $vale[1]; |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | return $department_mail; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param $contact_id |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function contactAddReply($contact_id) |
||
228 | { |
||
229 | $obj = $this->get($contact_id); |
||
230 | $obj->setVar('contact_reply', 1); |
||
231 | if (!$this->insert($obj)) { |
||
232 | return false; |
||
233 | } |
||
234 | |||
235 | return true; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param $contact_id |
||
240 | * @return array|bool |
||
241 | */ |
||
242 | public function contactGetReply($contact_id) |
||
243 | { |
||
244 | $ret = false; |
||
245 | |||
246 | $criteria = new \CriteriaCompo(); |
||
247 | $criteria->add(new \Criteria('contact_cid', $contact_id)); |
||
248 | $criteria->add(new \Criteria('contact_type', 'Contact')); |
||
249 | $contacts = $this->getObjects($criteria, false); |
||
250 | if ($contacts) { |
||
251 | $ret = []; |
||
252 | /** @var Contact $root */ |
||
253 | foreach ($contacts as $root) { |
||
254 | $tab = []; |
||
255 | $tab = $root->toArray(); |
||
256 | $tab['contact_owner'] = XoopsUser::getUnameFromId($root->getVar('contact_uid')); |
||
257 | $tab['contact_create'] = formatTimestamp($root->getVar('contact_create'), _MEDIUMDATESTRING); |
||
258 | $ret [] = $tab; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | return $ret; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @param $contact |
||
267 | * @param $id |
||
268 | * @return array |
||
269 | */ |
||
270 | public function contactGetAdminList($contact, $id) |
||
271 | { |
||
272 | $ret = []; |
||
273 | $criteria = new \CriteriaCompo(); |
||
274 | $criteria->add(new \Criteria($id, '0')); |
||
275 | $criteria->add(new \Criteria('contact_type', 'Contact')); |
||
276 | $criteria->setSort($contact['sort']); |
||
277 | $criteria->setOrder($contact['order']); |
||
278 | $criteria->setStart($contact['start']); |
||
279 | $criteria->setLimit($contact['limit']); |
||
280 | $contacts = $this->getObjects($criteria, false); |
||
281 | if ($contacts) { |
||
282 | /** @var Contact $root */ |
||
283 | foreach ($contacts as $root) { |
||
284 | $tab = []; |
||
285 | $tab = $root->toArray(); |
||
286 | $tab['contact_owner'] = XoopsUser::getUnameFromId($root->getVar('contact_uid')); |
||
287 | $tab['contact_create'] = formatTimestamp($root->getVar('contact_create'), _MEDIUMDATESTRING); |
||
288 | $ret [] = $tab; |
||
289 | } |
||
290 | } |
||
291 | |||
292 | return $ret; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Get file Count |
||
297 | * @param $id |
||
298 | * @return int |
||
299 | */ |
||
300 | public function contactGetCount($id) |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Get Insert ID |
||
311 | */ |
||
312 | public function getInsertId() |
||
313 | { |
||
314 | return $this->db->getInsertId(); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Contact Prune Count |
||
319 | * @param $timestamp |
||
320 | * @param $onlyreply |
||
321 | * @return int |
||
322 | */ |
||
323 | public function contactPruneCount($timestamp, $onlyreply) |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Contact Delete Before Date |
||
336 | * @param $timestamp |
||
337 | * @param $onlyreply |
||
338 | */ |
||
339 | public function contactDeleteBeforeDate($timestamp, $onlyreply) |
||
340 | { |
||
341 | $criteria = new \CriteriaCompo(); |
||
342 | $criteria->add(new \Criteria('contact_create', $timestamp, '<=')); |
||
343 | if ($onlyreply) { |
||
344 | $criteria->add(new \Criteria('contact_reply', 1)); |
||
345 | } |
||
346 | $this->deleteAll($criteria); |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Contact Platform |
||
351 | * @param $platform |
||
352 | * @return string |
||
353 | */ |
||
354 | public function contactPlatform($platform) |
||
355 | { |
||
356 | $platform = strtolower($platform); |
||
357 | switch ($platform) { |
||
358 | case 'Android': |
||
359 | $ret = 'Android'; |
||
360 | break; |
||
361 | |||
362 | case 'Ios': |
||
363 | $ret = 'Ios'; |
||
364 | break; |
||
365 | |||
366 | case 'Web': |
||
367 | default: |
||
368 | $ret = 'Web'; |
||
369 | break; |
||
370 | } |
||
371 | |||
372 | return $ret; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Contact type |
||
377 | * @param $type |
||
378 | * @return string |
||
379 | */ |
||
380 | public function contactType($type) |
||
381 | { |
||
382 | $type = strtolower($type); |
||
383 | switch ($type) { |
||
384 | case 'Mail': |
||
385 | $ret = 'Mail'; |
||
386 | break; |
||
387 | |||
388 | case 'Phone': |
||
389 | $ret = 'Phone'; |
||
390 | break; |
||
391 | |||
392 | case 'Contact': |
||
393 | default: |
||
394 | $ret = 'Contact'; |
||
395 | break; |
||
396 | } |
||
397 | |||
398 | return $ret; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Contact logs |
||
403 | * @param $column |
||
404 | * @param null $timestamp |
||
405 | * @return array |
||
406 | */ |
||
407 | public function contactLogs($column, $timestamp = null) |
||
433 | } |
||
434 | } |
||
435 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.