Completed
Push — master ( 01c44a...cc980e )
by Stefan
03:21 queued 40s
created

SheetFile   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 87.5%

Importance

Changes 6
Bugs 2 Features 2
Metric Value
wmc 8
c 6
b 2
f 2
lcom 1
cbo 0
dl 0
loc 59
ccs 14
cts 16
cp 0.875
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A rewind() 0 4 1
A getFilePath() 0 4 1
A __destruct() 0 6 3
A fwrite() 0 4 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 8
    public function __construct()
28
    {
29 8
        $this->filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(null, 1) . '.xml';
30 8
        if (!$this->filePointer = fopen($this->filePath, 'wb+')) {
31
            throw new \RuntimeException("Failed to create temporary file {$this->filePath}!");
32
        }
33 8
    }
34
35
    /**
36
     * @param $string
37
     */
38 8
    public function fwrite($string)
39
    {
40 8
        fwrite($this->filePointer, $string);
41 8
    }
42
43
    /**
44
     * @return bool
45
     */
46 2
    public function rewind()
47
    {
48 2
        return rewind($this->filePointer);
49
    }
50
51
    /**
52
     * @return string
53
     */
54 2
    public function getFilePath()
55
    {
56 2
        return $this->filePath;
57
    }
58
59
    /**
60
     * Close file pointer and delete sheet file.
61
     */
62 8
    public function __destruct()
63
    {
64 8
        if (!fclose($this->filePointer) || !unlink($this->filePath)) {
65
            throw new \RuntimeException('Failed to close sheetfile!');
66
        }
67 8
    }
68
}
69