| Conditions | 4 |
| Paths | 4 |
| Total Lines | 45 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 10 | public function process(Import $import, callable $callback) |
||
| 11 | { |
||
| 12 | |||
| 13 | // CSV header row show be excluded |
||
| 14 | $csv_header = true; |
||
| 15 | |||
| 16 | // Read csv |
||
| 17 | $file = fopen(storage_path('app/'.$import->file), 'r'); |
||
| 18 | |||
| 19 | // Processed csv rows |
||
| 20 | $processed_row = 1; |
||
| 21 | |||
| 22 | // Go over csv data line by line |
||
| 23 | while ($csv_line = fgetcsv($file)) { |
||
| 24 | if ($csv_header) { |
||
| 25 | |||
| 26 | // Skip csv header |
||
| 27 | $csv_header = false; |
||
| 28 | } else { |
||
| 29 | |||
| 30 | // Drop ignore columns |
||
| 31 | $data = array_combine($import->model_map, $csv_line); |
||
| 32 | unset($data['']); |
||
| 33 | |||
| 34 | // Call user callback with data |
||
| 35 | if ($callback($data)) { |
||
| 36 | |||
| 37 | // If successful -> update imported rows |
||
| 38 | $import->row_imported = $import->row_imported + 1; |
||
| 39 | } |
||
| 40 | |||
| 41 | // Update porcessed rows |
||
| 42 | $import->row_processed = $processed_row; |
||
| 43 | $import->save(); |
||
| 44 | |||
| 45 | $processed_row++; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | // Close csv file |
||
| 50 | fclose($file); |
||
| 51 | |||
| 52 | // Update import as done |
||
| 53 | $import->completed_at = Carbon::now(); |
||
| 54 | $import->save(); |
||
| 55 | } |
||
| 57 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: