Test Setup Failed
Push — master ( b1a532...1cf3ce )
by
unknown
18:54
created

RunAddStudentsToInstitutions   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
c 1
b 0
f 0
dl 0
loc 104
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addStudents() 0 2 1
A handle() 0 21 3
A addStudent() 0 43 4
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use App\Models\Institution_class_student;
7
use App\Models\Institution_class_subject;
8
use App\Models\Institution_student_admission;
9
use App\Models\Institution_student;
10
use App\Models\Institution;
11
use Illuminate\Support\Facades\Log;
12
13
class RunAddStudentsToInstitutions extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'admission:students {institution}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Add approved students data to indtitution_student table';
28
29
    /**
30
     * Create a new command instance.
31
     *
32
     * @return void
33
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return mixed
43
     */
44
    public function handle()
45
    {
46
        // dd('test');
47
        $institution = Institution::where([
48
            'id' => $this->argument('institution')
49
        ])->first();
50
51
        if(!is_null($institution)){
52
53
            // dd($institution);
54
            try {
55
                $this->info('adding missing students to the institution ' . $institution->name);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on App\Models\Institution. 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...
56
                $approvedstudent = Institution_student_admission::select('institution_student_admission.*')->join('institutions', 'institution_id', '=', 'institutions.id')->leftJoin('institution_students', 'student_id', '=', 'institution_students.student_id')->where([
57
                    // 'status_id' => 121,122,123,124, // 
58
                    'institution_id' => $institution->id
59
                ])->get()->toArray();
60
                dd($approvedstudent);
61
                $approvedstudent = array_chunk($approvedstudent, 50);
62
                array_walk($approvedstudent, array($this, 'addStudents'));
63
            }catch (\Exception $e) {
64
                Log::error($e);
65
            }
66
        
67
        }
68
    }
69
70
    protected function addStudents($approvedstudent){
71
        array_walk($approvedstudent,array($this,'addStudent'));
72
    }
73
74
    protected function addStudent($approvedstudent){
75
        $output = new \Symfony\Component\Console\Output\ConsoleOutput();
76
        Log::info($approvedstudent);
77
78
        sleep(1);
79
        if(!(Institution_student::isDuplicated($approvedstudent) > 0)){
80
            $this->count += 1;
0 ignored issues
show
Bug Best Practice introduced by
The property count does not exist on App\Console\Commands\RunAddStudentsToInstitutions. Did you maybe forget to declare it?
Loading history...
81
            $this->student = $approvedstudent ;
0 ignored issues
show
Bug Best Practice introduced by
The property student does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
82
            try{
83
               Institution_student::insert([
84
                   'student_status_id' => 1,
85
                   'student_id' => $approvedstudent['student_id'],
86
                   'education_grade_id' => $approvedstudent['education_grade_id'],
87
                   'academic_period_id' => $approvedstudent['academic_period_id'],
88
                   'start_date' => $approvedstudent['start_date'],
89
                   'start_year' => \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $approvedstudent['start_date'])->year , // $approvedstudent['start_date']->format('Y'),
90
                   'end_date' => $approvedstudent['end_date'],
91
                   'end_year' =>  \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $approvedstudent['end_date'])->year , //$approvedstudent['end_date']->format('Y'),
92
                   'institution_id' => $approvedstudent['institution_id'],
93
                   'admission_id' => $approvedstudent['admission_id'],
94
                   'created_user_id' => $approvedstudent['created_user_id'],
95
               ]);
96
97
               if(!is_null($approvedstudent['institution_class_id'])){
98
                   Institution_class_student::insert([
99
                       'student_id' => $approvedstudent['student_id'],
100
                       'institution_class_id' => $approvedstudent['institution_class_id'],
101
                       'education_grade_id' =>  $approvedstudent['education_grade_id'],
102
                       'academic_period_id' => $approvedstudent['academic_period_id'],
103
                       'institution_id' =>$approvedstudent['institution_id'],
104
                       'student_status_id' => 1,
105
                       'created_user_id' => $approvedstudent['created_user_id'],
106
                   ]);
107
               }
108
                $output->writeln('
109
        ####################################################
110
           Total number of students updated : '.$this->count.'
111
        #                                                  #
112
        ####################################################' );
113
//        $output->writeln();
114
           }catch (\Exception $e){
115
//               echo $e->getMessage();
116
               $output->writeln( $e->getMessage());
117
           }
118
        }
119
120
    }
121
}
122