Passed
Pull Request — master (#1093)
by Iman
06:16
created

ImportData   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A postDoImportChunk() 0 13 3
B getImportData() 0 34 6
1
<?php
2
3
namespace crocodicstudio\crudbooster\controllers\CBController;
4
5
use crocodicstudio\crudbooster\controllers\Helpers\IndexImport;
6
use Illuminate\Support\Facades\DB;
7
use Maatwebsite\Excel\Facades\Excel;
8
use Illuminate\Support\Facades\Route;
9
10
trait ImportData
11
{
12
    public $button_import = true;
13
14
    public function getImportData()
15
    {
16
        $this->genericLoader();
0 ignored issues
show
Bug introduced by
It seems like genericLoader() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

16
        $this->/** @scrutinizer ignore-call */ 
17
               genericLoader();
Loading history...
17
18
        $data = [];
19
        $data['page_menu'] = Route::getCurrentRoute()->getActionName();
20
        $data['page_title'] = 'Import Data '.CB::getCurrentModule()->name;
21
22
        if (! request('file') || request('import')) {
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

22
        if (! /** @scrutinizer ignore-call */ request('file') || request('import')) {
Loading history...
23
            return $this->cbView('crudbooster::import', $data);
0 ignored issues
show
Bug introduced by
It seems like cbView() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

23
            return $this->/** @scrutinizer ignore-call */ cbView('crudbooster::import', $data);
Loading history...
24
        }
25
26
        $file = 'storage'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.base64_decode(request('file'));
27
        $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

27
        $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...
28
        })->get();
29
30
        session()->put('total_data_import', count($rows));
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

30
        /** @scrutinizer ignore-call */ 
31
        session()->put('total_data_import', count($rows));
Loading history...
31
32
        $data_import_column = [];
33
        foreach ($rows as $value) {
34
            $a = [];
35
            foreach ($value as $k => $v) {
36
                $a[] = $k;
37
            }
38
            if (count($a)) {
39
                $data_import_column = $a;
40
            }
41
            break;
42
        }
43
44
        $data['table_columns'] = DB::getSchemaBuilder()->getColumnListing($this->table);
45
        $data['data_import_column'] = $data_import_column;
46
47
        return $this->cbView('crudbooster::import', $data);
48
    }
49
50
    public function postDoImportChunk()
51
    {
52
        $import = app(IndexImport::class);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
        $import = /** @scrutinizer ignore-call */ app(IndexImport::class);
Loading history...
53
        $this->genericLoader();
54
        $fileMD5 = md5(request('file'));
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

54
        $fileMD5 = md5(/** @scrutinizer ignore-call */ request('file'));
Loading history...
55
56
        if (request('file') && request('resume') == 1) {
57
            return $import->handleImportProgress($fileMD5);
58
        }
59
60
        $import->InsertToDB($fileMD5, $this->table, $this->title_field);
61
62
        return response()->json(['status' => true]);
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

62
        return /** @scrutinizer ignore-call */ response()->json(['status' => true]);
Loading history...
63
    }
64
65
66
}