Total Complexity | 59 |
Total Lines | 410 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 4 | Features | 0 |
Complex classes like StudentUpdate 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 StudentUpdate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
58 | class StudentUpdate extends Import implements ToModel, WithStartRow, WithHeadingRow, WithMultipleSheets, WithEvents, WithMapping, WithLimit, WithBatchInserts, WithValidation , SkipsOnFailure , SkipsOnError{ |
||
59 | |||
60 | use Importable, |
||
|
|||
61 | RegistersEventListeners, |
||
62 | SkipsFailures, |
||
63 | SkipsErrors; |
||
64 | |||
65 | |||
66 | public function sheets(): array { |
||
67 | return [ |
||
68 | 'Update Students' => $this |
||
69 | ]; |
||
70 | } |
||
71 | |||
72 | public function registerEvents(): array { |
||
73 | // TODO: Implement registerEvents() method. |
||
74 | return [ |
||
75 | BeforeSheet::class => function(BeforeSheet $event) { |
||
76 | $this->sheetNames[] = $event->getSheet()->getTitle(); |
||
77 | $this->worksheet = $event->getSheet(); |
||
78 | $worksheet = $event->getSheet(); |
||
79 | $this->highestRow = $worksheet->getHighestDataRow('B'); |
||
80 | }, |
||
81 | BeforeImport::class => function (BeforeImport $event) { |
||
82 | $event->getReader()->getDelegate()->setActiveSheetIndex(2); |
||
83 | $this->highestRow = ($event->getReader()->getDelegate()->getActiveSheet()->getHighestDataRow('B')); |
||
84 | if ($this->highestRow < 3) { |
||
85 | $error = \Illuminate\Validation\ValidationException::withMessages([]); |
||
86 | $failure = new Failure(3, 'remark', [0 => 'No enough rows!'], [null]); |
||
87 | $failures = [0 => $failure]; |
||
88 | throw new \Maatwebsite\Excel\Validators\ValidationException($error, $failures); |
||
89 | } |
||
90 | } |
||
91 | ]; |
||
92 | } |
||
93 | |||
94 | |||
95 | public function model(array $row) { |
||
96 | |||
97 | try { |
||
98 | $institutionClass = Institution_class::find($this->file['institution_class_id']); |
||
99 | $institution = $institutionClass->institution_id; |
||
100 | |||
101 | |||
102 | if (!array_filter($row)) { |
||
103 | return nulll; |
||
104 | } |
||
105 | |||
106 | if (!empty($institutionClass)) { |
||
107 | $mandatorySubject = Institution_class_subject::getMandetorySubjects($this->file['institution_class_id']); |
||
108 | $subjects = getMatchingKeys($row); |
||
109 | $genderId = null; |
||
110 | switch ($row['gender_mf']) { |
||
111 | case 'M': |
||
112 | $row['gender_mf'] = 1; |
||
113 | $this->maleStudentsCount += 1; |
||
114 | break; |
||
115 | case 'F': |
||
116 | $row['gender_mf'] = 2; |
||
117 | $this->femaleStudentsCount += 1; |
||
118 | break; |
||
119 | } |
||
120 | |||
121 | $BirthArea = Area_administrative::where('name', 'like', '%' . $row['birth_registrar_office_as_in_birth_certificate'] . '%')->first(); |
||
122 | $nationalityId = Nationality::where('name', 'like', '%' . $row['nationality'] . '%')->first(); |
||
123 | $identityType = Identity_type::where('national_code', 'like', '%' . $row['identity_type'] . '%')->first(); |
||
124 | $academicPeriod = Academic_period::where('name', '=', $row['academic_period'])->first(); |
||
125 | |||
126 | |||
127 | $date = $row['date_of_birth_yyyy_mm_dd']; |
||
128 | |||
129 | $identityType = $identityType !== null ? $identityType->id : null; |
||
130 | $nationalityId = $nationalityId !== null ? $nationalityId->id : null; |
||
131 | |||
132 | $BirthArea = $BirthArea !== null ? $BirthArea->id : null; |
||
133 | $identityNUmber = $row['identity_number']; |
||
134 | |||
135 | //create students data |
||
136 | \Log::debug('Security_user'); |
||
137 | |||
138 | $studentInfo = Security_user::where('openemis_no', '=', $row['student_id'])->first(); |
||
139 | Security_user::where('openemis_no', $studentInfo['openemis_no']) |
||
140 | ->update([ |
||
141 | 'first_name' => $row['full_name'] ? $row['full_name'] : $studentInfo['first_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
142 | 'last_name' => $row['full_name'] ? genNameWithInitials($row['full_name']) : genNameWithInitials($studentInfo['first_name']), |
||
143 | 'gender_id' => $genderId ? $genderId : $studentInfo['gender_id'], |
||
144 | 'date_of_birth' => $date ? $date : $studentInfo['date_of_birth'], |
||
145 | 'address' => $row['address'] ? $row['address'] : $studentInfo['address'], |
||
146 | 'birthplace_area_id' => $row['birth_registrar_office_as_in_birth_certificate'] ? $BirthArea : $studentInfo['birthplace_area_id'], |
||
147 | 'nationality_id' => $row['nationality'] ? $nationalityId : $studentInfo['nationality_id'], |
||
148 | 'identity_type_id' => $row['identity_type'] ? $identityType : $studentInfo['identity_type_id'], |
||
149 | 'identity_number' => $row['identity_number'] ? $identityNUmber : $studentInfo['identity_number'], |
||
150 | 'is_student' => 1, |
||
151 | 'modified' => now(), |
||
152 | 'modified_user_id' => $this->file['security_user_id'] |
||
153 | ]); |
||
154 | |||
155 | $student = Institution_class_student::where('student_id', '=', $studentInfo->id)->first(); |
||
156 | |||
157 | if(!empty($row['admission_no']) && !empty($academicPeriod)){ |
||
158 | Institution_student::where('student_id','=',$studentInfo->id) |
||
159 | ->where('institution_id','=', $institution) |
||
160 | ->where('academic_period_id','=',$academicPeriod->id) |
||
161 | ->update(['admission_id'=> $row['admission_no']]); |
||
162 | } |
||
163 | |||
164 | if (!empty($row['special_need'])) { |
||
165 | |||
166 | $specialNeed = Special_need_difficulty::where('name', '=', $row['special_need'])->first(); |
||
167 | $data = [ |
||
168 | 'special_need_date' => now(), |
||
169 | 'security_user_id' => $student->student_id, |
||
170 | 'special_need_type_id' => 1, |
||
171 | 'special_need_difficulty_id' => $specialNeed->id, |
||
172 | 'created_user_id' => $this->file['security_user_id'] |
||
173 | ]; |
||
174 | |||
175 | $check = User_special_need::isDuplicated($data); |
||
176 | if ($check) { |
||
177 | User_special_need::create($data); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | |||
182 | |||
183 | if (!empty($row['bmi_height']) && (!empty($row['bmi_weight']))) { |
||
184 | |||
185 | // convert Meeter to CM |
||
186 | $hight = $row['bmi_height'] / 100; |
||
187 | |||
188 | //calculate BMI |
||
189 | $bodyMass = ($row['bmi_weight']) / pow($hight, 2); |
||
190 | |||
191 | $bmiAcademic = Academic_period::where('name', '=', $row['bmi_academic_period'])->first(); |
||
192 | $count = User_body_mass::where('academic_period_id' ,'=',$bmiAcademic->id ) |
||
193 | ->where('security_user_id','=',$student->student_id)->count(); |
||
194 | |||
195 | \Log::debug('User_body_mass'); |
||
196 | if(!($count > 0)){ |
||
197 | User_body_mass::create([ |
||
198 | 'height' => $row['bmi_height'], |
||
199 | 'weight' => $row['bmi_weight'], |
||
200 | 'date' => $row['bmi_date_yyyy_mm_dd'], |
||
201 | 'body_mass_index' => $bodyMass, |
||
202 | 'academic_period_id' => $bmiAcademic->id, |
||
203 | 'security_user_id' => $student->student_id, |
||
204 | 'created_user_id' => $this->file['security_user_id'] |
||
205 | ]); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | if (!empty($row['fathers_full_name']) && ($row['fathers_date_of_birth_yyyy_mm_dd'] !== null)) { |
||
210 | $AddressArea = Area_administrative::where('name', 'like', '%' . $row['fathers_address_area'] . '%')->first(); |
||
211 | $nationalityId = Nationality::where('name', 'like', '%' . $row['fathers_nationality'] . '%')->first(); |
||
212 | $identityType = Identity_type::where('national_code', 'like', '%' . $row['fathers_identity_type'] . '%')->first(); |
||
213 | $openemisFather = $this->uniqueUid::getUniqueAlphanumeric(); |
||
214 | |||
215 | $identityType = ($identityType !== null) ? $identityType->id : null; |
||
216 | $nationalityId = $nationalityId !== null ? $nationalityId->id : null; |
||
217 | |||
218 | $father = null; |
||
219 | if (!empty($row['fathers_identity_number'])) { |
||
220 | $father = Security_user::where('identity_type_id', '=', $nationalityId) |
||
221 | ->where('identity_number', '=', $row['fathers_identity_number'])->first(); |
||
222 | } |
||
223 | |||
224 | |||
225 | if ($father === null) { |
||
226 | $data = [ |
||
227 | 'username' => str_replace('-','',$openemisFather), |
||
228 | 'openemis_no' => $openemisFather, |
||
229 | 'first_name' => $row['fathers_full_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
230 | 'last_name' => genNameWithInitials($row['fathers_full_name']), |
||
231 | 'gender_id' => 1, |
||
232 | 'date_of_birth' => $row['fathers_date_of_birth_yyyy_mm_dd'], |
||
233 | 'address' => $row['fathers_address'], |
||
234 | 'address_area_id' => $AddressArea->id, |
||
235 | 'nationality_id' => $nationalityId, |
||
236 | 'identity_type_id' => $identityType, |
||
237 | 'identity_number' => $row['fathers_identity_number'], |
||
238 | 'is_guardian' => 1, |
||
239 | 'created_user_id' => $this->file['security_user_id'] |
||
240 | ]; |
||
241 | $father = Security_user::create($data); |
||
242 | |||
243 | $father['guardian_relation_id'] = 1; |
||
244 | if (array_key_exists('fathers_phone', $row)) { |
||
245 | $father['contact'] = $row['fathers_phone']; |
||
246 | User_contact::createOrUpdate($father,$this->file['security_user_id']); |
||
247 | } |
||
248 | Student_guardian::createStudentGuardian($student, $father, $this->file['security_user_id']); |
||
249 | } else { |
||
250 | Security_user::where('id', '=', $father->id) |
||
251 | ->update(['is_guardian' => 1]); |
||
252 | $father['guardian_relation_id'] = 1; |
||
253 | if (array_key_exists('fathers_phone', $row)) { |
||
254 | $father['contact'] = $row['fathers_phone']; |
||
255 | User_contact::createOrUpdate($father,$this->file['security_user_id']); |
||
256 | } |
||
257 | Student_guardian::createStudentGuardian($student, $father, $this->file['security_user_id']); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | if (!empty($row['mothers_full_name']) && ($row['mothers_date_of_birth_yyyy_mm_dd'] !== null)) { |
||
262 | $AddressArea = Area_administrative::where('name', 'like', '%' . $row['mothers_address_area'] . '%')->first(); |
||
263 | $nationalityId = Nationality::where('name', 'like', '%' . $row['mothers_nationality'] . '%')->first(); |
||
264 | $identityType = Identity_type::where('national_code', 'like', '%' . $row['mothers_identity_type'] . '%')->first(); |
||
265 | $openemisMother = $this->uniqueUid::getUniqueAlphanumeric(); |
||
266 | |||
267 | $identityType = $identityType !== null ? $identityType->id : null; |
||
268 | $nationalityId = $nationalityId !== null ? $nationalityId->id : null; |
||
269 | |||
270 | $mother = null; |
||
271 | |||
272 | if (!empty($row['mothers_identity_number'])) { |
||
273 | $mother = Security_user::where('identity_type_id', '=', $nationalityId) |
||
274 | ->where('identity_number', '=', $row['mothers_identity_number'])->first(); |
||
275 | } |
||
276 | |||
277 | if ($mother === null) { |
||
278 | $mother = Security_user::create([ |
||
279 | 'username' => str_replace('-','',$openemisMother), |
||
280 | 'openemis_no' => $openemisMother, |
||
281 | 'first_name' => $row['mothers_full_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
282 | 'last_name' => genNameWithInitials($row['mothers_full_name']), |
||
283 | 'gender_id' => 2, |
||
284 | 'date_of_birth' => $row['mothers_date_of_birth_yyyy_mm_dd'], |
||
285 | 'address' => $row['mothers_address'], |
||
286 | 'address_area_id' => $AddressArea->id, |
||
287 | 'nationality_id' => $nationalityId, |
||
288 | 'identity_type_id' => $identityType, |
||
289 | 'identity_number' => $row['mothers_identity_number'], |
||
290 | 'is_guardian' => 1, |
||
291 | 'created_user_id' => $this->file['security_user_id'] |
||
292 | ]); |
||
293 | |||
294 | $mother['guardian_relation_id'] = 2; |
||
295 | if (array_key_exists('mothers_phone', $row)) { |
||
296 | $mother['contact'] = $row['mothers_phone']; |
||
297 | User_contact::createOrUpdate($mother,$this->file['security_user_id']); |
||
298 | } |
||
299 | Student_guardian::createStudentGuardian($student, $mother, $this->file['security_user_id']); |
||
300 | } else { |
||
301 | Security_user::where('id', '=', $mother->id) |
||
302 | ->update(['is_guardian' => 1]); |
||
303 | $mother['guardian_relation_id'] = 2; |
||
304 | if (array_key_exists('mothers_phone', $row)) { |
||
305 | $mother['contact'] = $row['mothers_phone']; |
||
306 | User_contact::createOrUpdate($mother,$this->file['security_user_id']); |
||
307 | } |
||
308 | Student_guardian::createStudentGuardian($student, $mother, $this->file['security_user_id']); |
||
309 | } |
||
310 | } |
||
311 | |||
312 | |||
313 | if (!empty($row['guardians_full_name']) && ($row['guardians_date_of_birth_yyyy_mm_dd'] !== null)) { |
||
314 | $genderId = $row['guardians_gender_mf'] == 'M' ? 1 : 2; |
||
315 | $AddressArea = Area_administrative::where('name', 'like', '%' . $row['guardians_address_area'] . '%')->first(); |
||
316 | $nationalityId = Nationality::where('name', 'like', '%' . $row['guardians_nationality'] . '%')->first(); |
||
317 | $identityType = Identity_type::where('national_code', 'like', '%' . $row['guardians_identity_type'] . '%')->first(); |
||
318 | $openemisGuardian = $this->uniqueUid::getUniqueAlphanumeric(); |
||
319 | |||
320 | $identityType = $identityType !== null ? $identityType->id : null; |
||
321 | $nationalityId = $nationalityId !== null ? $nationalityId->id : null; |
||
322 | |||
323 | $guardian = null; |
||
324 | |||
325 | if (!empty($row['guardians_identity_number'])) { |
||
326 | $guardian = Security_user::where('identity_type_id', '=', $nationalityId) |
||
327 | ->where('identity_number', '=', $row['guardians_identity_number'])->first(); |
||
328 | } |
||
329 | |||
330 | if ($guardian === null) { |
||
331 | $guardian = Security_user::create([ |
||
332 | 'username' => str_replace('-','',$openemisGuardian), |
||
333 | 'openemis_no' => $openemisGuardian, |
||
334 | 'first_name' => $row['guardians_full_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
335 | 'last_name' => genNameWithInitials($row['guardians_full_name']), |
||
336 | 'gender_id' => $genderId, |
||
337 | 'date_of_birth' => $row['guardians_date_of_birth_yyyy_mm_dd'], |
||
338 | 'address' => $row['guardians_address'], |
||
339 | 'address_area_id' => $AddressArea->id, |
||
340 | // 'birthplace_area_id' => $BirthArea->id, |
||
341 | 'nationality_id' => $nationalityId, |
||
342 | 'identity_type_id' => $identityType, |
||
343 | 'identity_number' => $row['guardians_identity_number'], |
||
344 | 'is_guardian' => 1, |
||
345 | 'created_user_id' => $this->file['security_user_id'] |
||
346 | ]); |
||
347 | |||
348 | $guardian['guardian_relation_id'] = 3; |
||
349 | if (array_key_exists('guardians_phone', $row)) { |
||
350 | $guardian['contact'] = $row['guardians_phone']; |
||
351 | User_contact::createOrUpdate($guardian,$this->file['security_user_id']); |
||
352 | } |
||
353 | Student_guardian::createStudentGuardian($student, $guardian, $this->file['security_user_id']); |
||
354 | } else { |
||
355 | Security_user::where('id', '=', $guardian->id) |
||
356 | ->update(['is_guardian' => 1]); |
||
357 | $guardian['guardian_relation_id'] = 3; |
||
358 | if (array_key_exists('guardians_phone', $row)) { |
||
359 | $guardian['contact'] = $row['guardians_phone']; |
||
360 | User_contact::createOrUpdate($guardian,$this->file['security_user_id']); |
||
361 | } |
||
362 | Student_guardian::createStudentGuardian($student, $guardian, $this->file['security_user_id']); |
||
363 | } |
||
364 | } |
||
365 | |||
366 | $optionalSubjects = Institution_class_subject::getStudentOptionalSubject($subjects, $student, $row, $institution); |
||
367 | |||
368 | $allSubjects = array_merge_recursive($optionalSubjects, $mandatorySubject); |
||
369 | // $stundetSubjects = $this->getStudentSubjects($student); |
||
370 | // $allSubjects = array_merge_recursive($newSubjects, $stundetSubjects); |
||
371 | |||
372 | if (!empty($allSubjects)) { |
||
373 | $allSubjects = unique_multidim_array($allSubjects, 'institution_subject_id'); |
||
374 | $this->student = $student; |
||
375 | $allSubjects = array_map(array($this,'setStudentSubjects'),$allSubjects); |
||
376 | // $allSubjects = array_unique($allSubjects,SORT_REGULAR); |
||
377 | $allSubjects = unique_multidim_array($allSubjects, 'education_subject_id'); |
||
378 | array_walk($allSubjects,array($this,'insertSubject')); |
||
379 | // Institution_subject_student::insert((array) $allSubjects); |
||
380 | // array_walk($allSubjects, array($this, 'updateSubjectCount')); |
||
381 | } |
||
382 | |||
383 | unset($allSubjects); |
||
384 | |||
385 | $totalStudents = Institution_class_student::getStudentsCount($this->file['institution_class_id']); |
||
386 | |||
387 | if ($totalStudents['total'] > $institutionClass->no_of_students) { |
||
388 | $error = \Illuminate\Validation\ValidationException::withMessages([]); |
||
389 | $failure = new Failure(3, 'rows', [3 => 'Class student count exceeded! Max number of students is ' . $institutionClass->no_of_students], [null]); |
||
390 | $failures = [0 => $failure]; |
||
391 | throw new \Maatwebsite\Excel\Validators\ValidationException($error, $failures); |
||
392 | Log::info('email-sent', [$this->file]); |
||
393 | } |
||
394 | |||
395 | |||
396 | Institution_class::where('id', '=', $institutionClass->id) |
||
397 | ->update([ |
||
398 | 'total_male_students' => $totalStudents['total_male_students'], |
||
399 | 'total_female_students' => $totalStudents['total_female_students']]); |
||
400 | } |
||
401 | } catch (\Maatwebsite\Excel\Validators\ValidationException $e) { |
||
402 | $error = \Illuminate\Validation\ValidationException::withMessages([]); |
||
403 | $failures = $e->failures(); |
||
404 | throw new \Maatwebsite\Excel\Validators\ValidationException($error, $failures); |
||
405 | Log::info('email-sent', [$e]); |
||
406 | } |
||
407 | unset($row); |
||
408 | } |
||
409 | |||
410 | public function getStudentSubjects($student) { |
||
411 | return Institution_subject_student::where('student_id', '=', $student->student_id) |
||
412 | ->where('institution_class_id', '=', $student->institution_class_id)->get()->toArray(); |
||
413 | } |
||
414 | |||
415 | protected function insertSubject($subject){ |
||
416 | if(!Institution_subject_student::isDuplicated($subject)) |
||
417 | Institution_subject_student::updateOrInsert($subject); |
||
418 | } |
||
419 | |||
420 | |||
421 | |||
422 | public function rules(): array { |
||
468 | ]; |
||
469 | } |
||
470 | |||
471 | } |
||
472 |