Completed
Push — master ( 4c8845...0e974a )
by ARCANEDEV
09:55
created

Log::file()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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