GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ebb95a...02b71f )
by Alireza
08:21 queued 07:03
created

ExportImportController::importDataToDB()   C

Complexity

Conditions 17
Paths 72

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 5.2166
c 0
b 0
f 0
cc 17
nc 72
nop 6

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Serverfireteam\Panel;
4
5
use Illuminate\Routing\Controller;
6
use Illuminate\Support\Facades\Input;
7
use Maatwebsite\Excel\Facades\Excel;
8
9
class ExportImportController extends Controller {
10
11
    protected $failed = false;
12
13
    public function export($entity, $fileType) {
14
        if (strcmp($fileType, "excel") == 0) {
15
            $export = new EntityExport($entity);
16
            return Excel::download($export, $entity.'.xlsx');
17
        }
18
        return \Redirect::to('panel/' . $entity . '/all')->with('export_message', "File type is not excel");
19
    }
20
21
    public function import($entity) {
22
23
        $status = Input::get('status');
24
25
        $filePath = null;
26
        if (Input::hasFile('import_file') && Input::file('import_file')->isValid()) {
27
            $pathTemp = Input::file('import_file')->store('temp');
28
            $filePath = storage_path('app').'/'.$pathTemp;
29
        }
30
31
        if ($filePath)
0 ignored issues
show
Bug Best Practice introduced by
The expression $filePath of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
32
        {
33
            $import = new EntityImport($entity, $status);
34
            Excel::import($import, $filePath);
35
        }
36
37
        $importMessage = ($this->failed == true) ? \Lang::get('panel::fields.importDataFailure') : \Lang::get('panel::fields.importDataSuccess');
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...
38
39
        return \Redirect::to('panel/' . $entity . '/all')->with('import_message', $importMessage);
40
    }
41
42
    public function importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames) {
43
44
	$rows        = $reader->toArray();
45
	$newData     = array();
46
	$updatedData = array();
47
48
	// Check validation of values
49
	foreach ($rows as $i => $row) {
50
		foreach ($notNullColumnNames as $notNullColumn) {
51
			if (!isset($row[$notNullColumn])) {
52
				unset($rows[$i]);
53
			}
54
		}
55
	}
56
57
	if (!$this->failed) {
58
		if ($status == 1) {
59
			$model->truncate();
60
		}
61
		foreach ($rows as $row) {
62
			if (!empty($row[$key])) {
63
				$exists = $model->where($key, '=', $row[$key])->count();
64
				if (!$exists) {
65
					$values = array();
66
					foreach ($columns as $col) {
67
						if ($col != $key) {
68
							$values[$col] = $row[$col];
69
						}
70
					}
71
					$newData[] = $values;
72
				} else if ($status == 2 && $exists) {
73
					$values = array();
74
					foreach ($columns as $col) {
75
						$values[$col] = $row[$col];
76
					}
77
					$updatedData[] = $values;
78
				}
79
			}
80
		}
81
	}
82
83
	// insert data into table
84
	if (!empty($newData)) {
85
		$model->insert($newData);
86
	}
87
88
	// update available data
89
	if (!empty($updatedData)) {
90
		foreach ($updatedData as $data) {
91
			$keyValue = $data[$key];
92
			unset($data[$key]);
93
			$model->where($key, $keyValue)->update($data);
94
		}
95
	}
96
    }
97
}
98