Test Setup Failed
Push — master ( 93675a...ba3342 )
by Mohamed
14:07 queued 12s
created

Security_user::updateExaminationStudent()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 2
Metric Value
cc 3
eloc 20
nc 6
nop 2
dl 0
loc 28
rs 9.6
c 7
b 1
f 2
1
<?php
2
3
namespace App\Models;
4
5
use Lsf\UniqueUid\UniqueUid;
6
use App\Models\Unique_user_id;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\Log;
9
10
class Security_user extends Model
11
{
12
13
    public const CREATED_AT = 'created';
14
    public const UPDATED_AT = 'modified';
15
    /**
16
     * The database table used by the model.
17
     *
18
     * @var string
19
     */
20
21
    public $timestamps = true;
22
23
    protected $table = 'security_users';
24
25
    protected $appends = [
26
        'special_need_name'
27
    ];
28
29
    /**
30
     * Attributes that should be mass-assignable.
31
     *
32
     * @var array
33
     */
34
    protected $fillable = [
35
        'openemis_no',
36
        'first_name',
37
        'last_name',
38
        'address',
39
        'address_area_id',
40
        'birthplace_area_id',
41
        'gender_id',
42
        'remember_token',
43
        'date_of_birth',
44
        'nationality_id',
45
        'identity_type_id',
46
        'identity_number',
47
        'is_student',
48
        'modified_user_id',
49
        'modified',
50
        'created_user_id',
51
        'created',
52
        'username',
53
        'password',
54
    ];
55
56
    /**
57
     * The attributes excluded from the model's JSON form.
58
     *
59
     * @var array
60
     */
61
    protected $hidden = [
62
        'password',
63
        'modified_user_id',
64
        'middle_name',
65
        'third_name',
66
        'modified',
67
        'created_user_id',
68
        'created'
69
70
    ];
71
72
73
    public function getSpecialNeedNameAttribute()
74
    {
75
        return optional($this->special_needs())->special_need_difficulty_id;
76
    }
77
78
    /**
79
     * The attributes that should be casted to native types.
80
     *
81
     * @var array
82
     */
83
    protected $casts = [];
84
85
    public function institutionStudents()
86
    {
87
        return $this->hasOne(Institution_student::class, 'student_id');
88
    }
89
90
    public function institutionStudentsClass()
91
    {
92
        return $this->hasOne(Institution_student::class, 'student_id');
93
    }
94
95
    /**
96
     * The attributes that should be mutated to dates.
97
     *
98
     * @var array
99
     */
100
    protected $dates = ['date_of_birth', 'date_of_death', 'last_login', 'modified', 'created'];
101
102
    public function rules()
103
    {
104
        return [
105
            'identity_number' => [
106
                'required',
107
                'unique:security_users,identity_number',
108
            ]
109
        ];
110
    }
111
112
    public function getAuthPassword()
113
    {
114
        return $this->password;
0 ignored issues
show
Bug introduced by
The property password does not seem to exist on App\Models\Security_user. 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...
115
    }
116
117
    public function uploads()
118
    {
119
        return $this->hasMany('App\Models\Upload');
120
    }
121
122
    public function class()
123
    {
124
        return $this->belongsTo('App\Models\Institution_class_student', 'id', 'student_id');
125
    }
126
127
    public function special_needs()
128
    {
129
        return $this->hasMany('App\Models\User_special_need', 'id', 'security_user_id');
130
    }
131
132
    public function genUUID()
133
    {
134
        $uuid = Uuid::generate(4);
0 ignored issues
show
Bug introduced by
The type App\Models\Uuid was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
135
        return str_split($uuid, '8')[0];
136
    }
137
138
    /**
139
     * First level search for students
140
     *
141
     * @param array $student
142
     * @return array
143
     */
144
    public function getMatches($student)
145
    {
146
        return $this->where([
147
            'gender_id' => $student['gender'] + 1, // DoE id differs form MoE id
148
            'date_of_birth' => $student['b_date'],
149
            'institutions.code' => $student['schoolid']
150
        ])
151
            ->join('institution_students', 'security_users.id', 'institution_students.student_id')
152
            ->join('institutions', 'institution_students.institution_id', 'institutions.id')
153
            ->get()->toArray();
154
    }
155
156
    /**
157
     * insert student data from examination
158
     * @input array
159
     * @return array
160
     */
161
    public function insertExaminationStudent($student)
162
    {
163
        $this->uniqueUserId = new Unique_user_id();
0 ignored issues
show
Bug introduced by
The property uniqueUserId does not seem to exist on App\Models\Security_user. 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...
164
        $this->uniqueUid = new UniqueUid();
0 ignored issues
show
Bug introduced by
The property uniqueUid does not seem to exist on App\Models\Security_user. 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...
165
        $uniqueId = $this->uniqueUId::getUniqueAlphanumeric();
0 ignored issues
show
Bug introduced by
The property uniqueUId does not exist on App\Models\Security_user. Did you mean uniqueUid?
Loading history...
166
        $studentData = [
167
            'username' => str_replace('-', '', $uniqueId),
168
            'openemis_no' => $uniqueId, // Openemis no is unique field, in case of the duplication it will failed
169
            'first_name' => $student['f_name'], // here we save full name in the column of first name. re reduce breaks of the system.
170
            'last_name' => genNameWithInitials($student['f_name']),
171
            'gender_id' => $student['gender'] + 1,
172
            'date_of_birth' => $student['b_date'],
173
            'address' => $student['pvt_address'],
174
            'is_student' => 1,
175
            'created' => now(),
176
            'created_user_id' => 1
177
        ];
178
        try {
179
            $id = $this->insertGetId($studentData);
180
            $studentData['id'] = $id;
181
            $this->uniqueUserId->updateOrInsertRecord($studentData);
182
            return $studentData;
183
        } catch (\Exception $th) {
184
            Log::error($th->getMessage());
185
            // in case of duplication of the Unique ID this will recursive.
186
            $this->insertExaminationStudent($student);
187
        }
188
        return $studentData;
189
    }
190
191
    /**
192
     * Update the existing student's data
193
     *
194
     * @param array $student
195
     * @param array $sis_student
196
     * @return array
197
     */
198
    public function updateExaminationStudent($student, $sis_student)
199
    {
200
        $this->uniqueUserId = new Unique_user_id();
0 ignored issues
show
Bug introduced by
The property uniqueUserId does not seem to exist on App\Models\Security_user. 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...
201
        $this->uniqueUid = new UniqueUid();
0 ignored issues
show
Bug introduced by
The property uniqueUid does not seem to exist on App\Models\Security_user. 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...
202
        // regenerate unique id if it's not available
203
        $uniqueId = $this->uniqueUId::isValidUniqueId($sis_student['openemis_no']) ?  $sis_student['openemis_no'] : $this->uniqueUId::getUniqueAlphanumeric();
0 ignored issues
show
Bug introduced by
The property uniqueUId does not exist on App\Models\Security_user. Did you mean uniqueUid?
Loading history...
204
205
        $studentData = [
206
            'id' => $sis_student['id'],
207
            'username' => str_replace('-', '', $uniqueId),
208
            'openemis_no' => $uniqueId, // Openemis no is unique field, in case of the duplication it will failed
209
            'first_name' => $student['f_name'], // here we save full name in the column of first name. re reduce breaks of the system.
210
            'last_name' => genNameWithInitials($student['f_name']),
211
            'date_of_birth' => $student['b_date'],
212
            'address' => $student['pvt_address'],
213
            'modified' => now()
214
        ];
215
216
        try {
217
            $this->update($studentData);
218
            $this->uniqueUserId->updateOrInsertRecord($studentData);
219
            return $studentData;
220
        } catch (\Exception $th) {
221
            Log::error($th->getMessage());
222
            // in case of duplication of the Unique ID this will recursive.
223
            $this->updateExaminationStudent($student, $sis_student);
224
        }
225
        return $studentData;
226
    }
227
228
}
229