Passed
Push — master ( 696666...5ca42d )
by Phaniraj
02:36
created

Import::processImport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
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
It seems like file($path) can also be of type false; however, parameter $arr1 of array_map() does only seem to accept 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

50
        $data = array_map('str_getcsv', /** @scrutinizer ignore-type */ file($path));
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
Bug introduced by
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 ignore-call  annotation

66
        /** @scrutinizer ignore-call */ 
67
        $relations = $this->model->relationships();

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
$returnArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $returnArray = array(); before regardless.
Loading history...
89
        $returnArray['database_columns'] = (! empty($relationship_array)) ? array_merge(array_unique($db_cols), $relationship_array) : array_unique($db_cols);
90
91
        return response()->json($returnArray);
92
    }
93
94
    public function processImport(Closure $callback)
95
    {
96
        $temp_file_contents = Storage::disk('local')->get($this->saved_file);
97
        $temp_file_contents = json_decode($temp_file_contents, true);
98
99
        foreach (array_chunk($temp_file_contents, 1000) as $row) {
100
            dispatch(function () use ($callback,$row) {
101
                call_user_func($callback, $row);
102
            });
103
        }
104
105
        return response()->json(['message' => 'Importing..'], 200);
106
    }
107
}
108