| Total Complexity | 41 |
| Total Lines | 354 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Tickets 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 Tickets, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class Tickets |
||
| 5 | { |
||
| 6 | private $db; |
||
| 7 | |||
| 8 | public function __construct($database) |
||
| 11 | } |
||
| 12 | |||
| 13 | public function add_ticket($ticketnumber, $sla, $reporteddate, $reportedby, $telp, $email, $problemsummary, $problemdetail, $ticketstatus, $assignee, $documentedby, $pro) |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | public function update_ticket($id, $sla, $reporteddate, $reportedby, $telp, $email, $problemsummary, $problemdetail, $ticketstatus, $assignee, $assigneddate, $pendingby, $pendingdate, $resolution, $resolvedby, $resolveddate, $closedby, $closeddate, $processby, $processdate, $comment) |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | public function delete($id) |
||
| 73 | { |
||
| 74 | $sql = 'DELETE FROM `tickets` WHERE `id` = ?'; |
||
| 75 | $query = $this->db->prepare($sql); |
||
| 76 | $query->bindValue(1, $id); |
||
| 77 | |||
| 78 | try { |
||
| 79 | $query->execute(); |
||
| 80 | } catch (PDOException $e) { |
||
| 81 | die($e->getMessage()); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | public function ticket_data($id) |
||
| 86 | { |
||
| 87 | $query = $this->db->prepare('SELECT * FROM `tickets` WHERE `id`= ?'); |
||
| 88 | $query->bindValue(1, $id); |
||
| 89 | |||
| 90 | try { |
||
| 91 | $query->execute(); |
||
| 92 | |||
| 93 | return $query->fetch(); |
||
| 94 | } catch (PDOException $e) { |
||
| 95 | die($e->getMessage()); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | public function get_tickets() |
||
| 110 | } |
||
| 111 | |||
| 112 | public function get_pro() |
||
| 113 | { |
||
| 114 | $query = $this->db->prepare('SELECT pro FROM `tickets`'); |
||
| 115 | |||
| 116 | try { |
||
| 117 | $query->execute(); |
||
| 118 | } catch (PDOException $e) { |
||
| 119 | die($e->getMessage()); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $query->fetchAll(); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function get_opened_tickets() |
||
| 126 | { |
||
| 127 | $query = $this->db->prepare("SELECT * FROM `tickets` WHERE `ticketstatus` <> 'Closed' ORDER BY `ticketnumber` DESC"); |
||
| 128 | |||
| 129 | try { |
||
| 130 | $query->execute(); |
||
| 131 | } catch (PDOException $e) { |
||
| 132 | die($e->getMessage()); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $query->fetchAll(); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function get_tickets_by_requester($userid) |
||
| 139 | { |
||
| 140 | $query = $this->db->prepare('SELECT * FROM `tickets` WHERE `documentedby`= ? ORDER BY `ticketnumber` DESC'); |
||
| 141 | $query->bindValue(1, $userid); |
||
| 142 | |||
| 143 | try { |
||
| 144 | $query->execute(); |
||
| 145 | } catch (PDOException $e) { |
||
| 146 | die($e->getMessage()); |
||
| 147 | } |
||
| 148 | |||
| 149 | return $query->fetchAll(); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function get_tickets_by_assignee($userid) |
||
| 153 | { |
||
| 154 | $query = $this->db->prepare('SELECT * FROM `tickets` WHERE `assignee`= ? ORDER BY `ticketnumber` DESC'); |
||
| 155 | $query->bindValue(1, $userid); |
||
| 156 | |||
| 157 | try { |
||
| 158 | $query->execute(); |
||
| 159 | } catch (PDOException $e) { |
||
| 160 | die($e->getMessage()); |
||
| 161 | } |
||
| 162 | |||
| 163 | return $query->fetchAll(); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function get_tickets_by_resolver($username) |
||
| 178 | } |
||
| 179 | |||
| 180 | public function get_tickets_by_resolver_not_closed($username) |
||
| 193 | } |
||
| 194 | |||
| 195 | public function get_tickets_by_status($ticketstatus) |
||
| 207 | } |
||
| 208 | |||
| 209 | public function get_tickets_by_date($start_date, $end_date) |
||
| 210 | { |
||
| 211 | $start_date = strtotime($start_date); |
||
| 212 | $end_date = strtotime($end_date); |
||
| 213 | $query = $this->db->prepare('SELECT * FROM `tickets` WHERE `documenteddate` >= ? AND `documenteddate` <= ? ORDER BY `ticketnumber` DESC'); |
||
| 214 | $query->bindValue(1, $start_date); |
||
| 215 | $query->bindValue(2, $end_date); |
||
| 216 | |||
| 217 | try { |
||
| 218 | $query->execute(); |
||
| 219 | } catch (PDOException $e) { |
||
| 220 | die($e->getMessage()); |
||
| 221 | } |
||
| 222 | |||
| 223 | return $query->fetchAll(); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function search_closed_ticket($fromperiod, $toperiod) |
||
| 227 | { |
||
| 228 | $query = $this->db->prepare("SELECT * FROM `tickets` WHERE `documenteddate` >= ? AND `documenteddate` <= ? AND `ticketstatus` = 'Closed' ORDER BY `documenteddate` DESC"); |
||
| 229 | $query->bindValue(1, $fromperiod); |
||
| 230 | $query->bindValue(2, $toperiod); |
||
| 231 | |||
| 232 | try { |
||
| 233 | $query->execute(); |
||
| 234 | } catch (PDOException $e) { |
||
| 235 | die($e->getMessage()); |
||
| 236 | } |
||
| 237 | |||
| 238 | return $query->fetchAll(); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function count_tickets_by_status() |
||
| 242 | { |
||
| 243 | $query = $this->db->prepare('SELECT ticketstatus, count(*) as total FROM `tickets` GROUP BY ticketstatus'); |
||
| 244 | |||
| 245 | try { |
||
| 246 | $query->execute(); |
||
| 247 | } catch (PDOException $e) { |
||
| 248 | die($e->getMessage()); |
||
| 249 | } |
||
| 250 | |||
| 251 | return $query->fetchAll(); |
||
| 252 | } |
||
| 253 | |||
| 254 | public function count_resolved_tickets_by_month() |
||
| 255 | { |
||
| 256 | $sql = "SELECT Month(FROM_UNIXTIME(`documenteddate`)) as Bulan, Count(*) as Total FROM `tickets` WHERE (`ticketstatus`='Resolved' OR `ticketstatus`='Closed') AND FROM_UNIXTIME(`documenteddate`) >= CURDATE() - INTERVAL 1 YEAR GROUP BY Month(FROM_UNIXTIME(`documenteddate`))"; |
||
| 257 | $query = $this->db->prepare($sql); |
||
| 258 | |||
| 259 | try { |
||
| 260 | $query->execute(); |
||
| 261 | } catch (PDOException $e) { |
||
| 262 | die($e->getMessage()); |
||
| 263 | } |
||
| 264 | |||
| 265 | return $query->fetchAll(); |
||
| 266 | } |
||
| 267 | |||
| 268 | public function count_inprogress_tickets_by_month() |
||
| 269 | { |
||
| 270 | $sql = "SELECT Month(FROM_UNIXTIME(`documenteddate`)) as Bulan, Count(*) as Total FROM `tickets` WHERE (`ticketstatus`='Assigned' OR `ticketstatus`='Pending') AND FROM_UNIXTIME(`documenteddate`) >= CURDATE() - INTERVAL 1 YEAR GROUP BY Month(FROM_UNIXTIME(`documenteddate`))"; |
||
| 271 | $query = $this->db->prepare($sql); |
||
| 272 | |||
| 273 | try { |
||
| 274 | $query->execute(); |
||
| 275 | } catch (PDOException $e) { |
||
| 276 | die($e->getMessage()); |
||
| 277 | } |
||
| 278 | |||
| 279 | return $query->fetchAll(); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function get_last_ticket() |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /* |
||
| 296 | public function notify_assignee($id,$ticketnumber,$email_assignee) |
||
| 297 | { if (substr(php_uname(), 0, 7) == "Windows"){ |
||
| 298 | $cmd = "D:\mowes_portable\www\helpdesk\batch\sendemail.bat"; |
||
| 299 | $WshShell = new COM("WScript.Shell"); |
||
| 300 | $oExec = $WshShell->Run("cmd /C $cmd", 0, false); |
||
| 301 | return $oExec == 0 ? true : false; |
||
| 302 | } |
||
| 303 | else { |
||
| 304 | $cmd = "php /batch/sendemail.bat"; |
||
| 305 | exec($cmd . " > /dev/null &"); |
||
| 306 | } |
||
| 307 | }*/ |
||
| 308 | |||
| 309 | public function log_tickets($id, $sla, $reporteddate, $reportedby, $telp, $email, $problemsummary, $problemdetail, $ticketstatus, $assignee, $assigneddate, $pendingby, $pendingdate, $resolution, $resolvedby, $resolveddate, $closedby, $closeddate, $changes, $changeby, $processby, $processdate, $comment) |
||
| 310 | { |
||
| 311 | $changedate = time(); |
||
| 312 | $querystring = 'INSERT INTO `log_tickets` (`id`,`sla`,`reporteddate`, `reportedby`, `telp`, `email`, `problemsummary`,`problemdetail`,`ticketstatus`,`assignee`,`assigneddate`,`pendingby`,`pendingdate`,`resolution`,`resolvedby`,`resolveddate`,`closedby`,`closeddate`,`changes`,`changeby`,`changedate`,`processby`,`processdate`,`comment`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; |
||
| 313 | $query = $this->db->prepare($querystring); |
||
| 314 | $query->bindValue(1, $id); |
||
| 315 | $query->bindValue(2, $sla); |
||
| 316 | $query->bindValue(3, $reporteddate); |
||
| 317 | $query->bindValue(4, $reportedby); |
||
| 318 | $query->bindValue(5, $telp); |
||
| 319 | $query->bindValue(6, $email); |
||
| 320 | $query->bindValue(7, $problemsummary); |
||
| 321 | $query->bindValue(8, $problemdetail); |
||
| 322 | $query->bindValue(9, $ticketstatus); |
||
| 323 | $query->bindValue(10, $assignee); |
||
| 324 | $query->bindValue(11, $assigneddate); |
||
| 325 | $query->bindValue(12, $pendingby); |
||
| 326 | $query->bindValue(13, $pendingdate); |
||
| 327 | $query->bindValue(14, $resolution); |
||
| 328 | $query->bindValue(15, $resolvedby); |
||
| 329 | $query->bindValue(16, $resolveddate); |
||
| 330 | $query->bindValue(17, $closedby); |
||
| 331 | $query->bindValue(18, $closeddate); |
||
| 332 | $query->bindValue(19, $changes); |
||
| 333 | $query->bindValue(20, $changeby); |
||
| 334 | $query->bindValue(21, $changedate); |
||
| 335 | $query->bindValue(22, $processby); |
||
| 336 | $query->bindValue(23, $processdate); |
||
| 337 | $query->bindValue(24, $comment); |
||
| 338 | |||
| 339 | try { |
||
| 340 | $query->execute(); |
||
| 341 | } catch (PDOException $e) { |
||
| 342 | die($e->getMessage()); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | public function get_audit_trail($id) |
||
| 358 | } |
||
| 359 | } |
||
| 360 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.