BantenprovSekolahSeederSekolah::readCSV()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 24
nc 2
nop 0
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Database\Seeder;
4
5
/**
6
 * Usage :
7
 * [1] $ composer dump-autoload -o
8
 * [2] $ php artisan db:seed --class=BantenprovSekolahSeederSekolah
9
 */
10
11
class BantenprovSekolahSeederSekolah extends Seeder
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    /* text color */
14
    protected $RED     ="\033[0;31m";
15
    protected $CYAN    ="\033[0;36m";
16
    protected $YELLOW  ="\033[1;33m";
17
    protected $ORANGE  ="\033[0;33m";
18
    protected $PUR     ="\033[0;35m";
19
    protected $GRN     ="\e[32m";
20
    protected $WHI     ="\e[37m";
21
    protected $NC      ="\033[0m";
22
23
    /* File name */
24
    /* location : /databse/seeds/file_name.csv */
25
    protected $fileName = "BantenprovSekolahSeederSekolah.csv";
26
27
    /* text info : default (true) */
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
    protected $textInfo = true;
29
30
    /* model class */
31
    protected $model;
32
33
    /* __construct */
34
    public function __construct(){
35
36
        $this->model = new Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
37
38
    }
39
40
    /**
41
     * Run the database seeds.
42
     *
43
     * @return void
44
     */
45
    public function run()
46
    {
47
        $this->insertData();
48
    }
49
50
    /* function insert data */
51
    protected function insertData()
52
    {
53
        /* silahkan di rubah sesuai kebutuhan */
54
        foreach($this->readCSV() as $data){
55
56
            $this->model->create([
57
                'id'                => $data['id'],
58
            	'user_id'           => $data['user_id'],
59
                'nama'              => $data['nama'],
60
                'npsn'              => $data['npsn'],
61
                'jenis_sekolah_id'  => $data['jenis_sekolah_id'],
62
                'alamat'            => $data['alamat'],
63
                'logo'              => $data['logo'],
64
                'foto_gedung'       => $data['foto_gedung'],
65
                'province_id'       => $data['province_id'],
66
                'city_id'           => $data['city_id'],
67
                'district_id'       => $data['district_id'],
68
                'village_id'        => $data['village_id'],
69
                'no_telp'           => $data['no_telp'],
70
                'email'             => $data['email'],
71
                'kode_zona'         => $data['kode_zona'],
72
                'uuid'              => $data['uuid'],
73
74
            ]);
75
76
            if($this->textInfo){
77
                echo "============[DATA]============\n";
78
                $this->orangeText('nama : ').$this->greenText($data['nama']);
79
                echo"\n";
80
                $this->orangeText('npsn : ').$this->greenText($data['npsn']);
81
                echo"\n";
82
                $this->orangeText('jenis_sekolah_id : ').$this->greenText($data['jenis_sekolah_id']);
83
                echo"\n";
84
                $this->orangeText('alamat : ').$this->greenText($data['alamat']);
85
                echo"\n";
86
                $this->orangeText('logo : ').$this->greenText($data['logo']);
87
                echo"\n";
88
                $this->orangeText('foto_gedung : ').$this->greenText($data['foto_gedung']);
89
                echo"\n";
90
                $this->orangeText('province_id : ').$this->greenText($data['province_id']);
91
                echo"\n";
92
                $this->orangeText('city_id : ').$this->greenText($data['city_id']);
93
                echo"\n";
94
                $this->orangeText('district_id : ').$this->greenText($data['district_id']);
95
                echo"\n";
96
                $this->orangeText('village_id : ').$this->greenText($data['village_id']);
97
                echo"\n";
98
                $this->orangeText('no_telp : ').$this->greenText($data['no_telp']);
99
                echo"\n";
100
                $this->orangeText('email : ').$this->greenText($data['logo']);
101
                echo"\n";
102
                $this->orangeText('kode_zona : ').$this->greenText($data['kode_zona']);
103
                echo"\n";
104
                $this->orangeText('user_id : ').$this->greenText($data['user_id']);
105
                echo"\n";
106
                $this->orangeText('id : ').$this->greenText($data['id']);
107
                echo"\n";
108
                $this->orangeText('uuid : ').$this->greenText($data['uuid']);
109
                echo"\n";
110
                echo "============[DATA]============\n\n";
111
            }
112
113
        }
114
115
        $this->greenText('[ SEEDER DONE ]');
116
        echo"\n\n";
117
    }
118
119
    /* text color: orange */
120
    protected function orangeText($text)
121
    {
122
        printf($this->ORANGE.$text.$this->NC);
123
    }
124
125
    /* text color: green */
126
    protected function greenText($text)
127
    {
128
        printf($this->GRN.$text.$this->NC);
129
    }
130
131
    /* function read CSV file */
132
    protected function readCSV()
133
    {
134
        /* Silahkan di rubah sesuai struktur file csv */
135
        $file = fopen(database_path("seeds/".$this->fileName), "r");
136
        $all_data = array();
137
        $row = 1;
0 ignored issues
show
Unused Code introduced by
$row is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
138
        while(($data = fgetcsv($file, 1000, ",")) !== FALSE){
139
            $all_data[] = [ 
140
                            'id'                => $data[0],
141
                            'user_id'           => $data[1],
142
                            'nama'              => $data[2],
143
                            'npsn'              => $data[3],
144
                            'jenis_sekolah_id'  => $data[4],
145
                            'alamat'            => $data[5],
146
                            'logo'              => $data[6],
147
                            'foto_gedung'       => $data[7],
148
                            'province_id'       => $data[8],
149
                            'city_id'           => $data[9],
150
                            'district_id'       => $data[10],
151
                            'village_id'        => $data[11],
152
                            'no_telp'           => $data[12],
153
                            'email'             => $data[13],
154
                            'kode_zona'         => $data[14],
155
                            'uuid'              => $data[15],
156
                        ];
157
        }
158
        fclose($file);
159
160
        return  $all_data;
161
    }
162
}
163