Completed
Push — master ( 85c58c...2c5e88 )
by wen
10:27
created

Table::initialize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Display;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
use Sco\Admin\Contracts\Display\ColumnInterface;
9
use Sco\Admin\Display\Concerns\WithPagination;
10
11
class Table extends Display
12
{
13
    use WithPagination;
14
15
    protected $columns;
16
17
    protected $type = 'table';
18
19
    public function __construct()
20
    {
21
        parent::__construct();
22
23
        $this->columns = new Collection();
24
    }
25
26
    public function initialize()
27
    {
28
        parent::initialize();
29
30
        $orderBy = request()->query('orderBy', '');
31
        $sortedBy = request()->query('sortedBy', '');
32
33
        if (! empty($orderBy) && ! empty($sortedBy)) {
34
            $this->orderBy($orderBy, $sortedBy);
35
        }
36
    }
37
38
    /**
39
     * @param array $columns
40
     *
41
     * @return $this
42
     */
43
    public function setColumns(array $columns)
44
    {
45
        foreach ($columns as $column) {
46
            $this->columns->push($column);
47
        }
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return \Illuminate\Support\Collection
54
     */
55
    public function getColumns()
56
    {
57
        return $this->columns;
58
    }
59
60 View Code Duplication
    public function get()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
    {
62
        if ($this->isPagination()) {
63
            $data = $this->paginate();
64
65
            return $data->setCollection($this->parseRows($data->getCollection()));
66
        }
67
68
        return $this->parseRows($this->getQuery()->get());
0 ignored issues
show
Bug introduced by
It seems like $this->getQuery()->get() targeting Illuminate\Database\Eloquent\Builder::get() can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Sco\Admin\Display\Table::parseRows() does only seem to accept object<Illuminate\Support\Collection>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
69
    }
70
71
    public function toArray()
72
    {
73
        return parent::toArray() + [
74
                'columns' => $this->getColumns(),
75
            ];
76
    }
77
78
    protected function parseRows(Collection $rows)
79
    {
80
        return $rows->map(function (Model $row) {
81
            $newRow = $this->getColumns()->mapWithKeys(function (
82
                ColumnInterface $column
83
            ) use ($row) {
84
                return [
85
                    $column->getName() => $column->setModel($row)->getValue(),
86
                ];
87
            });
88
89
            // whether this row has been soft deleted
90
            if ($this->getRepository()->isRestorable()) {
91
                $newRow->put('_deleted', $row->trashed() ? 1 : 0);
92
            }
93
            $newRow->put('_primary', $row->getKey());
94
95
            return $newRow;
96
        });
97
    }
98
}
99