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 |
|
|
|
|
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, |
|
|
|
|
22
|
|
|
'db_cols' => $columns, |
23
|
|
|
]); |
24
|
|
|
|
25
|
|
|
return $import; |
|
|
|
|
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)) { |
|
|
|
|
50
|
|
|
$csv_data[] = $csv_line; |
51
|
|
|
|
52
|
|
|
if ($read_line > $rows) { |
53
|
|
|
break; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$read_line++; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
fclose($file); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return $csv_data; |
|
|
|
|
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 |
|
|
|
|
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; |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths