Test Failed
Push — master ( 5904eb...0e6af8 )
by Phaniraj
05:25
created

ImportHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 45 4
1
<?php
2
3
namespace LWS\ImportExport;
4
5
use Carbon\Carbon;
6
use LWS\ImportExport\Models\Import;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, LWS\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...
7
8
class ImportHandler
9
{
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)) {
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

23
        while ($csv_line = fgetcsv(/** @scrutinizer ignore-type */ $file)) {
Loading history...
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);
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

31
                $data = array_combine(/** @scrutinizer ignore-type */ $import->model_map, $csv_line);
Loading history...
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);
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

50
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
51
52
        // Update import as done
53
        $import->completed_at = Carbon::now();
54
        $import->save();
55
    }
56
}
57