Test Setup Failed
Pull Request — master (#478)
by Mohamed
13:16
created

CleanExamData::cleanInvalidData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 2
b 0
f 1
nc 2
nop 1
dl 0
loc 9
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('institution_students as is')
58
            ->join('security_users as su', 'su.id', 'is.student_id')
59
            ->where('is.updated_from', 'doe')
60
            ->orWhere('su.updated_from', 'doe')
61
            ->groupBy('is.student_id')
62
            ->orderBy('is.student_id')
63
            ->get()
64
            ->toArray();
65
            
66
        }elseif('all'){
67
            $students = DB::table('examination_students')
68
            ->where('nsid','<>','')
69
            ->whereNotNull('nsid')
70
            ->get()
71
            ->toArray();
72
        }
73
74
        $this->output->writeln('###########################################------Start cleanning exam records------###########################################');    
75
        if(count($students) > 0){
76
            $this->output->writeln('Total students to clean: '.  count($students));
77
            $students = array_chunk($students, $this->argument('chunk'));
78
            $function = array($this, 'process');
79
            processParallel($function,$students, $this->argument('max'));
80
        }else{
81
            $this->output->writeln('nothing to process, all are cleaned');
82
        }   
83
        $this->output->writeln('###########################################------Finished cleaning exam records------###########################################');
84
    }
85
86
    public function process($students){
87
        $type = $this->argument('type');
88
       if($type == 'duplication'){
89
        array_walk($students,array($this,'cleanData'));
90
       }elseif($type == 'invalid' || 'all'){
91
        array_walk($students,array($this,'cleanInvalidData'));
92
       }
93
    }
94
95
96
    public function cleanData($Student)
97
    {
98
        $exist = Examination_student::where('nsid','=',  $Student->openemis_no)->count();
99
100
        if (!$exist) {
101
            Institution_student::where('student_id', $Student->student_id)->delete();
102
            Institution_class_student::where('student_id', $Student->student_id)->delete();
103
            Institution_student_admission::where('student_id', $Student->student_id)->delete();
104
            Security_user::where('id', $Student->student_id)->delete();
105
        }
106
    }
107
108
    public function cleanInvalidData($Student)
109
    {
110
        $Student = (array) $Student;
111
        $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...
112
        
113
        $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...
114
115
        $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...
116
        if(!$this->uniqueUId::isValidUniqueId('DBY-898-3J2')){
117
        }
118
    }
119
}
120