| Total Complexity | 51 |
| Total Lines | 305 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GridSchedule 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 GridSchedule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class GridSchedule |
||
| 11 | { |
||
| 12 | use ShiftSchedule; |
||
| 13 | use \Processor; |
||
| 14 | |||
| 15 | protected $department; |
||
| 16 | protected $shifts; |
||
| 17 | protected $ssheat; |
||
| 18 | |||
| 19 | public function __construct($department, $shifts) |
||
| 20 | { |
||
| 21 | $this->department = $department; |
||
| 22 | $this->shifts = $shifts; |
||
| 23 | $this->ssheat = $this->createSpreadSheet(); |
||
| 24 | } |
||
| 25 | |||
| 26 | protected function isVolunteerAdmin() |
||
| 27 | { |
||
| 28 | return true; |
||
| 29 | } |
||
| 30 | |||
| 31 | protected function getSimpleHour($hour) |
||
| 32 | { |
||
| 33 | if($hour < 12) |
||
| 34 | { |
||
| 35 | if($hour === 0) |
||
| 36 | { |
||
| 37 | return '12a'; |
||
| 38 | } |
||
| 39 | return $hour.'a'; |
||
| 40 | } |
||
| 41 | if($hour === 12) |
||
| 42 | { |
||
| 43 | return $hour.'p'; |
||
| 44 | } |
||
| 45 | return ($hour - 12).'p'; |
||
| 46 | } |
||
| 47 | |||
| 48 | protected function grayOutUnused($hourCount, $rowCount, $sheat) |
||
| 49 | { |
||
| 50 | for($i = 0; $i < $hourCount; $i++) |
||
| 51 | { |
||
| 52 | for($j = 0; $j < $rowCount; $j++) |
||
| 53 | { |
||
| 54 | $cell = $sheat->getCellByColumnAndRow($i + 2, $j + 4); |
||
| 55 | if($cell->isInMergeRange()) |
||
| 56 | { |
||
| 57 | continue; |
||
| 58 | } |
||
| 59 | else |
||
| 60 | { |
||
| 61 | $style = $cell->getStyle(); |
||
| 62 | $style->getBorders()->getAllBorders()->setBorderStyle(false); |
||
| 63 | $style->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_LIGHTGRAY); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | protected function setShiftNameInCell($sheat, $col, $row, $shift) |
||
| 70 | { |
||
| 71 | if(isset($shift['participant'])) |
||
| 72 | { |
||
| 73 | $sheat->setCellValueByColumnAndRow($col, $row, $this->getParticipantDiplayName($shift['participant'])); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | protected function getHoursArrays($hour, $hourCount) |
||
| 78 | { |
||
| 79 | $simpleHours = array(); |
||
| 80 | $militaryHours = array(); |
||
| 81 | for($i = 0; $i < $hourCount; $i++) |
||
| 82 | { |
||
| 83 | array_push($simpleHours, $this->getSimpleHour($hour)); |
||
| 84 | array_push($militaryHours, $hour.':00'); |
||
| 85 | $hour++; |
||
| 86 | if($hour === 24) |
||
| 87 | { |
||
| 88 | $hour = 0; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | return array($simpleHours, $militaryHours); |
||
| 92 | } |
||
| 93 | |||
| 94 | protected function createShiftCell($sheat, $col, $row, $shift) |
||
| 95 | { |
||
| 96 | $sheat->mergeCellsByColumnAndRow($col, $row, $col + $shift['length'] - 1, $row); |
||
| 97 | $this->setShiftNameInCell($sheat, $col, $row, $shift); |
||
| 98 | } |
||
| 99 | |||
| 100 | protected function getRowForShift($roleID, $rows, $col, $sheat) |
||
| 101 | { |
||
| 102 | $i = 1; |
||
| 103 | $firstRow = array_search($roleID, $rows); |
||
| 104 | $cell = $sheat->getCellByColumnAndRow($col, $firstRow + 4); |
||
| 105 | if($cell->isInMergeRange()) |
||
| 106 | { |
||
| 107 | while($rows[$firstRow + $i] === $roleID) |
||
| 108 | { |
||
| 109 | $cell = $sheat->getCellByColumnAndRow($col, $firstRow + 4 + $i); |
||
| 110 | if(!$cell->isInMergeRange()) |
||
| 111 | { |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | $i++; |
||
| 115 | } |
||
| 116 | return $firstRow + 4 + $i; |
||
| 117 | } |
||
| 118 | return $firstRow + 4; |
||
| 119 | } |
||
| 120 | |||
| 121 | protected function createSpreadSheet() |
||
| 247 | } |
||
| 248 | |||
| 249 | public function getBuffer($type) |
||
| 250 | { |
||
| 251 | if($type === 'XLSX') |
||
| 252 | { |
||
| 253 | $writer = new Xlsx($this->ssheat); |
||
| 254 | $content = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; |
||
| 255 | $extension = '.xlsx'; |
||
| 256 | } |
||
| 257 | else if($type === 'PDF') |
||
| 258 | { |
||
| 259 | $this->ssheat->getActiveSheet()->getPageSetup()->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE); |
||
| 260 | $writer = new mpdf($this->ssheat); |
||
| 261 | $writer->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE); |
||
| 262 | $content = 'application/pdf'; |
||
| 263 | $extension = '.pdf'; |
||
| 264 | } |
||
| 265 | else |
||
| 266 | { |
||
| 267 | return array('error'=> true, 'msg'=>'Unknown type specified: '.$type); |
||
| 268 | } |
||
| 269 | ob_start(); |
||
| 270 | $writer->save('php://output'); |
||
| 271 | $str = ob_get_clean(); |
||
| 272 | return array('content-type'=>$content, 'extension'=>$extension, 'buffer'=>$str); |
||
| 273 | } |
||
| 274 | |||
| 275 | public function shiftSort($a, $b) |
||
| 278 | } |
||
| 279 | |||
| 280 | public function groupSort($a, $b) |
||
| 281 | { |
||
| 282 | $aArr = explode(' ', $a); |
||
| 283 | $bArr = explode(' ', $b); |
||
| 284 | if($aArr[1] === 'PM' && $bArr[1] === 'AM') |
||
| 285 | { |
||
| 286 | return 1; |
||
| 287 | } |
||
| 288 | else if($aArr[1] === 'AM' && $bArr[1] === 'PM') |
||
| 289 | { |
||
| 290 | return -1; |
||
| 291 | } |
||
| 292 | return strcmp($a, $b); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function shiftTimeSort($a, $b) |
||
| 315 | } |
||
| 316 | } |
||
| 317 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 318 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.