Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
PlainLogFile
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 15
420
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPath
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIndexPath
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 exists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 modified
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 open
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 seek
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 tell
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 eof
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 gets
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 close
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 optimize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 purge
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 serialize
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 deserialize
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Olz\Apps\Logs\Utils;
4
5use Olz\Utils\WithUtilsTrait;
6
7class PlainLogFile 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, 'PlainLogFile::modified failed');
31        return $result;
32    }
33
34    /** @return resource */
35    public function open(string $mode): mixed {
36        $result = fopen($this->path, $mode);
37        $this->generalUtils()->checkNotBool($result, 'PlainLogFile::open failed');
38        return $result;
39    }
40
41    /** @param resource $fp */
42    public function seek(mixed $fp, int $offset, int $whence = SEEK_SET): int {
43        return fseek($fp, $offset, $whence);
44    }
45
46    /** @param resource $fp */
47    public function tell(mixed $fp): int {
48        $result = ftell($fp);
49        $this->generalUtils()->checkNotBool($result, 'PlainLogFile::tell failed');
50        return $result;
51    }
52
53    /** @param resource $fp */
54    public function eof(mixed $fp): bool {
55        return feof($fp);
56    }
57
58    /** @param resource $fp */
59    public function gets(mixed $fp): ?string {
60        $result = fgets($fp);
61        return $result === false ? null : $result;
62    }
63
64    /** @param resource $fp */
65    public function close(mixed $fp): bool {
66        return fclose($fp);
67    }
68
69    public function optimize(): void {
70    }
71
72    public function purge(): void {
73        if (is_file($this->path)) {
74            unlink($this->path);
75            $this->log()->info("Removed old log file {$this->path}");
76        }
77        if (is_file($this->indexPath)) {
78            unlink($this->indexPath);
79            $this->log()->info("Removed old log index file {$this->indexPath}");
80        }
81    }
82
83    public function serialize(): string {
84        return json_encode([
85            'class' => self::class,
86            'path' => $this->path,
87            'indexPath' => $this->indexPath,
88        ]) ?: '{}';
89    }
90
91    public static function deserialize(string $serialized): ?LogFileInterface {
92        $deserialized = json_decode($serialized, true);
93        if ($deserialized['class'] !== self::class) {
94            return null;
95        }
96        return new self($deserialized['path'], $deserialized['indexPath']);
97    }
98}