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

EntityImport::onRow()   C

Complexity

Conditions 17
Paths 96

Size

Total Lines 53

Duplication

Lines 9
Ratio 16.98 %

Importance

Changes 0
Metric Value
dl 9
loc 53
rs 5.2166
c 0
b 0
f 0
cc 17
nc 96
nop 1

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
4
namespace Serverfireteam\Panel;
5
6
use Illuminate\Support\Facades\Hash;
7
use Maatwebsite\Excel\Concerns\ToModel;
8
use Serverfireteam\Panel\libs\AppHelper;
9
use Maatwebsite\Excel\Row;
10
use Maatwebsite\Excel\Concerns\OnEachRow;
11
use Maatwebsite\Excel\Concerns\WithHeadingRow;
12
13
class EntityImport implements OnEachRow, WithHeadingRow
14
{
15
    protected $entity;
16
    protected $notNullColumnNames;
17
    protected $model;
18
    protected $columns;
19
    protected $key;
20
    protected $status;
21
22
    public function __construct($entity, $status)
23
    {
24
        $this->entity = $entity;
25
        $appHelper = new libs\AppHelper();
26
27
        $className = $appHelper->getModel($entity);
28
        $model     = new $className;
29
        $tablePrefix = \DB::getTablePrefix();
30
        $table     = $model->getTable();
31
        $columns   = \Schema::getColumnListing($table);
32
        $key	   = $model->getKeyName();
33
34
        $notNullColumnNames = array();
35
        $notNullColumnsList = \DB::select(\DB::raw("SHOW COLUMNS FROM `" . $tablePrefix.$table . "` where `Null` = 'no'"));
36
        if (!empty($notNullColumnsList)) {
37
            foreach ($notNullColumnsList as $notNullColumn) {
38
                $notNullColumnNames[] = $notNullColumn->Field;
39
            }
40
        }
41
        $this->notNullColumnNames = $notNullColumnNames;
42
        $this->model = $model;
43
        $this->columns = $columns;
44
        $this->key = $key;
45
        $this->status = $status;
46
47
        if ($this->status == 1) {
48
            $this->model->truncate();
49
        }
50
    }
51
52
    /**
53
     * @param array $row
54
     *
55
     * @return User|null
56
     */
57
    public function onRow(Row $row)
58
    {
59
        $rowIndex = $row->getIndex();
0 ignored issues
show
Unused Code introduced by
$rowIndex is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
        $row      = $row->toArray();
61
62
        $newData     = array();
63
        $updatedData = array();
64
65
        $hasNotNULL = false;
66
        //check not NULL columns
67
        foreach ($this->notNullColumnNames as $notNullColumn) {
68
            //if there is not the column in the row
69
            if (!isset($row[$notNullColumn])) {
70
                $hasNotNULL = true;
71
            }
72
        }
73
74
        if (!empty($row[$this->key])) {
75
            $exists = $this->model->where($this->key, '=', $row[$this->key])->count();
76
            if (!$exists && !$hasNotNULL) {
77
                $values = array();
78 View Code Duplication
                foreach ($this->columns as $col) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
                    if ($col != $this->key && array_key_exists($col, $row)) {
80
                        $values[$col] = $row[$col];
81
                    }
82
                }
83
                $newData[] = $values;
84
            } else if ($this->status == 2 && $exists) {
85
                $values = array();
86 View Code Duplication
                foreach ($this->columns as $col) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
                    if (array_key_exists($col, $row))
88
                        $values[$col] = $row[$col];
89
                }
90
                $updatedData[] = $values;
91
            }
92
        }
93
94
        // insert data into table
95
        if (!empty($newData)) {
96
            $this->model->insert($newData);
97
        }
98
99
        // update available data
100
        if (!empty($updatedData)) {
101
            foreach ($updatedData as $data) {
102
                $keyValue = (int)$data[$this->key];
103
                unset($data[$this->key]);
104
                try{
105
                    $this->model->where($this->key, $keyValue)->update($data);
106
                }catch (\Exception $e){}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
107
            }
108
        }
109
    }
110
}