Completed
Pull Request — master (#7)
by Stefan
03:14
created

SheetFile::fwrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 2
    public function __construct()
28
    {
29 2
        $this->filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(null, 1) . '.xml';
30 2
        if (!$this->filePointer = fopen($this->filePath, 'wb+')) {
31
            throw new \RuntimeException("Failed to create temporary file {$this->filePath}!");
32
        }
33 2
    }
34
35
    /**
36
     * @param $string
37
     * @return bool
38
     */
39 2
    public function fwrite($string)
40
    {
41 2
        return (bool)fwrite($this->filePointer, $string);
42
    }
43
44
    /**
45
     * @return bool
46
     */
47 1
    public function rewind()
48
    {
49 1
        return rewind($this->filePointer);
50
    }
51
52
    /**
53
     * Close and delete the file.
54
     */
55 2
    public function __destruct()
56
    {
57 2
        fclose($this->filePointer);
58 2
        unlink($this->filePath);
59 2
    }
60
61
    /**
62
     * @return string
63
     */
64 1
    public function getFilePath()
65
    {
66 1
        return $this->filePath;
67
    }
68
}
69