Completed
Pull Request — master (#280)
by ARCANEDEV
04:53 queued 12s
created

LogCollection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 206
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0
wmc 17
lcom 2
cbo 6

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A setFilesystem() 0 6 1
A get() 0 7 2
A paginate() 0 13 1
A log() 0 4 1
A entries() 0 4 1
A stats() 0 11 2
A dates() 0 4 1
A total() 0 6 1
A tree() 0 11 2
A menu() 0 11 2
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
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

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