Test Setup Failed
Pull Request — master (#620)
by Mohamed
08:18
created

Institution_student::updateStudentArea()   D

Complexity

Conditions 23
Paths 9

Size

Total Lines 40
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 36
c 0
b 0
f 0
nc 9
nop 1
dl 0
loc 40
rs 4.1666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Models;
4
5
use Webpatser\Uuid\Uuid;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
11
12
class Institution_student extends Base_Model
13
{
14
15
    use SoftDeletes;
16
17
    public const CREATED_AT = 'created';
18
    public const UPDATED_AT = 'modified';
19
20
    /**
21
     * The database table used by the model.
22
     *
23
     * @var string
24
     */
25
    protected $table = 'institution_students';
26
27
    protected $softDelete = true;
28
29
30
    /**
31
     * @var bool
32
     */
33
    public $timestamps = true;
34
35
    /**
36
     * Attributes that should be mass-assignable.
37
     *
38
     * @var array
39
     */
40
    protected $fillable = ['student_status_id', 'student_id', 'education_grade_id', 'academic_period_id', 'start_date', 'start_year', 'end_date', 'end_year', 'institution_id', 'previous_institution_student_id', 'modified_user_id', 'modified', 'created_user_id', 'created', 'area_administrative_id', 'admission_id'];
41
42
    /**
43
     * The attributes excluded from the model's JSON form.
44
     *
45
     * @var array
46
     */
47
    protected $hidden = [];
48
49
    /**
50
     * The attributes that should be casted to native types.
51
     *
52
     * @var array
53
     */
54
    protected $casts = [];
55
56
    protected $maps = [
57
        'uuid' => 'id'
58
    ];
59
60
    /**
61
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
62
     */
63
    public function institutionStudents()
64
    {
65
        return $this->belongsTo('App\Security_user', 'student_id');
66
    }
67
68
    /**
69
     *
70
     */
71
    public static function boot()
72
    {
73
        parent::boot();
74
        self::creating(function ($model) {
75
            $model->id = (string) Uuid::generate(4);
76
            $model->created = now();
77
        });
78
    }
79
80
81
    /**
82
     * @var string
83
     */
84
    protected $primaryKey = 'id';
85
    protected $keyType = 'string';
86
87
    /**
88
     * @param $inputs
89
     * @return bool
90
     *
91
     *
92
     */
93
    public static function  isDuplicated($inputs)
94
    {
95
96
        $exists = self::where('student_id', '=', $inputs['student_id'])->count();
97
98
99
        return $exists;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $exists also could return the type Illuminate\Database\Eloquent\Builder|integer which is incompatible with the documented return type boolean.
Loading history...
100
    }
101
102
103
    /**
104
     * The attributes that should be mutated to dates.
105
     *
106
     * @var array
107
     */
108
    protected $dates = ['date_of_birth', 'date_of_death', 'last_login', 'modified', 'created', 'start_date', 'end_date', 'modified', 'created'];
109
110
    /**
111
     * get list of students which are going to be promoted
112
     *
113
     * @param $institutionGrade
114
     * @param $academicPeriod
115
     * @return array
116
     */
117
    public function getStudentListToPromote($institutionGrade, $academicPeriod)
118
    {
119
        return self::query()
120
            ->select(
121
                'institution_students.id',
122
                'institution_students.student_id',
123
                'institution_students.student_status_id',
124
                'institution_students.education_grade_id',
125
                'institution_students.education_grade_id',
126
                'institution_students.academic_period_id',
127
                'institution_students.institution_id',
128
                'institution_students.created_user_id',
129
                'institution_students.admission_id'
130
            )
131
            ->where('institution_students.institution_id', $institutionGrade['institution_id'])
132
            ->where('institution_students.education_grade_id', $institutionGrade['education_grade_id'])
133
            ->whereNull('institution_students.deleted_at')
134
            ->where('institution_students.academic_period_id', $academicPeriod->id)->get()->toArray();
135
    }
136
137
    /**
138
     * Create new Institution student from examination data
139
     *
140
     * @param [type] $student
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
141
     * @param [type] $admissionInfo
142
     * @return void
143
     */
144
    public static function createExaminationData($student, $admissionInfo)
145
    {
146
        $student['sp_center'] = gettype((int)$student['sp_center']) == 'integer' ?  $student['sp_center'] : 0;
147
        try {
148
            self::create([
149
                'id' => (string) Uuid::generate(4),
150
                'student_status_id' => 1,
151
                'student_id' => $student['id'],
152
                'taking_g5_exam' => $student['taking_g5_exam'],
153
                'taking_ol_exam' => $student['taking_ol_exam'],
154
                'taking_al_exam' => $student['taking_al_exam'],
155
                // Set special examination center
156
                'exam_center_for_special_education_g5' =>   $student['taking_g5_exam'] ? $student['sp_center'] : 0,
157
                'exam_center_for_special_education_ol' =>   $student['taking_ol_exam'] ? $student['sp_center'] : 0,
158
                'exam_center_for_special_education_al' =>   $student['taking_al_exam'] ? $student['sp_center'] : 0,
159
                'income_at_g5' => $student['a_income'],
160
                'education_grade_id' => $admissionInfo['education_grade']->id,
161
                'academic_period_id' => $admissionInfo['academic_period']->id,
162
                'start_date' => $admissionInfo['academic_period']->start_date,
163
                'start_year' => $admissionInfo['academic_period']->start_year,
164
                'end_date' => $admissionInfo['academic_period']->end_date,
165
                'end_year' => $admissionInfo['academic_period']->end_year,
166
                'institution_id' => $admissionInfo['instituion']->id,
167
                'updated_from' => 'doe',
168
                'created' => now(),
169
                'created_user_id' => 1
170
            ]);
171
        } catch (\Throwable $th) {
172
            Log::error($th);
173
        }
174
    }
175
176
    /**
177
     * Update new Institution student from examination data
178
     *
179
     * @param [type] $student
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
180
     * @param [type] $admissionInfo
181
     * @return void
182
     */
183
    public static function updateExaminationData($student, $admissionInfo)
184
    {
185
        $student['sp_center'] = gettype((int)$student['sp_center']) == 'integer' ?  $student['sp_center'] : 0;
186
        try {
187
            self::where([
188
                'student_id' => $student['student_id'],
189
                'education_grade_id' => $admissionInfo['education_grade']->id,
190
                'academic_period_id' => $admissionInfo['academic_period']->id,
191
            ])->update(
192
                [
193
                    'taking_g5_exam' => $student['taking_g5_exam'],
194
                    'taking_ol_exam' => $student['taking_ol_exam'],
195
                    'taking_al_exam' => $student['taking_al_exam'],
196
                    // Set special examination center
197
                    'exam_center_for_special_education_g5' =>   $student['taking_g5_exam'] ? $student['sp_center'] : 0,
198
                    'exam_center_for_special_education_ol' =>   $student['taking_ol_exam'] ? $student['sp_center'] : 0,
199
                    'exam_center_for_special_education_al' =>   $student['taking_al_exam'] ? $student['sp_center'] : 0,
200
                    'updated_from' => 'doe',
201
                    'income_at_g5' => $student['a_income'],
202
                    'modified' => now(),
203
                    'modified_user_id' => 1
204
                ]
205
            );
206
        } catch (\Throwable $th) {
207
            Log::error($th);
208
        }
209
    }
210
211
    public static function updateStudentArea(array $student){
212
        $father = Student_guardian::where('student_id',$student['student_id'])
213
        ->join('security_users as sg','guardian_id', 'sg.id')
214
        ->where('guardian_relation_id',1)
215
        ->get()->first();
216
217
        $mother = Student_guardian::where('student_id',$student['student_id'])
218
        ->join('security_users as sg','guardian_id', 'sg.id')
219
        ->where('guardian_relation_id',2)
220
        ->get()->first();
221
222
        $guardian = Student_guardian::where('student_id',$student['student_id'])
223
        ->join('security_users as sg','guardian_id', 'sg.id')
224
        ->where('guardian_relation_id',3)
225
        ->get()->first();
226
227
        if(!is_null($father) && is_null($mother) && is_null($guardian)){
228
            Security_user::where('id',$student['student_id'])
229
            ->update(['address_area_id' => $father->address_area_id]);
0 ignored issues
show
Bug introduced by
The property address_area_id does not seem to exist on App\Models\Student_guardian. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
230
        }elseif(!is_null($mother)  && (is_null($father) && is_null($guardian))){
231
            Security_user::where('id',$student['student_id'])
232
            ->update(['address_area_id' => $mother->address_area_id]);
233
        }elseif(!is_null($guardian) && is_null($father) && is_null($mother)){
234
            Security_user::where('id',$student['student_id'])
235
            ->update(['address_area_id' => $guardian->address_area_id]);
236
        }elseif(!is_null($mother)  && !is_null($father) && ($father->address_area_id ==  $mother->address_area_id)){
237
            Security_user::where('id',$student['student_id'])
238
            ->update(['address_area_id' => $mother->address_area_id]);
239
        }elseif(!is_null($mother)  && !is_null($father) && ($father->address_area_id !==  $mother->address_area_id) && !is_null($guardian)){
240
            Security_user::where('id',$student['student_id'])
241
            ->update(['address_area_id' => $guardian->address_area_id]);
242
        }elseif(!is_null($father) && $father->address == $student['address']){
0 ignored issues
show
Bug introduced by
The property address does not seem to exist on App\Models\Student_guardian. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
243
            Security_user::where('id',$student['student_id'])
244
            ->update(['address_area_id' => $guardian->address_area_id]);
245
        }elseif(!is_null($mother) && $mother->address == $student['address']){
246
            Security_user::where('id',$student['student_id'])
247
            ->update(['address_area_id' => $mother->address_area_id]);
248
        }elseif(!is_null($guardian) && $guardian->address == $student['address']){
249
            Security_user::where('id',$student['student_id'])
250
            ->update(['address_area_id' => $guardian->address_area_id]);
251
        }
252
    }
253
}
254