Import   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 14
eloc 52
c 4
b 3
f 0
dl 0
loc 97
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelObject() 0 7 2
A getDbCols() 0 13 3
A processImport() 0 12 2
B parseImport() 0 51 7
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
        $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