Completed
Push — master ( 30460b...74da27 )
by wen
03:07
created

ModelConfig::orderBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Sco\Admin\Config;
4
5
use JsonSerializable;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Support\Jsonable;
8
use Sco\Admin\Exceptions\InvalidArgumentException;
9
10
class ModelConfig extends Config implements ConfigInterface, Arrayable, Jsonable, JsonSerializable
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: jsonSerialize, toArray, toJson
Loading history...
11
{
12
13
    protected $orderBy = [];
14
    protected $modelFilters = [];
15
16
    /**
17
     * Get Model
18
     *
19
     * @return \Illuminate\Database\Eloquent\Model
20
     * @throws \Sco\Admin\Exceptions\InvalidArgumentException
21
     */
22
    protected function getModel()
23
    {
24
        $modelName = $this->getOriginal('model');
25
        if (class_exists($modelName)) {
26
            return new $modelName();
27
        }
28
        throw new InvalidArgumentException("class {$modelName} not found");
29
    }
30
31
    public function filters($filters)
32
    {
33
        $this->modelFilters = $filters;
34
        return $this;
35
    }
36
37
    public function orderBy($column, $direction = 'asc')
38
    {
39
        $this->orderBy = compact('column', 'direction');
40
        return $this;
41
    }
42
43
    public function paginate()
44
    {
45
        $data = $this->parseWhere()
0 ignored issues
show
Unused Code introduced by
$data 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...
46
            ->orderBy($this->orderBy['column'], $this->orderBy['direction'])
47
            ->paginate(20);
48
    }
49
50
    protected function parseWhere()
51
    {
52
        $model = $this->getModel();
53
        if (!empty($this->modelFilters)) {
54
            foreach ($this->modelFilters as $key => $filter) {
55
                list($operator, $value) = is_array($filter) ? $filter : ['=', $filter];
56
                $model->where($key, $operator, $value);
57
            }
58
        }
59
        return $model;
60
    }
61
}
62