| Total Complexity | 43 |
| Total Lines | 352 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 5 | Features | 2 |
Complex classes like Security_user 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 Security_user, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Security_user extends Model |
||
| 14 | { |
||
| 15 | |||
| 16 | use SoftDeletes; |
||
| 17 | |||
| 18 | public const CREATED_AT = 'created'; |
||
| 19 | public const UPDATED_AT = 'modified'; |
||
| 20 | /** |
||
| 21 | * The database table used by the model. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | |||
| 26 | public $timestamps = true; |
||
| 27 | |||
| 28 | protected $softDelete = true; |
||
| 29 | |||
| 30 | protected $table = 'security_users'; |
||
| 31 | |||
| 32 | protected $appends = [ |
||
| 33 | 'special_need_name' |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Attributes that should be mass-assignable. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $fillable = [ |
||
| 42 | 'openemis_no', |
||
| 43 | 'first_name', |
||
| 44 | 'last_name', |
||
| 45 | 'address', |
||
| 46 | 'address_area_id', |
||
| 47 | 'birthplace_area_id', |
||
| 48 | 'gender_id', |
||
| 49 | 'remember_token', |
||
| 50 | 'date_of_birth', |
||
| 51 | 'nationality_id', |
||
| 52 | 'identity_type_id', |
||
| 53 | 'identity_number', |
||
| 54 | 'is_student', |
||
| 55 | 'modified_user_id', |
||
| 56 | 'modified', |
||
| 57 | 'created_user_id', |
||
| 58 | 'created', |
||
| 59 | 'username', |
||
| 60 | 'password', |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The attributes excluded from the model's JSON form. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $hidden = [ |
||
| 69 | 'password', |
||
| 70 | 'modified_user_id', |
||
| 71 | 'middle_name', |
||
| 72 | 'third_name', |
||
| 73 | 'modified', |
||
| 74 | 'created_user_id', |
||
| 75 | 'created' |
||
| 76 | |||
| 77 | ]; |
||
| 78 | |||
| 79 | |||
| 80 | public function getSpecialNeedNameAttribute() |
||
| 81 | { |
||
| 82 | return optional($this->special_needs())->special_need_difficulty_id; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The attributes that should be casted to native types. |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $casts = []; |
||
| 91 | |||
| 92 | public function institutionStudents() |
||
| 93 | { |
||
| 94 | return $this->hasOne(Institution_student::class, 'student_id'); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function institutionStudentsClass() |
||
| 98 | { |
||
| 99 | return $this->hasOne(Institution_student::class, 'student_id'); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The attributes that should be mutated to dates. |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $dates = ['date_of_birth', 'date_of_death', 'last_login', 'modified', 'created']; |
||
| 108 | |||
| 109 | public function rules() |
||
| 110 | { |
||
| 111 | return [ |
||
| 112 | 'identity_number' => [ |
||
| 113 | 'required', |
||
| 114 | 'unique:security_users,identity_number', |
||
| 115 | ] |
||
| 116 | ]; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getAuthPassword() |
||
| 120 | { |
||
| 121 | return $this->password; |
||
|
|
|||
| 122 | } |
||
| 123 | |||
| 124 | public function uploads() |
||
| 125 | { |
||
| 126 | return $this->hasMany('App\Models\Upload'); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function class() |
||
| 130 | { |
||
| 131 | return $this->belongsTo('App\Models\Institution_class_student', 'id', 'student_id'); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function special_needs() |
||
| 135 | { |
||
| 136 | return $this->hasMany('App\Models\User_special_need', 'id', 'security_user_id'); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function genUUID() |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * First level search for students |
||
| 147 | * |
||
| 148 | * @param array $student |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function getMatches($student) |
||
| 152 | { |
||
| 153 | return $this |
||
| 154 | ->where('gender_id', $student['gender'] + 1) |
||
| 155 | ->where('institutions.code', $student['schoolid']) |
||
| 156 | ->where('date_of_birth', $student['b_date']) |
||
| 157 | ->join('institution_students', 'security_users.id', 'institution_students.student_id') |
||
| 158 | ->join('institutions', 'institution_students.institution_id', 'institutions.id') |
||
| 159 | ->get()->toArray(); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * First level search for students |
||
| 164 | * |
||
| 165 | * @param array $student |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | public function getStudentCount($student) |
||
| 169 | { |
||
| 170 | return $this |
||
| 171 | ->where('gender_id', $student['gender'] + 1) |
||
| 172 | ->where('institutions.code', $student['schoolid']) |
||
| 173 | ->where('date_of_birth', $student['b_date']) |
||
| 174 | ->join('institution_students', 'security_users.id', 'institution_students.student_id') |
||
| 175 | ->join('institutions', 'institution_students.institution_id', 'institutions.id') |
||
| 176 | ->count(); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * insert student data from examination |
||
| 181 | * @input array |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function insertExaminationStudent($student) |
||
| 185 | { |
||
| 186 | $this->uniqueUserId = new Unique_user_id(); |
||
| 187 | $this->uniqueUId = new UniqueUid(); |
||
| 188 | $uniqueId = $this->uniqueUId::getUniqueAlphanumeric(); |
||
| 189 | $studentData = [ |
||
| 190 | 'username' => str_replace('-', '', $uniqueId), |
||
| 191 | 'openemis_no' => $uniqueId, // Openemis no is unique field, in case of the duplication it will failed |
||
| 192 | 'first_name' => $student['f_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
| 193 | 'last_name' => genNameWithInitials($student['f_name']), |
||
| 194 | 'gender_id' => $student['gender'] + 1, |
||
| 195 | 'date_of_birth' => $student['b_date'], |
||
| 196 | 'address' => $student['pvt_address'], |
||
| 197 | 'is_student' => 1, |
||
| 198 | 'updated_from' => 'doe', |
||
| 199 | 'created' => now(), |
||
| 200 | 'created_user_id' => 1 |
||
| 201 | ]; |
||
| 202 | try { |
||
| 203 | $id = $this->insertGetId($studentData); |
||
| 204 | $studentData['id'] = $id; |
||
| 205 | $this->uniqueUserId->updateOrInsertRecord($studentData); |
||
| 206 | return $studentData; |
||
| 207 | } catch (\Exception $th) { |
||
| 208 | Log::error($th->getMessage()); |
||
| 209 | // in case of duplication of the Unique ID this will recursive. |
||
| 210 | $sis_student['openemis_no'] = $this->uniqueUId::getUniqueAlphanumeric(); |
||
| 211 | $this->insertExaminationStudent($student, $sis_student); |
||
| 212 | } |
||
| 213 | return $studentData; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Update the existing student's data |
||
| 218 | * |
||
| 219 | * @param array $student |
||
| 220 | * @param array $sis_student |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public function updateExaminationStudent($student, $sis_student) |
||
| 224 | { |
||
| 225 | $this->uniqueUserId = new Unique_user_id(); |
||
| 226 | $this->uniqueUId = new UniqueUid(); |
||
| 227 | // regenerate unique id if it's not available |
||
| 228 | $uniqueId = ($this->uniqueUId::isValidUniqueId($sis_student['openemis_no'], 9)) ? $sis_student['openemis_no'] : $this->uniqueUId::getUniqueAlphanumeric(); |
||
| 229 | |||
| 230 | $studentData = [ |
||
| 231 | 'username' => str_replace('-', '', $uniqueId), |
||
| 232 | 'openemis_no' => $uniqueId, // Openemis no is unique field, in case of the duplication it will failed |
||
| 233 | 'first_name' => $student['f_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
| 234 | 'last_name' => genNameWithInitials($student['f_name']), |
||
| 235 | 'date_of_birth' => $student['b_date'], |
||
| 236 | 'address' => $student['pvt_address'], |
||
| 237 | 'updated_from' => 'doe', |
||
| 238 | 'modified' => now() |
||
| 239 | ]; |
||
| 240 | |||
| 241 | try { |
||
| 242 | self::where('id', $sis_student['student_id'])->update($studentData); |
||
| 243 | $studentData['id'] = $sis_student['student_id']; |
||
| 244 | $this->uniqueUserId->updateOrInsertRecord($studentData); |
||
| 245 | return $studentData; |
||
| 246 | } catch (\Exception $th) { |
||
| 247 | Log::error($th); |
||
| 248 | // in case of duplication of the Unique ID this will recursive. |
||
| 249 | $sis_student['openemis_no'] = $this->uniqueUId::getUniqueAlphanumeric(); |
||
| 250 | $this->updateExaminationStudent($student, $sis_student); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | public static function createOrUpdateStudentProfile($row, $prefix, $file) |
||
| 255 | { |
||
| 256 | try { |
||
| 257 | $uniqueUid = new UniqueUid(); |
||
| 258 | $BirthArea = Area_administrative::where('name', 'like', '%' . $row['birth_registrar_office_as_in_birth_certificate'] . '%')->first(); |
||
| 259 | $nationalityId = Nationality::where('name', 'like', '%' . $row['nationality'] . '%')->first(); |
||
| 260 | $identityType = Identity_type::where('national_code', 'like', '%' . $row['identity_type'] . '%')->first(); |
||
| 261 | |||
| 262 | $date = $row['date_of_birth_yyyy_mm_dd']; |
||
| 263 | $identityType = $identityType !== null ? $identityType->id : null; |
||
| 264 | $nationalityId = $nationalityId !== null ? $nationalityId->id : null; |
||
| 265 | |||
| 266 | $BirthArea = $BirthArea !== null ? $BirthArea->id : null; |
||
| 267 | $openemisNo = $uniqueUid::getUniqueAlphanumeric(); |
||
| 268 | $preferred_name = null; |
||
| 269 | if (array_key_exists('preferred_name', $row)) { |
||
| 270 | $preferred_name = $row['preferred_name']; |
||
| 271 | } |
||
| 272 | switch ($prefix) { |
||
| 273 | case 'create': |
||
| 274 | return Security_user::create([ |
||
| 275 | 'username' => str_replace('-', '', $openemisNo), |
||
| 276 | 'openemis_no' => $openemisNo, |
||
| 277 | 'first_name' => $row['full_name'], // here we save full name in the column of first name. re reduce breaks of the system. |
||
| 278 | 'last_name' => genNameWithInitials($row['full_name']), |
||
| 279 | 'preferred_name' => $preferred_name, |
||
| 280 | 'gender_id' => $row['gender_mf'], |
||
| 281 | 'date_of_birth' => $date, |
||
| 282 | 'address' => $row['address'], |
||
| 283 | 'birthplace_area_id' => $BirthArea, |
||
| 284 | 'nationality_id' => $nationalityId, |
||
| 285 | 'identity_type_id' => $identityType, |
||
| 286 | 'identity_number' => $row['identity_number'], |
||
| 287 | 'is_student' => 1, |
||
| 288 | 'created_user_id' => $file['security_user_id'] |
||
| 289 | ]); |
||
| 290 | case 'update': |
||
| 291 | if (!is_null($row['student_id'])) { |
||
| 292 | $studentInfo = Security_user::where('openemis_no',trim($row['student_id']))->first(); |
||
| 293 | self::query()->where('openemis_no', $row['student_id']) |
||
| 294 | ->update([ |
||
| 295 | '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. |
||
| 296 | 'last_name' => $row['full_name'] ? genNameWithInitials($row['full_name']) : genNameWithInitials($studentInfo['first_name']), |
||
| 297 | 'preferred_name' => $preferred_name, |
||
| 298 | 'gender_id' => is_numeric($row['gender_mf']) ? $row['gender_mf'] : $studentInfo['gender_id'], |
||
| 299 | 'date_of_birth' => $date ? $date : $studentInfo['date_of_birth'], |
||
| 300 | 'address' => $row['address'] ? $row['address'] : $studentInfo['address'], |
||
| 301 | 'birthplace_area_id' => $row['birth_registrar_office_as_in_birth_certificate'] ? $BirthArea : $studentInfo['birthplace_area_id'], |
||
| 302 | 'nationality_id' => $row['nationality'] ? $nationalityId : $studentInfo['nationality_id'], |
||
| 303 | 'identity_type_id' => $row['identity_type'] ? $identityType : $studentInfo['identity_type_id'], |
||
| 304 | 'identity_number' => $row['identity_number'] ? $row['identity_number'] : $studentInfo['identity_number'], |
||
| 305 | 'is_student' => 1, |
||
| 306 | 'modified' => now(), |
||
| 307 | 'modified_user_id' => $file['security_user_id'] |
||
| 308 | ]); |
||
| 309 | return $studentInfo; |
||
| 310 | } |
||
| 311 | break; |
||
| 312 | } |
||
| 313 | } catch (\Exception $e) { |
||
| 314 | dd($e); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | public static function createOrUpdateGuardianProfile($row, $prefix, $file) |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.