Total Complexity | 12 |
Total Lines | 56 |
Duplicated Lines | 0 % |
Coverage | 31.25% |
Changes | 0 |
1 | <?php |
||
7 | class TempFile { |
||
8 | |||
9 | private $id; |
||
10 | private $full_path; |
||
11 | |||
12 | 3 | public function __construct(string $id, string $path) { |
|
13 | 3 | $this->id = $id; |
|
14 | 3 | $this->full_path = $path; |
|
15 | 3 | } |
|
16 | |||
17 | public function getId() : string { |
||
19 | } |
||
20 | |||
21 | 3 | public function getPath() : string { |
|
22 | 3 | return $this->full_path; |
|
23 | } |
||
24 | |||
25 | public function __toString() : string { |
||
27 | } |
||
28 | |||
29 | 3 | public function getText() : string { |
|
30 | 3 | return file_get_contents($this->full_path); |
|
31 | } |
||
32 | |||
33 | public function getLastLines(int $lines = 10, $buffer = 4096) : string { |
||
34 | $f = fopen($this->full_path, "rb"); |
||
35 | if ($f === false) { |
||
36 | throw new Exception("Cant open file"); |
||
37 | } |
||
38 | |||
39 | fseek($f, -1, SEEK_END); |
||
40 | if (fread($f, 1) != "\n") { |
||
41 | $lines -= 1; |
||
42 | } |
||
43 | $output = ''; |
||
44 | |||
45 | while (ftell($f) > 0 && $lines >= 0) { |
||
46 | $seek = min(ftell($f), $buffer); |
||
47 | fseek($f, -$seek, SEEK_CUR); |
||
48 | $output = ($chunk = fread($f, $seek)) . $output; |
||
49 | fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR); |
||
50 | $lines -= substr_count($chunk, "\n"); |
||
51 | } |
||
52 | |||
53 | while ($lines++ < 0) { |
||
54 | $output = substr($output, strpos($output, "\n") + 1); |
||
55 | } |
||
56 | |||
57 | fclose($f); |
||
58 | return $output; |
||
59 | } |
||
60 | |||
61 | 3 | public function delete() { |
|
63 | 3 | } |
|
64 | |||
65 | } |