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

SheetFile::fwrite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2.0625
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