| Conditions | 4 |
| Paths | 4 |
| Total Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | function process(Import $import, callable $callback) |
||
| 12 | { |
||
| 13 | |||
| 14 | // CSV header row show be excluded |
||
| 15 | $csv_header = true; |
||
| 16 | |||
| 17 | // Read csv |
||
| 18 | $file = fopen( storage_path( 'app/' . $import->file ), 'r' ); |
||
| 19 | |||
| 20 | // Processed csv rows |
||
| 21 | $processed_row = 1; |
||
| 22 | |||
| 23 | // Go over csv data line by line |
||
| 24 | while ( $csv_line = fgetcsv( $file ) ) { |
||
| 25 | |||
| 26 | if ( $csv_header ) { |
||
| 27 | |||
| 28 | // Skip csv header |
||
| 29 | $csv_header = false; |
||
| 30 | |||
| 31 | } else { |
||
| 32 | |||
| 33 | // Drop ignore columns |
||
| 34 | $data = array_combine( $import->model_map, $csv_line ); |
||
| 35 | unset( $data[''] ); |
||
| 36 | |||
| 37 | // Call user callback with data |
||
| 38 | if ( $callback( $data ) ) { |
||
| 39 | |||
| 40 | // If successful -> update imported rows |
||
| 41 | $import->row_imported = $import->row_imported + 1; |
||
| 42 | |||
| 43 | } |
||
| 44 | |||
| 45 | // Update porcessed rows |
||
| 46 | $import->row_processed = $processed_row; |
||
| 47 | $import->save(); |
||
| 48 | |||
| 49 | $processed_row++; |
||
| 50 | } |
||
| 51 | |||
| 52 | } |
||
| 53 | |||
| 54 | // Close csv file |
||
| 55 | fclose( $file ); |
||
| 56 | |||
| 57 | // Update import as done |
||
| 58 | $import->completed_at = Carbon::now(); |
||
| 59 | $import->save(); |
||
| 60 | |||
| 63 |
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: