Completed
Push — master ( c2e2a2...a0c432 )
by Phaniraj
02:54
created

Import::getDbCols()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 17
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace LWS\Import;
4
5
use Closure;
6
use LWS\Import\Factory;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Routing\Controller;
10
11
class Import extends Controller
12
{
13
    private $model;
14
    protected $saved_file='file.json';
15
    protected $serialized;
16
17
    public function getModelObject($source)
18
    {
19
        $factory = new Factory();
20
        $model = $factory->make($source);
21
22
        if(!is_null($model))
23
            return $model;
24
    }
25
26
    public function getDbCols($source)
27
    {
28
        
29
        
30
        $factory = new Factory();
31
        $this->model = $factory->make($source);
32
33
        
34
        if(is_null($this->model)){
35
        
36
            return;
37
        }
38
39
        if(empty($this->model->getHidden())) {
40
            return array_unique($this->model->getFillable());
41
        } else {
42
            return(array_unique(array_merge($this->model->getFillable(),$this->model->getHidden())));
43
        }
44
    }
45
46
    
47
48
    public function parseImport($source,Request $request)
49
    {
50
51
        
52
53
        $request->validate([
54
            'csv_file' => "required|file|mimes:csv,txt"
55
        ]);
56
        
57
        $path = $request->file('csv_file')->getRealPath();
58
59
        $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

59
        $data = array_map('str_getcsv',/** @scrutinizer ignore-type */ file($path));
Loading history...
60
        
61
        if($request->has('header')) {
62
            array_shift($data);
63
        }
64
65
        array_shift($data);
66
        
67
        Storage::disk('local')->put($this->saved_file,json_encode($data));
68
        
69
        $db_cols = $this->getDbCols($source);
70
        
71
        if(is_null($db_cols)) {
72
            
73
            return response()->json(["message"=>"No Such Model Found in App"], 500);
74
        }
75
76
        $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

76
        /** @scrutinizer ignore-call */ 
77
        $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...
77
78
        $relationship_array = array();
79
80
        foreach($relations as $key => $value) {
81
            $model = new $value['model'];
82
            
83
            if(!empty($model->getFillable())) {
84
85
                if(empty($model->getHidden())) {
86
87
                    $relationship_array[] = implode(" ",array_values(array_unique(array_map(function ($v) use ($key) { return $key.".".$v;},$model->getFillable()))));
88
    
89
                } else {
90
                    $relationship_array[] = implode(" ",array_values(array_unique(array_merge(array_map(function ($v) use ($key) { return $key.".".$v;},$model->getFillable()),array_map(function ($v) use ($key) { return $key.".".$v;},$model->getHidden())))));
91
                }
92
93
            } //not a fillable model
94
        }
95
 
96
97
        $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...
98
        $returnArray['database_columns'] = (!empty($relationship_array)) ? array_merge(array_unique($db_cols),$relationship_array) : array_unique($db_cols) ;
99
100
        return response()->json($returnArray);
101
        
102
    }
103
104
    
105
106
    public function process(Closure $callback)
107
    {
108
        
109
        $temp_file_contents = Storage::disk('local')->get($this->saved_file);
110
        $temp_file_contents = json_decode($temp_file_contents,true);
111
112
        foreach(array_chunk($temp_file_contents,1000) as $row) {
113
 
114
            dispatch(function() use ($callback,$row){
115
                call_user_func($callback,$row);
116
            });
117
        }
118
119
        return response()->json(['message' => "Importing.."], 200);
120
    }
121
}