Passed
Push — master ( d63a45...8016ac )
by Iman
09:10
created

IndexImport::InsertToDB()   B

Complexity

Conditions 8
Paths 28

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 28
nop 3
dl 0
loc 36
rs 8.0995
c 0
b 0
f 0
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\Controllers\Helpers;
4
5
use Crocodicstudio\Crudbooster\Helpers\CRUDBooster;
6
use Crocodicstudio\Crudbooster\Helpers\DbInspector;
7
use Crocodicstudio\Crudbooster\Modules\ModuleGenerator\ModulesRepo;
8
use Illuminate\Support\Facades\Cache;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Facades\Storage;
11
use Illuminate\Support\Facades\Validator;
12
use Maatwebsite\Excel\Facades\Excel;
13
14
class IndexImport
15
{
16
    /**
17
     * @return mixed
18
     */
19
    public function doneImport()
20
    {
21
        $data = [];
22
        $data['page_title'] = trans('crudbooster.import_page_title', ['module' => \CRUDBooster::getCurrentModule()->name]);
0 ignored issues
show
Bug introduced by
The type CRUDBooster was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
        session()->put('select_column', request('select_column'));
24
25
        return view('crudbooster::import', $data);
26
    }
27
28
    /**
29
     * @param $file_md5
30
     * @return mixed
31
     * @throws \Exception
32
     */
33
    public function handleImportProgress($file_md5)
34
    {
35
        $total = session('total_data_import') * 100;
36
        $prog = intval(cache('success_'.$file_md5)) / $total;
37
        $prog = round($prog, 2);
38
39
        if ($prog >= 100) {
40
            cache()->forget('success_'.$file_md5);
41
        }
42
43
        return response()->json(['progress' => $prog, 'last_error' => cache('error_'.$file_md5)]);
0 ignored issues
show
Bug introduced by
The method json() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        return response()->/** @scrutinizer ignore-call */ json(['progress' => $prog, 'last_error' => cache('error_'.$file_md5)]);
Loading history...
Bug introduced by
Are you sure the usage of response()->json(array('...'error_' . $file_md5))) targeting ImanGhafoori\Terminator\Responder::json() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
    }
45
46
    /**
47
     * @param $file
48
     * @return string
49
     */
50
    public function uploadImportData($file)
51
    {
52
        $dir = 'uploads/'.date('Y-m');
53
        $filename = md5(str_random(5)).'.'.$file->getClientOriginalExtension();
54
55
        //Create Directory Monthly
56
        Storage::makeDirectory($dir);
57
58
        //Move file to storage
59
        Storage::putFileAs($dir, $file, $filename);
60
61
        return CRUDBooster::mainpath('import-data').'?file='.base64_encode($dir.'/'.$filename);
62
    }
63
64
    /**
65
     * @param $file
66
     * @return \Illuminate\Contracts\Validation\Validator
67
     */
68
    public function validateForImport($file)
69
    {
70
        return Validator::make(['extension' => $file->getClientOriginalExtension(),], ['extension' => 'in:xls,xlsx,csv']);
71
    }
72
73
    /**
74
     * @param $file_md5
75
     * @param $table
76
     * @param $titleField
77
     */
78
    public function InsertToDB($file_md5, $table, $titleField)
79
    {
80
        $select_column = array_filter(session('select_column'));
0 ignored issues
show
Bug introduced by
It seems like session('select_column') can also be of type Illuminate\Session\Store and Illuminate\Session\SessionManager; however, parameter $input of array_filter() 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

80
        $select_column = array_filter(/** @scrutinizer ignore-type */ session('select_column'));
Loading history...
81
        $table_columns = DB::getSchemaBuilder()->getColumnListing($table);
82
83
        $file = base64_decode(request('file'));
0 ignored issues
show
Bug introduced by
It seems like request('file') can also be of type array; however, parameter $data of base64_decode() does only seem to accept string, 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

83
        $file = base64_decode(/** @scrutinizer ignore-type */ request('file'));
Loading history...
84
        $file = 'storage'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.$file;
85
        $rows = Excel::load($file, function ($reader) {
0 ignored issues
show
Unused Code introduced by
The parameter $reader is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

85
        $rows = Excel::load($file, function (/** @scrutinizer ignore-unused */ $reader) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
        })->get();
87
88
        //$data_import_column = [];
89
        foreach ($rows as $value) {
90
            $a = $this->readAndInsert($select_column, $table_columns, $value);
91
92
            $has_title_field = true;
93
            foreach ($a as $k => $v) {
94
                if ($k == $titleField && $v == '') {
95
                    $has_title_field = false;
96
                    break;
97
                }
98
            }
99
100
            if ($has_title_field == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
101
                continue;
102
            }
103
104
            try {
105
                if (\Schema::hasColumn($table, 'created_at')) {
106
                    $a['created_at'] = YmdHis();
107
                }
108
109
                \DB::table($table)->insert($a);
110
                Cache::increment('success_'.$file_md5);
111
            } catch (\Exception $e) {
112
                $e = (string) $e;
113
                Cache::put('error_'.$file_md5, $e, 500);
114
            }
115
        }
116
    }
117
118
    /**
119
     * @param $selectColumn
120
     * @param $table_columns
121
     * @param $value
122
     * @return array
123
     */
124
    private function readAndInsert($selectColumn, $table_columns, $value)
125
    {
126
        $a = [];
127
        foreach ($selectColumn as $sk => $s) {
128
            $colname = $table_columns[$sk];
129
130
            if (! DbInspector::isForeignKey($colname) || intval($value->$s)) {
131
                $a[$colname] = $value->$s;
132
                continue;
133
            }
134
135
            //Skip if value is empty
136
            if ($value->$s == '') {
137
                continue;
138
            }
139
140
            $relation_table = DbInspector::getRelatedTableName($colname);
141
            $relation_class = $this->resolveController($relation_table);
142
            $relation_class->genericLoader();
143
144
            $titleField = $relation_class->titleField;
145
146
            $relation_insert_data = [];
147
            $relation_insert_data[$titleField] = $value->$s;
148
149
            if (\Schema::hasColumn($relation_table, 'created_at')) {
150
                $relation_insert_data['created_at'] = YmdHis();
151
            }
152
153
            try {
154
                $relation_exists = DB::table($relation_table)->where($titleField, $value->$s)->first();
155
                if ($relation_exists) {
156
                    $relation_primary_key = $relation_class->primaryKey;
157
                    $relation_id = $relation_exists->$relation_primary_key;
158
                } else {
159
                    $relation_id = DB::table($relation_table)->insertGetId($relation_insert_data);
160
                }
161
162
                $a[$colname] = $relation_id;
163
            } catch (\Exception $e) {
164
                //exit($e);
165
            }
166
        }
167
168
        return $a;
169
    }
170
171
    /**
172
     * @param $table
173
     */
174
    private function resolveController($table)
175
    {
176
        $module = ModulesRepo::getByTableName($table);
177
        if (is_null($module)) {
178
            return ;
179
        }
180
        $relation_class = __NAMESPACE__.'\\'.$module->controller;
181
        if (! class_exists($relation_class)) {
182
            $relation_class = ctrlNamespace().'\\'.$module->controller;
183
        }
184
185
        return new $relation_class;
186
    }
187
}