Completed
Pull Request — master (#269)
by
unknown
04:23
created

LogEntryCollection::tree()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php namespace Arcanedev\LogViewer\Entities;
2
3
use Arcanedev\LogViewer\Helpers\LogParser;
4
use Illuminate\Pagination\LengthAwarePaginator;
5
use Illuminate\Support\Collection;
6
7
/**
8
 * Class     LogEntryCollection
9
 *
10
 * @package  Arcanedev\LogViewer\Entities
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class LogEntryCollection extends Collection
14
{
15
    /* -----------------------------------------------------------------
16
     |  Main Methods
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * Load raw log entries.
22
     *
23
     * @param  string  $raw
24
     *
25
     * @return self
26
     */
27 183
    public function load($raw)
28
    {
29 183
        foreach (LogParser::parse($raw) as $entry) {
30 183
            list($level, $header, $stack) = array_values($entry);
31
32 183
            $this->push(new LogEntry($level, $header, $stack));
33
        }
34
35 183
        return $this;
36
    }
37
38
    /**
39
     * Paginate log entries.
40
     *
41
     * @param  int  $perPage
42
     *
43
     * @return \Illuminate\Pagination\LengthAwarePaginator
44
     */
45
    public function paginate($perPage = 20)
46
    {
47
        $page = request()->get('page', 1);
48
        $path = request()->url();
49
50
        return new LengthAwarePaginator(
51
            $this->forPage($page, $perPage),
52
            $this->count(),
53
            $perPage,
54
            $page,
55
            compact('path')
56
        );
57
    }
58
59
    /**
60
     * Get filtered log entries by level.
61
     *
62
     * @param  string  $level
63
     *
64
     * @return self
65
     */
66 9
    public function filterByLevel($level)
67
    {
68
        return $this->filter(function(LogEntry $entry) use ($level) {
69 9
            return $entry->isSameLevel($level);
70 9
        });
71
    }
72
73
    /**
74
     * Get log entries stats.
75
     *
76
     * @return array
77
     */
78 75
    public function stats()
79
    {
80 75
        $counters = $this->initStats();
81
82 75
        foreach ($this->groupBy('level') as $level => $entries) {
83 75
            $counters[$level] = $count = count($entries);
84 75
            $counters['all'] += $count;
85
        }
86
87 75
        return $counters;
88
    }
89
90
    /**
91
     * Get the log entries navigation tree.
92
     *
93
     * @param  bool|false  $trans
94
     *
95
     * @return array
96
     */
97 27
    public function tree($trans = false)
98
    {
99 27
        $tree = $this->stats();
100
101
        array_walk($tree, function(&$count, $level) use ($trans) {
102
            $count = [
103 27
                'name'  => $trans ? log_levels()->get($level) : $level,
104 27
                'count' => $count,
105
            ];
106 27
        });
107
108 27
        return $tree;
109
    }
110
111
    /* -----------------------------------------------------------------
112
     |  Other Methods
113
     | -----------------------------------------------------------------
114
     */
115
116
    /**
117
     * Init stats counters.
118
     *
119
     * @return array
120
     */
121 75
    private function initStats()
122
    {
123 75
        $levels = array_merge_recursive(
124 75
            ['all'],
125 75
            array_keys(log_viewer()->levels(true))
126
        );
127
128
        return array_map(function () {
129 75
            return 0;
130 75
        }, array_flip($levels));
131
    }
132
}
133