Completed
Push — master ( 7d05ee...bd6010 )
by
unknown
9s
created

BantenprovSekolahSeederSekolah::readCSV()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 0
dl 0
loc 27
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
                'nama' => $data['nama'],
58
                'npsn' => $data['npsn'],
59
                'jenis_sekolah_id' => $data['jenis_sekolah_id'],
60
                'alamat' => $data['alamat'],
61
                'logo' => $data['logo'],
62
                'foto_gedung' => $data['foto_gedung'],
63
                'province_id' => $data['province_id'],
64
                'city_id' => $data['city_id'],
65
                'district_id' => $data['district_id'],
66
                'village_id' => $data['village_id'],
67
                'no_telp' => $data['no_telp'],
68
                'email' => $data['email'],
69
                'kode_zona' => $data['kode_zona'],
70
            	'user_id' => $data['user_id'],
71
72
            ]);
73
74
            if($this->textInfo){
75
                echo "============[DATA]============\n";
76
                $this->orangeText('nama : ').$this->greenText($data['nama']);
77
                echo"\n";
78
                $this->orangeText('npsn : ').$this->greenText($data['npsn']);
79
                echo"\n";
80
                $this->orangeText('jenis_sekolah_id : ').$this->greenText($data['jenis_sekolah_id']);
81
                echo"\n";
82
                $this->orangeText('alamat : ').$this->greenText($data['alamat']);
83
                echo"\n";
84
                $this->orangeText('logo : ').$this->greenText($data['logo']);
85
                echo"\n";
86
                $this->orangeText('foto_gedung : ').$this->greenText($data['foto_gedung']);
87
                echo"\n";
88
                $this->orangeText('province_id : ').$this->greenText($data['province_id']);
89
                echo"\n";
90
                $this->orangeText('city_id : ').$this->greenText($data['city_id']);
91
                echo"\n";
92
                $this->orangeText('district_id : ').$this->greenText($data['district_id']);
93
                echo"\n";
94
                $this->orangeText('village_id : ').$this->greenText($data['village_id']);
95
                echo"\n";
96
                $this->orangeText('no_telp : ').$this->greenText($data['no_telp']);
97
                echo"\n";
98
                $this->orangeText('email : ').$this->greenText($data['logo']);
99
                echo"\n";
100
                $this->orangeText('kode_zona : ').$this->greenText($data['kode_zona']);
101
                echo"\n";
102
                $this->orangeText('user_id : ').$this->greenText($data['user_id']);
103
                echo"\n";
104
                echo "============[DATA]============\n\n";
105
            }
106
107
        }
108
109
        $this->greenText('[ SEEDER DONE ]');
110
        echo"\n\n";
111
    }
112
113
    /* text color: orange */
114
    protected function orangeText($text)
115
    {
116
        printf($this->ORANGE.$text.$this->NC);
117
    }
118
119
    /* text color: green */
120
    protected function greenText($text)
121
    {
122
        printf($this->GRN.$text.$this->NC);
123
    }
124
125
    /* function read CSV file */
126
    protected function readCSV()
127
    {
128
        /* Silahkan di rubah sesuai struktur file csv */
129
        $file = fopen(database_path("seeds/".$this->fileName), "r");
130
        $all_data = array();
131
        $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...
132
        while(($data = fgetcsv($file, 1000, ",")) !== FALSE){
133
            $all_data[] = [ 'nama' => $data[0],
134
                            'npsn' => $data[1],
135
                            'jenis_sekolah_id' => $data[2],
136
                            'alamat' => $data[3],
137
                            'logo' => $data[4],
138
                            'foto_gedung' => $data[5],
139
                            'province_id' => $data[6],
140
                            'city_id' => $data[7],
141
                            'district_id' => $data[8],
142
                            'village_id' => $data[9],
143
                            'no_telp' => $data[10],
144
                            'email' => $data[11],
145
                            'kode_zona' => $data[12],
146
                            'user_id' => $data[13],
147
                        ];
148
        }
149
        fclose($file);
150
151
        return  $all_data;
152
    }
153
}
154