Test Failed
Branch develop (e68766)
by Abhishek Kumar
03:33
created

Import::getImport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ladybirdweb\ImportExport;
4
5
use Ladybirdweb\ImportExport\Models\Import as ModelImport;
6
7
class Import
8
{
9
    /**
10
     * Store a newly created import in storage.
11
     *
12
     * @param  $path
13
     * @param  $columns
14
     * @return instance Import
0 ignored issues
show
Bug introduced by
The type Ladybirdweb\ImportExport\instance was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
     */
16
    public function createImport($path, $columns)
17
    {
18
        // Store file path and model class to db
19
        $import = ModelImport::create([
20
            'file' => $path,
21
            'file_rows' => count( file( storage_path( 'app/' . $path ) ) ) - 1,
0 ignored issues
show
Bug introduced by
It seems like file(storage_path('app/' . $path)) can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, 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

21
            'file_rows' => count( /** @scrutinizer ignore-type */ file( storage_path( 'app/' . $path ) ) ) - 1,
Loading history...
22
            'db_cols' => $columns
23
        ]);
24
25
        return $import;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $import returns the type Ladybirdweb\ImportExport\Models\Import which is incompatible with the documented return type Ladybirdweb\ImportExport\instance.
Loading history...
26
    }
27
28
    /**
29
     * Get import instance.
30
     *
31
     * @param  $id
32
     * @return instance Import
33
     */
34
    public function getImport($id)
35
    {
36
        return ModelImport::findOrFail( $id );
0 ignored issues
show
Bug Best Practice introduced by
The expression return Ladybirdweb\Impor...Import::findOrFail($id) returns the type Ladybirdweb\ImportExport\Models\Import which is incompatible with the documented return type Ladybirdweb\ImportExport\instance.
Loading history...
37
    }
38
39
    public function getImportFileData($id, $rows = 5)
40
    {
41
        // Get import instance
42
        $import = $this->getImport( $id );
43
        
44
        // Read 5 rows from csv
45
        $read_line = 1;
46
47
        $file = fopen( storage_path( 'app/' . $import->file ), 'r' );
48
49
        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

49
        while ( $csv_line = fgetcsv( /** @scrutinizer ignore-type */ $file ) ) {
Loading history...
50
            $csv_data[] = $csv_line;
51
52
            if ( $read_line > $rows ) break;
53
54
            $read_line++;
55
        }
56
        
57
        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

57
        fclose( /** @scrutinizer ignore-type */ $file );
Loading history...
58
59
        return $csv_data;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $csv_data does not seem to be defined for all execution paths leading up to this point.
Loading history...
60
    }
61
62
    /**
63
     * Update column map in DB.
64
     *
65
     * @param  mixed  $column
66
     * @param  int  $id
67
     * @return instance Import
68
     */
69
    public function setDataMap($column, $id)
70
    {
71
        // Get import instance
72
        $import = $this->getImport($id);
73
74
        // Store column map in DB
75
        $import->model_map = $column;
76
        $import->save();
77
78
        // Return import instance
79
        return $import;
80
    }
81
82
    /**
83
     * Dispatch import job
84
     *
85
     * @param  $job Job class to dispatch
86
     * @param  $import Instance of import
0 ignored issues
show
Bug introduced by
The type Ladybirdweb\ImportExport\Instance was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
     * @return void
88
     */
89
    public function dispatchImportJob($job, ModelImport $import)
90
    {
91
        // Dispatch import corn job
92
        $job::dispatch($import)->onQueue('importing');
93
    }
94
95
    /**
96
     * Return import progress
97
     *
98
     * @param  int  $id
99
     * @return \Illuminate\Http\Response json
100
     */
101
    public function returnImportProgress($id)
102
    {
103
        // GEt import instance
104
        $import = $this->getImport($id);
105
106
        $data['status'] = 200;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
107
        $data['progress'] = round(($import->row_processed / $import->file_rows) * 100);
108
109
        // If progress completed return successful imported rows count
110
        if ( $data['progress'] == 100 ) {
111
            $data['imported'] = $import->row_imported;
112
        }
113
114
        return response()->json( $data );
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($data) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
115
    }
116
117
118
    /**
119
     * Remove the specified resource from storage.
120
     *
121
     * @param  int  $id
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function removeImport($id)
125
    {
126
        // Get import instance
127
        $import = $this->getImport( $id );
128
129
        // Remove a import from db
130
        return $import->delete();
131
    }
132
}
133