Completed
Push — master ( 690f8b...84e983 )
by ARCANEDEV
15:22
created

StatsTable::totals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Arcanedev\LogViewer\Tables;
2
3
use Arcanedev\LogViewer\Contracts\Utilities\LogLevels as LogLevelsContract;
4
use Illuminate\Support\Arr;
5
use Illuminate\Support\Collection;
6
7
/**
8
 * Class     StatsTable
9
 *
10
 * @package  Arcanedev\LogViewer\Tables
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class StatsTable extends AbstractTable
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Main Functions
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Make a stats table instance.
21
     *
22
     * @param  array                                               $data
23
     * @param  \Arcanedev\LogViewer\Contracts\Utilities\LogLevels  $levels
24
     * @param  string|null                                         $locale
25
     *
26
     * @return \Arcanedev\LogViewer\Tables\StatsTable
27 72
     */
28
    public static function make(array $data, LogLevelsContract $levels, $locale = null)
29 72
    {
30
        return new self($data, $levels, $locale);
31
    }
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Other Functions
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * Prepare table header.
39
     *
40
     * @param  array  $data
41
     *
42
     * @return array
43 144
     */
44
    protected function prepareHeader(array $data)
45 144
    {
46
        return array_merge_recursive(
47 144
            [
48 144
                'date' => $this->translate('general.date'),
49 108
                'all'  => $this->translate('general.all'),
50 144
            ],
51 108
            $this->levels->names($this->locale)
52
        );
53
    }
54
55
    /**
56
     * Prepare table rows.
57
     *
58
     * @param  array  $data
59
     *
60
     * @return array
61 144
     */
62
    protected function prepareRows(array $data)
63 144
    {
64
        $rows = [];
65 144
66 144
        foreach ($data as $date => $levels) {
67 108
            $rows[$date] = array_merge(compact('date'), $levels);
68
        }
69 144
70
        return $rows;
71
    }
72
73
    /**
74
     * Prepare table footer.
75
     *
76
     * @param  array  $data
77
     *
78
     * @return array
79 144
     */
80
    protected function prepareFooter(array $data)
81 144
    {
82
        $footer = [];
83 144
84 144
        foreach ($data as $date => $levels) {
85 144
            foreach ($levels as $level => $count) {
86 144
                if ( ! isset($footer[$level])) {
87 108
                    $footer[$level] = 0;
88
                }
89 144
90 108
                $footer[$level] += $count;
91 108
            }
92
        }
93 144
94
        return $footer;
95
    }
96
97
    /**
98
     * Get totals.
99
     *
100
     * @param  string|null  $locale
101
     *
102
     * @return \Illuminate\Support\Collection
103 24
     */
104
    public function totals($locale = null)
105 24
    {
106
        $this->setLocale($locale);
107 24
108 24
        $totals = Collection::make();
109
110 24
        foreach (Arr::except($this->footer(), 'all') as $level => $count) {
111 24
            $totals->put($level, [
112 24
                'label'     => $this->translate("levels.$level"),
113 24
                'value'     => $count,
114 24
                'color'     => $this->color($level),
115 24
                'highlight' => $this->color($level),
116
            ]);
117 18
        }
118
119 24
        return $totals;
120
    }
121
122
    /**
123
     * Get json totals data.
124
     *
125
     * @param  string|null  $locale
126
     *
127
     * @return string
128
     */
129
    public function totalsJson($locale = null)
130
    {
131
        return $this->totals($locale)->toJson(JSON_PRETTY_PRINT);
132
    }
133
}
134