Test Setup Failed
Pull Request — master (#493)
by Mohamed
09:25
created

CleanExamData::lockData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
c 2
b 1
f 0
nc 2
nop 1
dl 0
loc 7
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
            ->where('nsid','<>','')
71
            ->whereNotNull('nsid')
72
            ->get()
73
            ->toArray();
74
        }
75
76
        $this->output->writeln('###########################################------Start cleanning exam records------###########################################');    
77
        if(count($students) > 0){
78
            $this->output->writeln('Total students to clean: '.  count($students));
79
            $students = array_chunk($students, $this->argument('chunk'));
80
            $function = array($this, 'process');
81
            processParallel($function,$students, $this->argument('max'),$type);
82
        }else{
83
            $this->output->writeln('nothing to process, all are cleaned');
84
        }   
85
        $this->output->writeln('###########################################------Finished cleaning exam records------###########################################');
86
    }
87
88
    public function process($students,$count,$type){
89
       if($type === 'duplicate'){
90
        array_walk($students,array($this,'cleanData'));
91
       }elseif($type === 'lock'){
92
        array_walk($students,array($this,'lockData'));
93
       }
94
    }
95
96
    public function lockData($Student){
97
        $Student = json_decode(json_encode($Student),true);
98
        $student = Security_user::where('openemis_no',(string)$Student['nsid'])->first();
99
        if(!empty($student)){
100
            Institution_student::where('student_id', $student->id)->update(['updated_from' => 'doe']);
101
            Security_user::where('id', $student->id)->update(['updated_from' => 'doe']);
102
            $this->output->writeln('Locked:'. (string)$Student['nsid'] .':' . $student['openemis_no']);
103
        }
104
    }
105
106
107
    public function cleanData($Student)
108
    {
109
        $exist = Examination_student::where('nsid','=',  (string)$Student->openemis_no)->count();
110
        if (!$exist) {
111
            Institution_student::where('student_id', $Student->id)->delete();
112
            Institution_class_student::where('student_id', $Student->id)->delete();
113
            Institution_student_admission::where('student_id', $Student->id)->delete();
114
            Security_user::where('id', $Student->id)->delete();
115
            $this->output->writeln('cleaned:'.  (string)$Student->openemis_no);
116
        }else{
117
            Institution_student::where('student_id', $Student->id)->update(['updated_from' => 'doe']);
118
            Security_user::where('id', $Student->id)->update(['updated_from' => 'doe']);
119
        }
120
    }
121
122
    public function cleanInvalidData($Student)
123
    {
124
        $Student = (array) $Student;
125
        $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...
126
        
127
        $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...
128
129
        $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...
130
        if(!$this->uniqueUId::isValidUniqueId('DBY-898-3J2')){
131
        }
132
    }
133
}
134