ladybirdweb /
laravel-importer
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace LWS\Import; |
||||||
| 4 | |||||||
| 5 | use Closure; |
||||||
| 6 | use Illuminate\Http\Request; |
||||||
| 7 | use Illuminate\Routing\Controller; |
||||||
| 8 | use Illuminate\Support\Facades\Storage; |
||||||
| 9 | |||||||
| 10 | class Import extends Controller |
||||||
| 11 | { |
||||||
| 12 | private $model; |
||||||
| 13 | protected $saved_file = 'file.json'; |
||||||
| 14 | protected $serialized; |
||||||
| 15 | |||||||
| 16 | public function getModelObject($source) |
||||||
| 17 | { |
||||||
| 18 | $factory = new Factory(); |
||||||
| 19 | $model = $factory->make($source); |
||||||
| 20 | |||||||
| 21 | if (! is_null($model)) { |
||||||
| 22 | return $model; |
||||||
| 23 | } |
||||||
| 24 | } |
||||||
| 25 | |||||||
| 26 | public function getDbCols($source) |
||||||
| 27 | { |
||||||
| 28 | $factory = new Factory(); |
||||||
| 29 | $this->model = $factory->make($source); |
||||||
| 30 | |||||||
| 31 | if (is_null($this->model)) { |
||||||
| 32 | return; |
||||||
| 33 | } |
||||||
| 34 | |||||||
| 35 | if (empty($this->model->getHidden())) { |
||||||
| 36 | return array_unique($this->model->getFillable()); |
||||||
| 37 | } else { |
||||||
| 38 | return array_unique(array_merge($this->model->getFillable(), $this->model->getHidden())); |
||||||
| 39 | } |
||||||
| 40 | } |
||||||
| 41 | |||||||
| 42 | public function parseImport($source, Request $request) |
||||||
| 43 | { |
||||||
| 44 | $request->validate([ |
||||||
| 45 | 'csv_file' => 'required|file|mimes:csv,txt', |
||||||
| 46 | ]); |
||||||
| 47 | |||||||
| 48 | $path = $request->file('csv_file')->getRealPath(); |
||||||
| 49 | |||||||
| 50 | $data = array_map('str_getcsv', file($path)); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 51 | |||||||
| 52 | if ($request->has('header')) { |
||||||
| 53 | array_shift($data); |
||||||
| 54 | } |
||||||
| 55 | |||||||
| 56 | array_shift($data); |
||||||
| 57 | |||||||
| 58 | Storage::disk('local')->put($this->saved_file, json_encode($data)); |
||||||
| 59 | |||||||
| 60 | $db_cols = $this->getDbCols($source); |
||||||
| 61 | |||||||
| 62 | if (is_null($db_cols)) { |
||||||
| 63 | return response()->json(['message'=>'No Such Model Found in App'], 500); |
||||||
| 64 | } |
||||||
| 65 | |||||||
| 66 | $relations = $this->model->relationships(); |
||||||
|
0 ignored issues
–
show
The method
relationships() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 67 | |||||||
| 68 | $relationship_array = []; |
||||||
| 69 | |||||||
| 70 | foreach ($relations as $key => $value) { |
||||||
| 71 | $model = new $value['model']; |
||||||
| 72 | |||||||
| 73 | if (! empty($model->getFillable())) { |
||||||
| 74 | if (empty($model->getHidden())) { |
||||||
| 75 | $relationship_array[] = implode(' ', array_values(array_unique(array_map(function ($v) use ($key) { |
||||||
| 76 | return $key.'.'.$v; |
||||||
| 77 | }, $model->getFillable())))); |
||||||
| 78 | } else { |
||||||
| 79 | $relationship_array[] = implode(' ', array_values(array_unique(array_merge(array_map(function ($v) use ($key) { |
||||||
| 80 | return $key.'.'.$v; |
||||||
| 81 | }, $model->getFillable()), array_map(function ($v) use ($key) { |
||||||
| 82 | return $key.'.'.$v; |
||||||
| 83 | }, $model->getHidden()))))); |
||||||
| 84 | } |
||||||
| 85 | } //not a fillable model |
||||||
| 86 | } |
||||||
| 87 | |||||||
| 88 | $returnArray['csv_sample_row'] = ($data[0]); |
||||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||||
| 89 | $returnArray['database_columns'] = (! empty($relationship_array)) ? array_merge(array_unique($db_cols), $relationship_array) : array_unique($db_cols); |
||||||
| 90 | $returnArray['relations'] = $relations; |
||||||
| 91 | |||||||
| 92 | return response()->json($returnArray); |
||||||
| 93 | } |
||||||
| 94 | |||||||
| 95 | public function processImport(Closure $callback) |
||||||
| 96 | { |
||||||
| 97 | $temp_file_contents = Storage::disk('local')->get($this->saved_file); |
||||||
| 98 | $temp_file_contents = json_decode($temp_file_contents, true); |
||||||
| 99 | |||||||
| 100 | foreach (array_chunk($temp_file_contents, 1000) as $row) { |
||||||
| 101 | dispatch(function () use ($callback,$row) { |
||||||
| 102 | call_user_func($callback, $row); |
||||||
| 103 | }); |
||||||
| 104 | } |
||||||
| 105 | |||||||
| 106 | return response()->json(['message' => 'Importing..'], 200); |
||||||
| 107 | } |
||||||
| 108 | } |
||||||
| 109 |