Completed
Pull Request — master (#87)
by ARCANEDEV
06:11
created

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