CleanExamData::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Lsf\UniqueUid\UniqueUid;
6
use App\Models\Security_user;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\DB;
9
use App\Models\Examination_student;
10
use App\Models\Institution_student;
11
use Illuminate\Support\Facades\Artisan;
12
use App\Models\Institution_class_student;
13
use App\Models\Institution_student_admission;
14
15
class CleanExamData extends Command
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'examination:clean {chunk} {max} {type}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Clean SIS data duplication after Exam import';
30
31
    /**
32
     * Create a new command instance.
33
     *
34
     * @return void
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
        $this->output = new \Symfony\Component\Console\Output\ConsoleOutput();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Symfony\Component\Co...\Output\ConsoleOutput() of type Symfony\Component\Console\Output\ConsoleOutput is incompatible with the declared type Illuminate\Console\OutputStyle of property $output.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
    }
41
42
    /**
43
     * Execute the console command.
44
     *
45
     * @return mixed
46
     */
47
    public function handle()
48
    {
49
        $type = $this->argument('type');
50
        $students = array();
51
        if($type == 'invalid'){
52
            $students = DB::table('examination_students as es')
53
            ->whereRaw('CHAR_LENGTH(nsid) > 11')
54
            ->get()
55
            ->toArray();
56
        }elseif($type == 'duplicate'){
57
            $students = DB::table('security_users')
58
            ->where('updated_from', 'doe')
59
            ->get()
60
            ->toArray();
61
            
62
        }elseif($type == 'all'){
63
            $students = DB::table('examination_students')
64
            ->where('nsid','<>','')
65
            ->whereNotNull('nsid')
66
            ->get()
67
            ->toArray();
68
        }elseif($type == 'lock'){
69
            $students = DB::table('examination_students')
70
            ->whereNotNull('nsid')
71
            ->get()
72
            ->toArray();
73
        }
74
75
        $this->output->writeln('###########################################------Start cleanning exam records------###########################################');    
76
        if(count($students) > 0){
77
            $this->output->writeln('Total students to clean: '.  count($students));
78
            $students = array_chunk($students, $this->argument('chunk'));
0 ignored issues
show
Bug introduced by
$this->argument('chunk') of type array|null|string is incompatible with the type integer expected by parameter $length of array_chunk(). ( Ignorable by Annotation )

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

78
            $students = array_chunk($students, /** @scrutinizer ignore-type */ $this->argument('chunk'));
Loading history...
79
            $function = array($this, 'process');
80
            processParallel($function,$students, $this->argument('max'),$type);
81
        }else{
82
            $this->output->writeln('nothing to process, all are cleaned');
83
        }   
84
        $this->output->writeln('###########################################------Finished cleaning exam records------###########################################');
85
    }
86
87
    public function process($students,$count,$type){
88
       if($type === 'duplicate'){
89
        array_walk($students,array($this,'cleanData'));
90
       }elseif($type === 'lock'){
91
        array_walk($students,array($this,'lockData'));
92
       }
93
    }
94
95
    public function lockData($Student){
96
        $Student = json_decode(json_encode($Student),true);
97
        $student = Security_user::where('openemis_no',(string)$Student['nsid'])->first();
98
        if(!empty($student)){
99
            Institution_student::where('student_id', $student->id)->update(['updated_from' => 'doe']);
100
            Security_user::where('id', $student->id)->update(['updated_from' => 'doe']);
101
            $this->output->writeln('Locked:'. (string)$Student['nsid'] .':' . $student['openemis_no']);
102
        }
103
    }
104
105
106
    public function cleanData($Student)
107
    {
108
        $exist = Examination_student::where('nsid','=',  (string)$Student->openemis_no)->count();
109
        if (!$exist) {
110
            Institution_student::where('student_id', $Student->id)->delete();
111
            Institution_class_student::where('student_id', $Student->id)->delete();
112
            Institution_student_admission::where('student_id', $Student->id)->delete();
113
            Security_user::where('id', $Student->id)->delete();
114
            $this->output->writeln('cleaned:'.  (string)$Student->openemis_no);
115
        }else{
116
            
117
            Institution_student::where('student_id', $Student->id)->update(['updated_from' => 'doe']);
118
            Security_user::where('id', $Student->id)->update(['updated_from' => 'doe']);
119
            // if(!is_null($Student->deleted_at)){
120
            //     try{
121
            //         Institution_student::withTrashed()->where('student_id',$Student->id)->restore();
122
            //         Institution_class_student::withTrashed()->where('student_id',$Student->id)->restore();
123
            //         Institution_student_admission::withTrashed()->where('student_id',$Student->id)->restore();
124
            //         Security_user::withTrashed()->find($Student->id)->restore();
125
            //         $this->output->writeln('restored:'.  (string)$Student->openemis_no);
126
            //     }catch(\Exception $e){
127
            //     }    
128
            // }
129
        }
130
    }
131
132
    public function cleanInvalidData($Student)
133
    {
134
        $Student = (array) $Student;
135
        $exist = Examination_student::where('nsid',$Student['nsid'])->count();
0 ignored issues
show
Unused Code introduced by
The assignment to $exist is dead and can be removed.
Loading history...
136
        
137
        $this->uniqueUId = new UniqueUid();
0 ignored issues
show
Bug Best Practice introduced by
The property uniqueUId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
138
139
        $nsid = ltrim(rtrim($Student['nsid'],'-'),'-');
0 ignored issues
show
Unused Code introduced by
The assignment to $nsid is dead and can be removed.
Loading history...
140
        if(!$this->uniqueUId::isValidUniqueId('DBY-898-3J2')){
141
        }
142
    }
143
}
144