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

Log   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 45.45%

Importance

Changes 0
Metric Value
dl 0
loc 239
ccs 20
cts 44
cp 0.4545
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 3

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getPath() 0 4 1
A file() 0 4 1
A size() 0 4 1
A createdAt() 0 4 1
A updatedAt() 0 4 1
A make() 0 5 1
A entries() 0 6 2
A getByLevel() 0 4 1
A stats() 0 4 1
A tree() 0 4 1
A menu() 0 4 1
A toArray() 0 8 1
A toJson() 0 4 1
A jsonSerialize() 0 4 1
A formatSize() 0 10 2
1
<?php namespace Arcanedev\LogViewer\Entities;
2
3
use Carbon\Carbon;
4
use Illuminate\Contracts\Support\Arrayable;
5
use Illuminate\Contracts\Support\Jsonable;
6
use JsonSerializable;
7
use SplFileInfo;
8
9
/**
10
 * Class     Log
11
 *
12
 * @package  Arcanedev\LogViewer\Entities
13
 * @author   ARCANEDEV <[email protected]>
14
 *
15
 * @todo     Add a stats method
16
 */
17
class Log implements Arrayable, Jsonable, JsonSerializable
18
{
19
    /* -----------------------------------------------------------------
20
     |  Properties
21
     | -----------------------------------------------------------------
22
     */
23
24
    /** @var string */
25
    public $date;
26
27
    /** @var string */
28
    private $path;
29
30
    /** @var \Arcanedev\LogViewer\Entities\LogEntryCollection */
31
    private $entries;
32
33
    /** @var \SplFileInfo */
34
    private $file;
35
36
    /* -----------------------------------------------------------------
37
     |  Constructor
38
     | -----------------------------------------------------------------
39
     */
40
41
    /**
42
     * Log constructor.
43
     *
44
     * @param  string  $date
45
     * @param  string  $path
46
     * @param  string  $raw
47
     */
48 171
    public function __construct($date, $path, $raw)
49
    {
50 171
        $this->date    = $date;
51 171
        $this->path    = $path;
52 171
        $this->file    = new SplFileInfo($path);
53 171
        $this->entries = (new LogEntryCollection)->load($raw);
54 171
    }
55
56
    /* -----------------------------------------------------------------
57
     |  Getters & Setters
58
     | -----------------------------------------------------------------
59
     */
60
61
    /**
62
     * Get log path.
63
     *
64
     * @return string
65
     */
66
    public function getPath()
67
    {
68
        return $this->path;
69
    }
70
71
    /**
72
     * Get file info.
73
     *
74
     * @return \SplFileInfo
75
     */
76
    public function file()
77
    {
78
        return $this->file;
79
    }
80
81
    /**
82
     * Get file size.
83
     *
84
     * @return string
85
     */
86
    public function size()
87
    {
88
        return $this->formatSize($this->file->getSize());
89
    }
90
91
    /**
92
     * Get file creation date.
93
     *
94
     * @return \Carbon\Carbon
95
     */
96
    public function createdAt()
97
    {
98
        return Carbon::createFromTimestamp($this->file()->getATime());
99
    }
100
101
    /**
102
     * Get file modification date.
103
     *
104
     * @return \Carbon\Carbon
105
     */
106
    public function updatedAt()
107
    {
108
        return Carbon::createFromTimestamp($this->file()->getMTime());
109
    }
110
111
    /* -----------------------------------------------------------------
112
     |  Main Methods
113
     | -----------------------------------------------------------------
114
     */
115
116
    /**
117
     * Make a log object.
118
     *
119
     * @param  string  $date
120
     * @param  string  $path
121
     * @param  string  $raw
122
     *
123
     * @return self
124
     */
125 171
    public static function make($date, $path, $raw)
126
    {
127
128 171
        return new self($date, $path, $raw);
129
    }
130
131
    /**
132
     * Get log entries.
133
     *
134
     * @param  string  $level
135
     *
136
     * @return \Arcanedev\LogViewer\Entities\LogEntryCollection
137
     */
138 12
    public function entries($level = 'all')
139
    {
140 12
        return $level === 'all'
141 6
            ? $this->entries
142 12
            : $this->getByLevel($level);
143
    }
144
145
    /**
146
     * Get filtered log entries by level.
147
     *
148
     * @param  string  $level
149
     *
150
     * @return \Arcanedev\LogViewer\Entities\LogEntryCollection
151
     */
152 6
    public function getByLevel($level)
153
    {
154 6
        return $this->entries->filterByLevel($level);
155
    }
156
157
    /**
158
     * Get log stats.
159
     *
160
     * @return array
161
     */
162 45
    public function stats()
163
    {
164 45
        return $this->entries->stats();
165
    }
166
167
    /**
168
     * Get the log navigation tree.
169
     *
170
     * @param  bool  $trans
171
     *
172
     * @return array
173
     */
174 24
    public function tree($trans = false)
175
    {
176 24
        return $this->entries->tree($trans);
177
    }
178
179
    /**
180
     * Get log entries menu.
181
     *
182
     * @param  bool  $trans
183
     *
184
     * @return array
185
     */
186 12
    public function menu($trans = true)
187
    {
188 12
        return log_menu()->make($this, $trans);
189
    }
190
191
    /* -----------------------------------------------------------------
192
     |  Convert Methods
193
     | -----------------------------------------------------------------
194
     */
195
196
    /**
197
     * Get the log as a plain array.
198
     *
199
     * @return array
200
     */
201
    public function toArray()
202
    {
203
        return [
204
            'date'    => $this->date,
205
            'path'    => $this->path,
206
            'entries' => $this->entries->toArray()
207
        ];
208
    }
209
210
    /**
211
     * Convert the object to its JSON representation.
212
     *
213
     * @param  int  $options
214
     *
215
     * @return string
216
     */
217
    public function toJson($options = 0)
218
    {
219
        return json_encode($this->toArray(), $options);
220
    }
221
222
    /**
223
     * Serialize the log object to json data.
224
     *
225
     * @return array
226
     */
227
    public function jsonSerialize()
228
    {
229
        return $this->toArray();
230
    }
231
232
    /* -----------------------------------------------------------------
233
     |  Other Methods
234
     | -----------------------------------------------------------------
235
     */
236
237
    /**
238
     * Format the file size.
239
     *
240
     * @param  int  $bytes
241
     * @param  int  $precision
242
     *
243
     * @return string
244
     */
245
    private function formatSize($bytes, $precision = 2)
246
    {
247
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];
248
249
        $bytes = max($bytes, 0);
250
        $pow   = floor(($bytes ? log($bytes) : 0) / log(1024));
251
        $pow   = min($pow, count($units) - 1);
252
253
        return round($bytes / pow(1024, $pow), $precision).' '.$units[$pow];
254
    }
255
}
256