Completed
Push — master ( fb7d68...1391ca )
by Stefan
24:27
created

SheetFile   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 82.35%

Importance

Changes 5
Bugs 2 Features 2
Metric Value
wmc 9
c 5
b 2
f 2
lcom 1
cbo 0
dl 0
loc 61
ccs 14
cts 17
cp 0.8235
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A fwrite() 0 6 2
A rewind() 0 4 1
A getFilePath() 0 4 1
A __destruct() 0 6 3
1
<?php
2
3
namespace OneSheet;
4
5
/**
6
 * Class SheetFile
7
 *
8
 * @package OneSheet
9
 */
10
class SheetFile
11
{
12
    /**
13
     * @var resource
14
     */
15
    private $filePointer;
16
17
    /**
18
     * @var string
19
     */
20
    private $filePath;
21
22
    /**
23
     * SheetFile constructor.
24
     *
25
     * @throws \RuntimeException
26
     */
27 6
    public function __construct()
28
    {
29 6
        $this->filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(null, 1) . '.xml';
30 6
        if (!$this->filePointer = fopen($this->filePath, 'wb+')) {
31
            throw new \RuntimeException("Failed to create temporary file {$this->filePath}!");
32
        }
33 6
    }
34
35
    /**
36
     * @param $string
37
     */
38 6
    public function fwrite($string)
39
    {
40 6
        if (!fwrite($this->filePointer, $string)) {
41
            throw new \RuntimeException("Failed to write to sheet file!");
42
        }
43 6
    }
44
45
    /**
46
     * @return bool
47
     */
48 1
    public function rewind()
49
    {
50 1
        return rewind($this->filePointer);
51
    }
52
53
    /**
54
     * @return string
55
     */
56 1
    public function getFilePath()
57
    {
58 1
        return $this->filePath;
59
    }
60
61
    /**
62
     * Close file pointer and delete sheet file.
63
     */
64 6
    public function __destruct()
65
    {
66 6
        if (!fclose($this->filePointer) || !unlink($this->filePath)) {
67
            throw new \RuntimeException('Failed to close sheetfile!');
68
        }
69 6
    }
70
}
71