Completed
Push — master ( 7e02e6...ea0e93 )
by wen
03:01
created

Table::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
4
namespace Sco\Admin\View;
5
6
use Illuminate\Support\Collection;
7
use Sco\Admin\Contracts\ColumnInterface;
8
9
class Table extends View
10
{
11
    protected $perPage = 20;
12
13
    protected $pageName = 'page';
14
15
    protected $columns;
16
17
    public function __construct()
18
    {
19
        $this->columns = new Collection();
20
    }
21
22
    /**
23
     * @param array $columns
24
     *
25
     * @return $this
26
     */
27
    public function setColumns(array $columns)
28
    {
29
        foreach ($columns as $column) {
30
            $this->columns->push($column);
31
        }
32
33
        return $this;
34
    }
35
36
    public function getColumns()
37
    {
38
        return $this->columns;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function paginate($perPage = 25, $pageName = 'page')
45
    {
46
        $this->perPage = (int) $perPage;
47
        $this->pageName = $pageName;
48
49
        return $this;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function disablePagination()
56
    {
57
        $this->perPage = 0;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function usePagination()
66
    {
67
        return $this->perPage > 0;
68
    }
69
70
    public function get()
71
    {
72
        $repository = $this->getRepository();
73
        //$orderBy = $this->config->get('orderBy', [$repository->getKeyName(), 'desc']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
        $query = $repository->getQuery();
75
76
        if ($repository->isRestorable()) {
77
            $query->withTrashed();
78
        }
79
80 View Code Duplication
        if ($this->usePagination()) {
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...
81
            $data = $query->paginate($this->perPage, ['*'], $this->pageName);
82
83
            $data->setCollection($this->parseRows($data->getCollection()));
84
        } else {
85
            $data = $this->parseRows($query->get());
0 ignored issues
show
Bug introduced by
It seems like $query->get() targeting Illuminate\Database\Eloquent\Builder::get() can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Sco\Admin\View\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...
86
        }
87
88
        return $data;
89
    }
90
91
    public function toArray()
92
    {
93
        return parent::toArray() + [
94
            'columns' => $this->getColumns(),
95
        ];
96
    }
97
98 View Code Duplication
    protected function parseRows(Collection $rows)
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...
99
    {
100
        if ($rows) {
101
            return $rows->map(function ($row) {
102
                    $newRow = $this->getColumns()->mapWithKeys(function (ColumnInterface $column) use ($row) {
103
                        return [$column->getName() => $column->setModel($row)->getModelValue()];
104
                    });
105
106
                // whether this row has been soft deleted
107
                if ($this->getRepository()->isRestorable()) {
108
                    $newRow->put('_deleted', $row->trashed() ? 1 : 0);
109
                }
110
                return $newRow;
111
            });
112
        }
113
        return collect();
114
    }
115
}
116