Test Setup Failed
Push — master ( ad3b88...b2f2cc )
by Mohamed
15:01 queued 07:46
created

ValidatorExtended::validateIdentity()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 8
nop 4
dl 0
loc 22
rs 9.3888
1
<?php
2
3
namespace App\Providers;
4
5
namespace App\Providers;
6
7
use App\Models\Academic_period;
8
use App\Models\Area_administrative;
9
use App\Models\Security_user;
10
use App\Models\Identity_type;
11
use Illuminate\Support\Carbon;
12
use Illuminate\Support\Facades\Date;
13
use Illuminate\Support\Facades\Validator;
14
use Illuminate\Validation\Validator as IlluminateValidator;
15
use Illuminate\Support\Facades\Log;
16
use App\Models\Education_grade;
17
use App\Models\Institution_class;
18
use App\Models\Institution_class_grade;
19
use App\Models\Institution_class_student;
20
21
class ValidatorExtended extends IlluminateValidator
22
{
23
24
    private $_custom_messages = array(
25
        "admission_age" => "The age limit not match with admission age for this class",
26
        "birth_place" => 'The Birth place combination in not valid, refer the Birth Registrar office only belongs to Divisional Secretariat',
27
        'user_unique' => 'The Birth place combination in not valid, refer the Birth Registrar office only belongs to Divisional Secretariat',
28
        "is_bc" => "The Birth Certificate number is not valid",
29
        "nic" => "NIC number is Not valid",
30
        "is_student_in_class" => "The Student ID is not belong to this class",
31
        "bmi" => "The record must have BMI information",
32
        "identity" => "Identity number format should match with Identity type",
33
    );
34
35
    public function __construct(
36
        $translator,
37
        $data,
38
        $rules,
39
        $messages = array(),
40
        $customAttributes = array()
41
    ) {
42
        parent::__construct(
43
            $translator,
44
            $data,
45
            $rules,
46
            $messages,
47
            $customAttributes
48
        );
49
        $this->_set_custom_stuff();
50
    }
51
52
    protected function _set_custom_stuff()
53
    {
54
        //setup our custom error messages
55
        $this->setCustomMessages($this->_custom_messages);
56
    }
57
58
    /**
59
     * this will validate admission age limit of the student
60
     *
61
     * admission age validation
62
     */
63
64
    protected function validateAdmissionAge($attribute, $value, $parameters, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

64
    protected function validateAdmissionAge($attribute, $value, $parameters, /** @scrutinizer ignore-unused */ $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        $institutionClass = Institution_class::find($parameters[0]);
67
        $institutionGrade = Institution_class_grade::where('institution_class_id', '=', $institutionClass->id)->first();
68
        if (!empty($institutionClass)) {
69
            $gradeEntity = Education_grade::where('id', '=', $institutionGrade->education_grade_id)->first();
0 ignored issues
show
Bug introduced by
The property education_grade_id does not seem to exist on App\Models\Institution_class_grade. 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...
70
            $academicPeriod = Academic_period::find($institutionClass->academic_period_id);
0 ignored issues
show
Bug introduced by
The property academic_period_id does not seem to exist on App\Models\Institution_class. 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...
71
            if (empty($value)) {
72
                return false;
73
            } elseif ($gradeEntity !== null) {
74
                $admissionAge = (($gradeEntity->admission_age) * 12) - 1;
0 ignored issues
show
Bug introduced by
The property admission_age does not seem to exist on App\Models\Education_grade. 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...
75
                $to = $academicPeriod->start_date;
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on App\Models\Academic_period. 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...
76
                $diff_in_months = $to->diffInMonths($value);
77
                $ageOfStudent = $diff_in_months;
78
                $enrolmentMaximumAge = $admissionAge + 120;
79
                return ($ageOfStudent <= $enrolmentMaximumAge) && ($ageOfStudent >= $admissionAge);
80
            } else {
81
                return false;
82
            }
83
        } else {
84
            $this->_custom_messages['admission_age'] = 'given' . $attribute . 'Not found';
85
            $this->_set_custom_stuff();
86
            return false;
87
        }
88
    }
89
90
    protected function validateHW($attribute, $value)
91
    {
92
93
        if (is_numeric($value)) {
94
            if ($value < 10) {
95
                $this->_custom_messages['bmi'] =  $attribute . ' is must greater than 10';
96
                $this->_set_custom_stuff();
97
                return false;
98
            } elseif ($value > 250) {
99
                $this->_custom_messages['bmi'] =  $attribute . ' is must smaller than 250';
100
                $this->_set_custom_stuff();
101
                return false;
102
            }
103
        } else {
104
            $this->_custom_messages['bmi'] =  $attribute . ' is must a valid numeric';
105
            $this->_set_custom_stuff();
106
            return false;
107
        }
108
        return true;
109
    }
110
111
    protected function validateBmi($attribute, $value, $parameters)
112
    {
113
        $bmiGrades =  ['G1', 'G4', 'G7', 'G10'];
114
        $institutionGrade = Institution_class_grade::where('institution_class_id', '=', $parameters[0])
115
            ->join('education_grades', 'institution_class_grades.education_grade_id', 'education_grades.id')
116
            ->first();
117
        $educationGrade =  Education_grade::where('id', '=', $institutionGrade->education_grade_id)->first();
0 ignored issues
show
Bug introduced by
The property education_grade_id does not seem to exist on App\Models\Institution_class_grade. 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...
118
        if (in_array($institutionGrade->code, $bmiGrades)) {
0 ignored issues
show
Bug introduced by
The property code does not seem to exist on App\Models\Institution_class_grade. 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...
119
            if (!empty($value)) {
120
                if (($attribute == 'bmi_height') || ('bmi_weight')) {
121
                    return $this->validateHW($attribute, $value);
122
                }
123
            } else {
124
                $this->_custom_messages['bmi'] =  $attribute . ' is required for ' . $educationGrade->name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on App\Models\Education_grade. 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...
125
                $this->_set_custom_stuff();
126
                return false;
127
            }
128
        } elseif (!empty($value)) {
129
            if (($attribute == 'bmi_height') || ('bmi_weight')) {
130
                return $this->validateHW($attribute, $value);
131
            }
132
        } else {
133
            return true;
134
        }
135
    }
136
137
    protected function validateBirthPlace($attribute, $value, $perameters, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $perameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

137
    protected function validateBirthPlace($attribute, $value, /** @scrutinizer ignore-unused */ $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

137
    protected function validateBirthPlace(/** @scrutinizer ignore-unused */ $attribute, $value, $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
    {
139
        foreach ($validator->getData() as $data) {
140
            if ($data['identity_type'] == 'BC' && key_exists('birth_divisional_secretariat', $data)) {
141
                $BirthDivision = Area_administrative::where('name', '=',  '%' . $data['birth_divisional_secretariat'] . '%')->where('area_administrative_level_id', '=', 5); //
142
                if ($BirthDivision->count() > 0) {
143
                    $BirthArea = Area_administrative::where('name', '=', '%' . $value . '%') //$data['birth_registrar_office_as_in_birth_certificate']
144
                        ->where('parent_id', '=', $BirthDivision->first()->id)->count();
145
                    return $BirthArea  > 0;
146
                } elseif (key_exists('birth_divisional_secretariat', $data) && (!key_exists('birth_registrar_office_as_in_birth_certificate', $data))) {
147
                    $this->_custom_messages['birth_place'] = 'birth_registrar_office_as_in_birth_certificate required with BC';
148
                    $this->_set_custom_stuff();
149
                    return false;
150
                } else {
151
                    return true;
152
                }
153
            } else {
154
                return true;
155
            }
156
        }
157
    }
158
159
    protected function validateIsStudentInClass($attribute, $value, $perameters, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

159
    protected function validateIsStudentInClass($attribute, $value, $perameters, /** @scrutinizer ignore-unused */ $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

159
    protected function validateIsStudentInClass(/** @scrutinizer ignore-unused */ $attribute, $value, $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
    {
161
        $student =  Security_user::where('openemis_no', '=', $value);
162
        if ($student->count() > 0) {
163
            $student = $student->first()->toArray();
164
            $check =  Institution_class_student::where('student_id', '=', $student['id'])->where('institution_class_id', '=', $perameters[0])->count();
165
            if ($check == 1) {
166
                return true;
167
            } else {
168
                return false;
169
            }
170
        } else {
171
            return false;
172
        }
173
    }
174
175
   
176
177
    protected function validateIdentity($attribute, $value, $perameters, $validator)
178
    {
179
        $valid = true;
180
        foreach ($validator->getData() as $data) {
181
            switch($data[$perameters[0]]){
182
                case 'BC':
183
                    $valid = preg_match('/^([0-9]{10,12})$/i', $value);
184
                    break;
185
                case 'NIC':
186
                    $valid = preg_match('/^([0-9]{9}[VX]|[0-9]{12})$/i', $value);
187
                    break;
188
                default:
189
                    $valid = true;    
190
            }
191
        }
192
        
193
        if (!$valid) {
194
            $this->_custom_messages['nic'] = $attribute . ' is not valid,  Please check the NIC number';
195
            $this->_set_custom_stuff();
196
            return false;
197
        } else {
198
            return true;
199
        }
200
    }
201
202
    protected function validateUserUnique($attribute, $value, $perameters, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $perameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

202
    protected function validateUserUnique($attribute, $value, /** @scrutinizer ignore-unused */ $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

202
    protected function validateUserUnique(/** @scrutinizer ignore-unused */ $attribute, $value, $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
    {
204
        foreach ($validator->getData() as $data) {
205
            $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first();
206
            if ($identityType !== null && ($value !== null)) {
207
                if ($identityType->national_code === 'BC') {
0 ignored issues
show
Bug introduced by
The property national_code does not seem to exist on App\Models\Identity_type. 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...
208
                    return $this->checkUnique($value, $data, $identityType);
209
                } elseif ($identityType->national_code === 'NIC') {
210
                    return $this->checkUnique($value, $data, $identityType);
211
                }
212
            } elseif (($value == null) || $value == "") {
213
                return true;
214
            }
215
        }
216
    }
217
218
    protected function validateIsBc($attribute, $value, $perameters, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

218
    protected function validateIsBc($attribute, /** @scrutinizer ignore-unused */ $value, $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

218
    protected function validateIsBc(/** @scrutinizer ignore-unused */ $attribute, $value, $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $perameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

218
    protected function validateIsBc($attribute, $value, /** @scrutinizer ignore-unused */ $perameters, $validator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
219
    {
220
        foreach ($validator->getData() as $data) {
221
            $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first();
222
            if (($identityType !== null) && ($identityType !== "")) {
223
                if (($identityType->national_code) === 'BC') {
0 ignored issues
show
Bug introduced by
The property national_code does not seem to exist on App\Models\Identity_type. 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...
224
                    return (strlen((string) $data['identity_number']) < 7);
225
                } else {
226
                    return true;
227
                }
228
            } else {
229
                return true;
230
            }
231
        }
232
    }
233
234
    protected function checkUnique($value, $data, $identityType)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

234
    protected function checkUnique($value, /** @scrutinizer ignore-unused */ $data, $identityType)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
235
    {
236
        $isUnique = Security_user::where('identity_number', '=', $value)->where('identity_type_id', '=', $identityType->id);
237
        if ($isUnique->count() > 0) {
238
            $this->_custom_messages['user_unique'] = 'The identity number already in use. User ID is : ' . $isUnique->first()->openemis_no;
0 ignored issues
show
Bug introduced by
The property openemis_no 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...
239
            $this->_set_custom_stuff();
240
            return false;
241
        } else {
242
            return true;
243
        }
244
    }
245
246
    protected function IsBc($data, $value)
247
    {
248
        $identityType = Identity_type::where('national_code', 'like', '%' . $data['identity_type'] . '%')->first();
249
        if ($identityType !== null) {
250
            if (($identityType->national_code) === 'BC' && strlen((string) $value) < 8) {
0 ignored issues
show
Bug introduced by
The property national_code does not seem to exist on App\Models\Identity_type. 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...
251
                return false;
252
            } else {
253
                return true;
254
            }
255
        } else {
256
            return true;
257
        }
258
    }
259
}
260