Completed
Pull Request — master (#284)
by ARCANEDEV
06:39 queued 02:45
created

LogCollection::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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