Completed
Push — master ( c6a049...7f7ede )
by Song
03:29
created

TotalRow::render()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Tools;
4
5
use Encore\Admin\Grid\Column;
6
use Illuminate\Database\Query\Builder;
7
use Illuminate\Support\Arr;
8
9
class TotalRow extends AbstractTool
10
{
11
    /**
12
     * @var Builder
13
     */
14
    protected $query;
15
16
    /**
17
     * @var array
18
     */
19
    protected $columns;
20
21
    /**
22
     * TotalRow constructor.
23
     *
24
     * @param Builder $query
25
     * @param array $columns
26
     */
27
    public function __construct($query, array $columns)
28
    {
29
        $this->query   = $query;
30
31
        $this->columns = $columns;
32
    }
33
34
    /**
35
     * Get total value of current column.
36
     *
37
     * @param string $column
38
     * @param mixed $display
39
     *
40
     * @return mixed
41
     */
42
    protected function total($column, $display)
43
    {
44
        if (!is_callable($display) && !is_null($display)) {
45
            return $display;
46
        }
47
48
        $sum = $this->query->sum($column);
49
50
        if (is_callable($display)) {
51
            return call_user_func($display, $sum);
52
        }
53
54
        return $sum;
55
    }
56
57
    /**
58
     * Render total-row.
59
     *
60
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
61
     */
62
    public function render()
63
    {
64
        $columns = $this->getGrid()->columns()->flatMap(function (Column $column) {
65
66
            $name = $column->getName();
67
68
            $total = ($display = Arr::get($this->columns, $name)) ? $this->total($name, $display) : '';
69
70
            return [$name => $total];
71
72
        })->toArray();
73
74
        return view('admin::grid.total-row', compact('columns'));
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin::grid.total-...', compact('columns')); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 74 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
75
    }
76
}