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

LogCollection::total()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php namespace Arcanedev\LogViewer\Entities;
2
3
use Arcanedev\LogViewer\Contracts\Utilities\Filesystem as FilesystemContract;
4
use Arcanedev\LogViewer\Exceptions\LogNotFoundException;
5
use Illuminate\Pagination\LengthAwarePaginator;
6
use Illuminate\Support\Collection;
7
8
/**
9
 * Class     LogCollection
10
 *
11
 * @package  Arcanedev\LogViewer\Entities
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class LogCollection extends Collection
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /** @var \Arcanedev\LogViewer\Contracts\Utilities\Filesystem */
22
    private $filesystem;
23
24
    /* -----------------------------------------------------------------
25
     |  Constructor
26
     | -----------------------------------------------------------------
27
     */
28
29
    /**
30
     * LogCollection constructor.
31
     *
32
     * @param  array  $items
33
     */
34 171
    public function __construct($items = [])
35
    {
36 171
        $this->setFilesystem(app(FilesystemContract::class));
37
38 171
        parent::__construct($items);
39
40 171
        if (empty($items)) $this->load();
41 171
    }
42
43
    /* -----------------------------------------------------------------
44
     |  Getters & Setters
45
     | -----------------------------------------------------------------
46
     */
47
48
    /**
49
     * Set the filesystem instance.
50
     *
51
     * @param  \Arcanedev\LogViewer\Contracts\Utilities\Filesystem  $filesystem
52
     *
53
     * @return \Arcanedev\LogViewer\Entities\LogCollection
54
     */
55 171
    public function setFilesystem(FilesystemContract $filesystem)
56
    {
57 171
        $this->filesystem = $filesystem;
58
59 171
        return $this;
60
    }
61
62
    /* -----------------------------------------------------------------
63
     |  Main Methods
64
     | -----------------------------------------------------------------
65
     */
66
67
    /**
68
     * Load all logs.
69
     *
70
     * @return \Arcanedev\LogViewer\Entities\LogCollection
71
     */
72 171
    private function load()
73
    {
74 171
        foreach($this->filesystem->dates(true) as $date => $path) {
75 171
            $this->put(basename($path), Log::make($path, $path, $this->filesystem->read($path)));
76
        }
77
78 171
        return $this;
79
    }
80
81
    /**
82
     * Get a log.
83
     *
84
     * @param  string      $date
85
     * @param  mixed|null  $default
86
     *
87
     * @return \Arcanedev\LogViewer\Entities\Log
88
     *
89
     * @throws \Arcanedev\LogViewer\Exceptions\LogNotFoundException
90
     */
91 45
    public function get($date, $default = null)
92
    {
93 45
        if ( ! $this->has($date))
94 45
            throw new LogNotFoundException("Log not found in this date [$date]");
95
96
        return parent::get($date, $default);
97
    }
98
99
    /**
100
     * Paginate logs.
101
     *
102
     * @param  int  $perPage
103
     *
104
     * @return \Illuminate\Pagination\LengthAwarePaginator
105
     */
106 6
    public function paginate($perPage = 30)
107
    {
108 6
        $page = request()->get('page', 1);
109 6
        $path = request()->url();
110
111 6
        return new LengthAwarePaginator(
112 6
            $this->forPage($page, $perPage),
113 6
            $this->count(),
114 4
            $perPage,
115 4
            $page,
116 6
            compact('path')
117
        );
118
    }
119
120
    /**
121
     * Get a log (alias).
122
     *
123
     * @see get()
124
     *
125
     * @param  string  $date
126
     *
127
     * @return \Arcanedev\LogViewer\Entities\Log
128
     */
129 21
    public function log($date)
130
    {
131 21
        return $this->get($date);
132
    }
133
134
135
    /**
136
     * Get log entries.
137
     *
138
     * @param  string  $date
139
     * @param  string  $level
140
     *
141
     * @return \Arcanedev\LogViewer\Entities\LogEntryCollection
142
     */
143 18
    public function entries($date, $level = 'all')
144
    {
145 18
        return $this->get($date)->entries($level);
146
    }
147
148
    /**
149
     * Get logs statistics.
150
     *
151
     * @return array
152
     */
153 45
    public function stats()
154
    {
155 45
        $stats = [];
156
157 45
        foreach ($this->items as $date => $log) {
158
            /** @var \Arcanedev\LogViewer\Entities\Log $log */
159 45
            $stats[$date] = $log->stats();
160
        }
161
162 45
        return $stats;
163
    }
164
165
    /**
166
     * List the log files (dates).
167
     *
168
     * @return array
169
     */
170 12
    public function dates()
171
    {
172 12
        return $this->keys()->toArray();
173
    }
174
175
    /**
176
     * Get entries total.
177
     *
178
     * @param  string  $level
179
     *
180
     * @return int
181
     */
182 12
    public function total($level = 'all')
183
    {
184
        return (int) $this->sum(function (Log $log) use ($level) {
185 12
            return $log->entries($level)->count();
186 12
        });
187
    }
188
189
    /**
190
     * Get logs tree.
191
     *
192
     * @param  bool  $trans
193
     *
194
     * @return array
195
     */
196 12
    public function tree($trans = false)
197
    {
198 12
        $tree = [];
199
200 12
        foreach ($this->items as $date => $log) {
201
            /** @var \Arcanedev\LogViewer\Entities\Log $log */
202 12
            $tree[$date] = $log->tree($trans);
203
        }
204
205 12
        return $tree;
206
    }
207
208
    /**
209
     * Get logs menu.
210
     *
211
     * @param  bool  $trans
212
     *
213
     * @return array
214
     */
215 12
    public function menu($trans = true)
216
    {
217 12
        $menu = [];
218
219 12
        foreach ($this->items as $date => $log) {
220
            /** @var \Arcanedev\LogViewer\Entities\Log $log */
221 12
            $menu[$date] = $log->menu($trans);
222
        }
223
224 12
        return $menu;
225
    }
226
}
227