| Total Complexity | 59 |
| Total Lines | 318 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Import 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 Import, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 65 | class Import |
||
| 66 | { |
||
| 67 | //Parent class for import script |
||
| 68 | use Importable, |
||
|
|
|||
| 69 | RegistersEventListeners; |
||
| 70 | |||
| 71 | public function __construct($file) |
||
| 72 | { |
||
| 73 | $this->sheetNames = []; |
||
| 74 | $this->file = $file; |
||
| 75 | $this->sheetData = []; |
||
| 76 | $this->template = false; |
||
| 77 | $this->worksheet = ''; |
||
| 78 | $this->failures = []; |
||
| 79 | $this->request = new Request; |
||
| 80 | $this->maleStudentsCount = 0; |
||
| 81 | $this->femaleStudentsCount = 0; |
||
| 82 | $this->highestRow = 0; |
||
| 83 | $this->isValidSheet = true; |
||
| 84 | $this->uniqueUid = new UniqueUid(); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function limit(): int |
||
| 88 | { |
||
| 89 | $highestColumn = $this->worksheet->getHighestDataColumn(3); |
||
| 90 | $higestRow = 0; |
||
| 91 | for ($row = $this->startRow(); $row <= $this->highestRow; $row++) { |
||
| 92 | $rowData = $this->worksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); |
||
| 93 | if (isEmptyRow(reset($rowData))) { |
||
| 94 | continue; |
||
| 95 | } else { |
||
| 96 | $higestRow += 1; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | return $higestRow; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function validateColumns($column, $existingColumns) |
||
| 103 | { |
||
| 104 | $columns = Config::get('excel.columns'); |
||
| 105 | $error = \Illuminate\Validation\ValidationException::withMessages([]); |
||
| 106 | $this->failures = []; |
||
| 107 | if (($column !== "") && (!in_array($column, $columns))) { |
||
| 108 | $this->isValidSheet = false; |
||
| 109 | $this->error[] = 'Unsupported column found ,remove:' . $column; |
||
| 110 | $this->failure = new Failure(3, 'remark', $this->error, [null]); |
||
| 111 | $this->failures = new \Maatwebsite\Excel\Validators\ValidationException($error, [$this->failure]); |
||
| 112 | } |
||
| 113 | if (is_object($this->failures)) { |
||
| 114 | throw $this->failures; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | public function validateColumnsToMap($existingColumns) |
||
| 119 | { |
||
| 120 | $columns = Config::get('excel.columns'); |
||
| 121 | $optional_columns = Config::get('excel.optional_columns'); |
||
| 122 | $columns = array_diff ($columns,$optional_columns); |
||
| 123 | $error = \Illuminate\Validation\ValidationException::withMessages([]); |
||
| 124 | $this->failures = []; |
||
| 125 | foreach ($columns as $column) { |
||
| 126 | if (($column !== "") && (!in_array($column, $existingColumns))) { |
||
| 127 | $this->isValidSheet = false; |
||
| 128 | $this->error[] = 'Missing Column :' . $column . ' Not found'; |
||
| 129 | $this->failure = new Failure(3, 'remark', $this->error, [null]); |
||
| 130 | $this->failures = new \Maatwebsite\Excel\Validators\ValidationException($error, [$this->failure]); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | if (is_object($this->failures)) { |
||
| 134 | throw $this->failures; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | public function batchSize(): int |
||
| 141 | { |
||
| 142 | $highestColumn = $this->worksheet->getHighestDataColumn(3); |
||
| 143 | $higestRow = 1; |
||
| 144 | for ($row = $this->startRow(); $row <= $this->highestRow; $row++) { |
||
| 145 | $rowData = $this->worksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); |
||
| 146 | if (isEmptyRow(reset($rowData))) { |
||
| 147 | continue; |
||
| 148 | } else { |
||
| 149 | $higestRow += 1; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | if ($higestRow == 0) { |
||
| 153 | exit; |
||
| 154 | } else { |
||
| 155 | return $higestRow; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | public function startRow(): int |
||
| 161 | { |
||
| 162 | return 3; |
||
| 163 | } |
||
| 164 | |||
| 165 | public function headingRow(): int |
||
| 166 | { |
||
| 167 | return 2; |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | protected function formateDate($row, $column, $format = 'Y-m-d') |
||
| 172 | { |
||
| 173 | try { |
||
| 174 | if (!empty($row[$column]) && ($row[$column] !== null)) { |
||
| 175 | switch (gettype($row[$column])) { |
||
| 176 | case 'string': |
||
| 177 | $row[$column] = preg_replace('/[^A-Za-z0-9\-]/', '-', $row[$column]); |
||
| 178 | $row[$column] = date($format, strtotime($row[$column])); //date($row[$column]); |
||
| 179 | $row[$column] = \Carbon\Carbon::createFromFormat($format, $row[$column]); |
||
| 180 | break; |
||
| 181 | case 'double'; |
||
| 182 | $row[$column] = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[$column]); |
||
| 183 | break; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | return $row; |
||
| 187 | } catch (Exception $e) { |
||
| 188 | $error = \Illuminate\Validation\ValidationException::withMessages([]); |
||
| 189 | $failure = new Failure(3, 'remark', [0 => 'Template is not valid for upload, use the template given in the system ' . $row[$column] . ' Not a valid date formate'], [null]); |
||
| 190 | $failures = [0 => $failure]; |
||
| 191 | throw new \Maatwebsite\Excel\Validators\ValidationException($error, $failures); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | |||
| 196 | protected function mapFields($row) |
||
| 197 | { |
||
| 198 | |||
| 199 | $keys = array_keys($row); |
||
| 200 | |||
| 201 | $this->validateColumnsToMap($keys); |
||
| 202 | array_walk($keys, array($this, 'validateColumns')); |
||
| 203 | $row = $this->formateDate($row, 'date_of_birth_yyyy_mm_dd'); |
||
| 204 | $row = $this->formateDate($row, 'bmi_date_yyyy_mm_dd'); |
||
| 205 | $row = $this->formateDate($row, 'start_date_yyyy_mm_dd'); |
||
| 206 | $row = $this->formateDate($row, 'fathers_date_of_birth_yyyy_mm_dd'); |
||
| 207 | $row = $this->formateDate($row, 'mothers_date_of_birth_yyyy_mm_dd'); |
||
| 208 | $row = $this->formateDate($row, 'guardians_date_of_birth_yyyy_mm_dd'); |
||
| 209 | |||
| 210 | $row['admission_no'] = str_pad($row['admission_no'], 4, '0', STR_PAD_LEFT); |
||
| 211 | |||
| 212 | if (array_key_exists('identity_type', $row)) { |
||
| 213 | if ($row['identity_type'] == 'BC' && (!empty($row['birth_divisional_secretariat'])) && ($row['identity_number'] !== null) && $row['date_of_birth_yyyy_mm_dd'] !== null) { |
||
| 214 | $row['identity_number'] = str_pad($row['identity_number'], 4, '0', STR_PAD_LEFT); |
||
| 215 | // dd(($row['date_of_birth_yyyy_mm_dd'])); |
||
| 216 | $BirthDivision = Area_administrative::where('name', 'like', '%' . $row['birth_divisional_secretariat'] . '%')->where('area_administrative_level_id', '=', 5)->first(); |
||
| 217 | if ($BirthDivision !== null) { |
||
| 218 | $BirthArea = Area_administrative::where('name', 'like', '%' . $row['birth_registrar_office_as_in_birth_certificate'] . '%') |
||
| 219 | ->where('parent_id', '=', $BirthDivision->id)->first(); |
||
| 220 | if ($BirthArea !== null) { |
||
| 221 | $row['identity_number'] = $BirthArea->id . '' . $row['identity_number'] . '' . substr($row['date_of_birth_yyyy_mm_dd']->format("yy"), -2) . '' . $row['date_of_birth_yyyy_mm_dd']->format("m"); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | } |
||
| 226 | } |
||
| 227 | return $row; |
||
| 228 | } |
||
| 229 | |||
| 230 | protected function checkKeys($key, $count, $row) |
||
| 231 | { |
||
| 232 | if (array_key_exists($key, $row)) { |
||
| 233 | return true; |
||
| 234 | } else { |
||
| 235 | $error = \Illuminate\Validation\ValidationException::withMessages([]); |
||
| 236 | $failure = new Failure($count, 'remark', [0 => 'Template is not valid for upload, use the template given in the system ' . $key, ' Is missing form the template'], [null]); |
||
| 237 | $failures = [0 => $failure]; |
||
| 238 | new \Maatwebsite\Excel\Validators\ValidationException($error, $failures); |
||
| 239 | }; |
||
| 240 | } |
||
| 241 | |||
| 242 | |||
| 243 | public function array(array $array) |
||
| 244 | { |
||
| 245 | $this->sheetData[] = $array; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param mixed $row |
||
| 250 | * @return array |
||
| 251 | * @throws \Exception |
||
| 252 | */ |
||
| 253 | public function map($row): array |
||
| 254 | { |
||
| 255 | $row = $this->mapFields($row); |
||
| 256 | return $row; |
||
| 257 | } |
||
| 258 | |||
| 259 | |||
| 260 | public function validateClass() |
||
| 261 | { |
||
| 262 | |||
| 263 | $institutionClass = Institution_class::find($this->file['institution_class_id']); |
||
| 264 | $totalMaleStudents = $institutionClass->total_male_students; |
||
| 265 | $totalFemaleStudents = $institutionClass->total_female_students; |
||
| 266 | $totalStudents = $totalMaleStudents + $totalFemaleStudents; |
||
| 267 | |||
| 268 | $exceededStudents = ($totalStudents + $this->limit()) > $institutionClass->no_of_students ? true : false; |
||
| 269 | if ($exceededStudents == true) { |
||
| 270 | $error = \Illuminate\Validation\ValidationException::withMessages([]); |
||
| 271 | $failure = new Failure(3, 'remark', ['Class student count exceeded! Max number of students is' . $institutionClass->no_of_students], [null]); |
||
| 272 | $failures = [0 => $failure]; |
||
| 273 | throw new \Maatwebsite\Excel\Validators\ValidationException($error, $failures); |
||
| 274 | Log::info('email-sent', [$this->file]); |
||
| 275 | } else { |
||
| 276 | return true; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | public function getNode() |
||
| 281 | { |
||
| 282 | return $this->file['node']; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param array $options |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getUniqueOpenemisId($options = []) |
||
| 290 | { |
||
| 291 | return Uuid::generate(4); |
||
| 292 | } |
||
| 293 | |||
| 294 | |||
| 295 | protected function updateSubjectCount($subject) |
||
| 302 | ]); |
||
| 303 | } |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * |
||
| 308 | */ |
||
| 309 | protected function setStudentSubjects($subject) |
||
| 323 | ]; |
||
| 324 | } |
||
| 325 | |||
| 326 | protected function insertSubject($subject) |
||
| 332 | } |
||
| 333 | |||
| 334 | public function createOrUpdateGuardian($row,$student,$param){ |
||
| 342 | } |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | protected function setRelation($param,$guardian){ |
||
| 347 | switch($param){ |
||
| 348 | case 'father': |
||
| 349 | return 1; |
||
| 350 | case 'mother': |
||
| 351 | return 2; |
||
| 352 | case 'guardian': |
||
| 353 | return 3; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | protected function setGender($row){ |
||
| 369 | } |
||
| 370 | |||
| 371 | protected function insertOrUpdateSubjects($row,$student,$institution){ |
||
| 372 | $mandatorySubject = Institution_class_subject::getMandatorySubjects($this->file['institution_class_id']); |
||
| 373 | $subjects = getMatchingKeys($row); |
||
| 374 | $optionalSubjects = Institution_class_subject::getStudentOptionalSubject($subjects, $student, $row, $institution); |
||
| 375 | $allSubjects = array_merge_recursive($optionalSubjects, $mandatorySubject); |
||
| 376 | if (!empty($allSubjects)) { |
||
| 377 | $allSubjects = unique_multidim_array($allSubjects, 'institution_subject_id'); |
||
| 378 | $this->student = $student; |
||
| 383 | } |
||
| 384 | } |
||
| 386 |