Completed
Push — master ( 4b5c92...896769 )
by Adrien
16:06 queued 09:43
created

BaseWriter::maybeCloseFileHandle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 5
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer;
4
5
abstract class BaseWriter implements IWriter
6
{
7
    /**
8
     * Write charts that are defined in the workbook?
9
     * Identifies whether the Writer should write definitions for any charts that exist in the PhpSpreadsheet object;.
10
     *
11
     * @var bool
12
     */
13
    protected $includeCharts = false;
14
15
    /**
16
     * Pre-calculate formulas
17
     * Forces PhpSpreadsheet to recalculate all formulae in a workbook when saving, so that the pre-calculated values are
18
     * immediately available to MS Excel or other office spreadsheet viewer when opening the file.
19
     *
20
     * @var bool
21
     */
22
    protected $preCalculateFormulas = true;
23
24
    /**
25
     * Use disk caching where possible?
26
     *
27
     * @var bool
28
     */
29
    private $useDiskCaching = false;
30
31
    /**
32
     * Disk caching directory.
33
     *
34
     * @var string
35
     */
36
    private $diskCachingDirectory = './';
37
38
    /**
39
     * @var resource
40
     */
41
    protected $fileHandle;
42
43
    /**
44
     * @var bool
45
     */
46
    private $shouldCloseFile;
47
48
    public function getIncludeCharts()
49
    {
50
        return $this->includeCharts;
51
    }
52
53 15
    public function setIncludeCharts($pValue)
54
    {
55 15
        $this->includeCharts = (bool) $pValue;
56
57 15
        return $this;
58
    }
59
60 23
    public function getPreCalculateFormulas()
61
    {
62 23
        return $this->preCalculateFormulas;
63
    }
64
65
    public function setPreCalculateFormulas($pValue)
66
    {
67
        $this->preCalculateFormulas = (bool) $pValue;
68
69
        return $this;
70
    }
71
72 114
    public function getUseDiskCaching()
73
    {
74 114
        return $this->useDiskCaching;
75
    }
76
77
    public function setUseDiskCaching($pValue, $pDirectory = null)
78
    {
79
        $this->useDiskCaching = $pValue;
80
81
        if ($pDirectory !== null) {
82
            if (is_dir($pDirectory)) {
83
                $this->diskCachingDirectory = $pDirectory;
84
            } else {
85
                throw new Exception("Directory does not exist: $pDirectory");
86
            }
87
        }
88
89
        return $this;
90
    }
91
92
    public function getDiskCachingDirectory()
93
    {
94
        return $this->diskCachingDirectory;
95
    }
96
97
    /**
98
     * Open file handle.
99
     *
100
     * @param resource|string $filename
101
     */
102 142
    public function openFileHandle($filename): void
103
    {
104 142
        if (is_resource($filename)) {
105 7
            $this->fileHandle = $filename;
106 7
            $this->shouldCloseFile = false;
107
108 7
            return;
109
        }
110
111 135
        $fileHandle = fopen($filename, 'wb+');
112 135
        if ($fileHandle === false) {
113
            throw new Exception('Could not open file ' . $filename . ' for writing.');
114
        }
115
116 135
        $this->fileHandle = $fileHandle;
117 135
        $this->shouldCloseFile = true;
118 135
    }
119
120
    /**
121
     * Close file handle only we opened it ourselves.
122
     */
123 142
    protected function maybeCloseFileHandle(): void
124
    {
125 142
        if ($this->shouldCloseFile) {
126 135
            if (!fclose($this->fileHandle)) {
127
                throw new Exception('Could not close file after writing.');
128
            }
129
        }
130 142
    }
131
}
132