Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Importable | |
0.00% |
0 / 17 |
|
0.00% |
0 / 2 |
56 | |
0.00% |
0 / 1 |
| import | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| getchunkdata | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Imports; |
| 4 | |
| 5 | trait Importable |
| 6 | { |
| 7 | |
| 8 | public $csv_data_in_array; |
| 9 | |
| 10 | /** |
| 11 | * @param string|null $filename |
| 12 | * @return string |
| 13 | */ |
| 14 | public function import($file) |
| 15 | { |
| 16 | $handle = fopen($file->path(), 'r'); |
| 17 | |
| 18 | fgetcsv($handle); |
| 19 | |
| 20 | $chunksize = 20; |
| 21 | while (!feof($handle)) { |
| 22 | $chunk_data = []; |
| 23 | for ($i = 0; $i < $chunksize; $i++) { |
| 24 | $data = fgetcsv($handle); |
| 25 | if ($data === false) { |
| 26 | break; |
| 27 | } |
| 28 | $chunk_data[] = $data; |
| 29 | } |
| 30 | |
| 31 | $this->getchunkdata($chunk_data); |
| 32 | } |
| 33 | fclose($handle); |
| 34 | |
| 35 | return $this->csv_data_in_array; |
| 36 | } |
| 37 | |
| 38 | public function getchunkdata($chunk_data) |
| 39 | { |
| 40 | foreach($chunk_data as $row){ |
| 41 | $this->csv_data_in_array[] = $row; |
| 42 | if ($this->shouldSaveToDB()) { |
| 43 | $this->saveToDB($row); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |