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

Import::removeImport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace LWS\ImportExport;
4
5
use LWS\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 LWS\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 LWS\ImportExport\Models\Import which is incompatible with the documented return type LWS\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);
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) {
53
                break;
54
            }
55
56
            $read_line++;
57
        }
58
59
        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

59
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
60
61
        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...
62
    }
63
64
    /**
65
     * Update column map in DB.
66
     *
67
     * @param  mixed  $column
68
     * @param  int  $id
69
     * @return instance Import
70
     */
71
    public function setDataMap($column, $id)
72
    {
73
        // Get import instance
74
        $import = $this->getImport($id);
75
76
        // Store column map in DB
77
        $import->model_map = $column;
78
        $import->save();
79
80
        // Return import instance
81
        return $import;
82
    }
83
84
    /**
85
     * Dispatch import job.
86
     *
87
     * @param  $job Job class to dispatch
88
     * @param  $import Instance of import
0 ignored issues
show
Bug introduced by
The type LWS\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...
89
     * @return void
90
     */
91
    public function dispatchImportJob($job, ModelImport $import)
92
    {
93
        // Dispatch import corn job
94
        $job::dispatch($import)->onQueue('importing');
95
    }
96
97
    /**
98
     * Return import progress.
99
     *
100
     * @param  int  $id
101
     * @return \Illuminate\Http\Response json
102
     */
103
    public function returnImportProgress($id)
104
    {
105
        // GEt import instance
106
        $import = $this->getImport($id);
107
108
        $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...
109
        $data['progress'] = round(($import->row_processed / $import->file_rows) * 100);
110
111
        // If progress completed return successful imported rows count
112
        if ($data['progress'] == 100) {
113
            $data['imported'] = $import->row_imported;
114
        }
115
116
        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...
117
    }
118
119
    /**
120
     * Remove the specified resource from storage.
121
     *
122
     * @param  int  $id
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function removeImport($id)
126
    {
127
        // Get import instance
128
        $import = $this->getImport($id);
129
130
        // Remove a import from db
131
        return $import->delete();
132
    }
133
}
134