Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 1 |
GzLogFile | |
0.00% |
0 / 46 |
|
0.00% |
0 / 15 |
462 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getIndexPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
exists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
modified | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
open | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
seek | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
tell | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
eof | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
gets | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
close | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
optimize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
purge | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
serialize | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
deserialize | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Olz\Apps\Logs\Utils; |
4 | |
5 | use Olz\Utils\WithUtilsTrait; |
6 | |
7 | class GzLogFile implements LogFileInterface { |
8 | use WithUtilsTrait; |
9 | |
10 | public function __construct( |
11 | protected string $path, |
12 | protected string $indexPath, |
13 | ) { |
14 | } |
15 | |
16 | public function getPath(): string { |
17 | return $this->path; |
18 | } |
19 | |
20 | public function getIndexPath(): string { |
21 | return $this->indexPath; |
22 | } |
23 | |
24 | public function exists(): bool { |
25 | return is_file($this->path); |
26 | } |
27 | |
28 | public function modified(): int { |
29 | $result = filemtime($this->path); |
30 | $this->generalUtils()->checkNotBool($result, 'GzLogFile::modified failed'); |
31 | return $result; |
32 | } |
33 | |
34 | /** @return resource */ |
35 | public function open(string $mode): mixed { |
36 | $compatibility_map = [ |
37 | 'r' => 'rb', |
38 | 'w' => 'wb', |
39 | 'rb' => 'rb', |
40 | 'wb' => 'wb', |
41 | ]; |
42 | $result = gzopen($this->path, $compatibility_map[$mode]); |
43 | $this->generalUtils()->checkNotBool($result, 'GzLogFile::open failed'); |
44 | return $result; |
45 | } |
46 | |
47 | /** @param resource $fp */ |
48 | public function seek(mixed $fp, int $offset, int $whence = SEEK_SET): int { |
49 | if ($whence === SEEK_END) { |
50 | ob_start(); |
51 | gzpassthru($fp); |
52 | ob_end_clean(); |
53 | $size = gztell($fp); |
54 | return gzseek($fp, $size - $offset, SEEK_SET); |
55 | } |
56 | return gzseek($fp, $offset, $whence); |
57 | } |
58 | |
59 | /** @param resource $fp */ |
60 | public function tell(mixed $fp): int { |
61 | $result = gztell($fp); |
62 | $this->generalUtils()->checkNotBool($result, 'GzLogFile::tell failed'); |
63 | return $result; |
64 | } |
65 | |
66 | /** @param resource $fp */ |
67 | public function eof(mixed $fp): bool { |
68 | return gzeof($fp); |
69 | } |
70 | |
71 | /** @param resource $fp */ |
72 | public function gets(mixed $fp): ?string { |
73 | $result = gzgets($fp); |
74 | return $result === false ? null : $result; |
75 | } |
76 | |
77 | /** @param resource $fp */ |
78 | public function close(mixed $fp): bool { |
79 | return gzclose($fp); |
80 | } |
81 | |
82 | public function optimize(): void { |
83 | } |
84 | |
85 | public function purge(): void { |
86 | if (is_file($this->path)) { |
87 | unlink($this->path); |
88 | $this->log()->info("Removed old log file {$this->path}"); |
89 | } |
90 | if (is_file($this->indexPath)) { |
91 | unlink($this->indexPath); |
92 | $this->log()->info("Removed old log index file {$this->indexPath}"); |
93 | } |
94 | } |
95 | |
96 | public function serialize(): string { |
97 | return json_encode([ |
98 | 'class' => self::class, |
99 | 'path' => $this->path, |
100 | 'indexPath' => $this->indexPath, |
101 | ]) ?: '{}'; |
102 | } |
103 | |
104 | public static function deserialize(string $serialized): ?LogFileInterface { |
105 | $deserialized = json_decode($serialized, true); |
106 | if ($deserialized['class'] !== self::class) { |
107 | return null; |
108 | } |
109 | return new self($deserialized['path'], $deserialized['indexPath']); |
110 | } |
111 | } |