Test Setup Failed
Push — master ( 72885b...568721 )
by Mohamed
02:18 queued 11s
created

ExaminationStudentsImport::__construct()   A

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
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Imports;
4
5
use Illuminate\Support\Collection;
6
use App\Models\Examination_student;
7
use Maatwebsite\Excel\Concerns\ToModel;
8
use Maatwebsite\Excel\Validators\Failure;
9
use Maatwebsite\Excel\Concerns\Importable;
10
use Maatwebsite\Excel\Concerns\SkipsErrors;
11
use Maatwebsite\Excel\Concerns\WithMapping;
12
use Maatwebsite\Excel\Concerns\SkipsOnError;
13
use Maatwebsite\Excel\Concerns\WithStartRow;
14
use Maatwebsite\Excel\Concerns\SkipsFailures;
15
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
16
use Maatwebsite\Excel\Concerns\WithHeadingRow;
17
use Maatwebsite\Excel\Concerns\WithValidation;
18
use Maatwebsite\Excel\Concerns\WithBatchInserts;
19
use Maatwebsite\Excel\Concerns\WithChunkReading;
20
21
class ExaminationStudentsImport implements ToModel, WithStartRow, WithHeadingRow, WithChunkReading, WithBatchInserts , WithValidation ,WithMapping, SkipsOnFailure, SkipsOnError
22
{
23
    use Importable , SkipsFailures, SkipsErrors;
0 ignored issues
show
introduced by
The trait Maatwebsite\Excel\Concerns\Importable requires some properties which are not provided by App\Imports\ExaminationStudentsImport: $disk, $readerType, $filePath
Loading history...
24
25
    public function __construct($year,$grade)
26
    {
27
        $this->year = $year;
0 ignored issues
show
Bug Best Practice introduced by
The property year does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
        $this->grade = $grade;
0 ignored issues
show
Bug Best Practice introduced by
The property grade does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
    }
30
    /**
31
     * @return int
32
     */
33
    public function startRow(): int
34
    {
35
        return 2;
36
    }
37
38
    /**
39
     * @return int
40
     */
41
    public function headingRow(): int
42
    {
43
        return 1;
44
    }
45
46
    public function chunkSize(): int
47
    {
48
        return 10000;
49
    }
50
51
    public function batchSize(): int
52
    {
53
        return 10000;
54
    }
55
56
    public function map($row): array
57
    {
58
        $row['b_date'] = $this->transformDateTime($row['b_date']);
59
        return $row;
60
    }
61
62
63
    private function transformDateTime(string $value, string $format = 'm/d/Y')
0 ignored issues
show
Unused Code introduced by
The parameter $format 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

63
    private function transformDateTime(string $value, /** @scrutinizer ignore-unused */ string $format = 'm/d/Y')

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...
64
    {
65
        try{
66
            $date = date_create_from_format('m/d/Y', $value);
67
            if(gettype($date)=='boolean'){
68
                $date = date_create_from_format('Y-m-d', $value);
69
            }
70
            $date = date_format($date, 'Y-m-d');
71
            return  $date;
72
        }catch(\Exception $e){
73
            $error = \Illuminate\Validation\ValidationException::withMessages([]);
74
            $failure = new Failure(2, 'remark', [0 => 'The given date is wrong']);
75
            $failures = [0 => $failure];
76
            new \Maatwebsite\Excel\Validators\ValidationException($error, $failures);
77
        }
78
    }
79
80
81
    /**
82
     * @param Collection $collection
83
     */
84
    public function model(array $row)
85
    {
86
       
87
        $insertData = array(
88
            'st_no' => $row['st_no'],
89
            'stu_no' => $row['stu_no'],
90
            "f_name" => $row['f_name'],
91
            "medium" => $row['medium'],
92
            "gender" => $row['gender'],
93
            "b_date" =>  $row['b_date'],
94
            "a_income" => $row['a_income'] ? $row['a_income'] : 0 ,
95
            "grade" => $this->grade,
96
            'year' => $this->year,
97
            "schoolid" => $row['schoolid'],
98
            "spl_need" => $row['spl_need'],
99
            "pvt_address" => $row['pvt_address'],
100
            "disability_type" => $row['disability_type'],
101
            "disability" => $row['disability'],
102
            "sp_center" => $row['sp_center']
103
        );
104
        Examination_student::insertData($insertData);
105
    }
106
107
    public function rules(): array
108
    {
109
        return [
110
            'b_date' => 'date|required',
111
            'schoolid' => 'exists:institutions,code'
112
        ];
113
    }
114
}
115