TempFile   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 31.25%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 56
rs 10
c 0
b 0
f 0
ccs 10
cts 32
cp 0.3125
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 2 1
A __toString() 0 2 1
A delete() 0 2 1
A __construct() 0 3 1
A getPath() 0 2 1
A getText() 0 2 1
A getLastLines() 0 26 6
1
<?php
2
3
namespace CSFCloud\TempFiles;
4
5
use Exception;
6
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 {
18
        return $this->id;
19
    }
20
21 3
    public function getPath() : string {
22 3
        return $this->full_path;
23
    }
24
25
    public function __toString() : string {
26
        return $this->full_path;
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() {
62 3
        unlink($this->full_path);
63 3
    }
64
65
}