Test Failed
Branch develop (5056e3)
by Abhishek Kumar
05:17
created

ImportHandler::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 49
rs 9.1127
c 0
b 0
f 0
1
<?php
2
3
namespace Ladybirdweb\ImportExport;
4
5
use Ladybirdweb\ImportExport\Models\Import;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ladybirdweb\ImportExport\Import. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Carbon\Carbon;
7
8
class ImportHandler
9
{
10
	
11
	function process(Import $import, callable $callback)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ) ) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fgetcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
        while ( $csv_line = fgetcsv( /** @scrutinizer ignore-type */ $file ) ) {
Loading history...
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 );
0 ignored issues
show
Bug introduced by
$import->model_map of type string is incompatible with the type array expected by parameter $keys of array_combine(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
                $data = array_combine( /** @scrutinizer ignore-type */ $import->model_map, $csv_line );
Loading history...
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 );
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        fclose( /** @scrutinizer ignore-type */ $file );
Loading history...
56
57
        // Update import as done
58
        $import->completed_at = Carbon::now();
59
        $import->save();
60
61
	}
62
}
63