Failed Conditions
Push — master ( 1e7115...0e6238 )
by Mark
25:51
created

BaseReader::shutdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader;
4
5
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
6
use PhpOffice\PhpSpreadsheet\Shared\File;
7
8
abstract class BaseReader implements IReader
9
{
10
    /**
11
     * Read data only?
12
     * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
13
     *        or whether it should read both data and formatting.
14
     *
15
     * @var bool
16
     */
17
    protected $readDataOnly = false;
18
19
    /**
20
     * Read empty cells?
21
     * Identifies whether the Reader should read data values for cells all cells, or should ignore cells containing
22
     *         null value or empty string.
23
     *
24
     * @var bool
25
     */
26
    protected $readEmptyCells = true;
27
28
    /**
29
     * Read charts that are defined in the workbook?
30
     * Identifies whether the Reader should read the definitions for any charts that exist in the workbook;.
31
     *
32
     * @var bool
33
     */
34
    protected $includeCharts = false;
35
36
    /**
37
     * Restrict which sheets should be loaded?
38
     * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
39
     *
40
     * @var array of string
41
     */
42
    protected $loadSheetsOnly;
43
44
    /**
45
     * IReadFilter instance.
46
     *
47
     * @var IReadFilter
48
     */
49
    protected $readFilter;
50
51
    protected $fileHandle;
52
53
    /**
54
     * @var XmlScanner
55
     */
56
    protected $securityScanner;
57
58 190
    public function __construct()
59
    {
60 190
        $this->readFilter = new DefaultReadFilter();
61
62
        // A fatal error will bypass the destructor, so we register a shutdown here
63 190
        register_shutdown_function([$this, '__destruct']);
64 190
    }
65
66
    private function shutdown()
67
    {
68
        if ($this->securityScanner !== null) {
69
            $this->securityScanner = null;
70
        }
71
    }
72
73
    public function __destruct()
74
    {
75
        $this->shutdown();
76
    }
77
78 48
    public function getReadDataOnly()
79
    {
80 48
        return $this->readDataOnly;
81
    }
82
83 1
    public function setReadDataOnly($pValue)
84
    {
85 1
        $this->readDataOnly = (bool) $pValue;
86
87 1
        return $this;
88
    }
89
90
    public function getReadEmptyCells()
91
    {
92
        return $this->readEmptyCells;
93
    }
94
95 2
    public function setReadEmptyCells($pValue)
96
    {
97 2
        $this->readEmptyCells = (bool) $pValue;
98
99 2
        return $this;
100
    }
101
102
    public function getIncludeCharts()
103
    {
104
        return $this->includeCharts;
105
    }
106
107 3
    public function setIncludeCharts($pValue)
108
    {
109 3
        $this->includeCharts = (bool) $pValue;
110
111 3
        return $this;
112
    }
113
114
    public function getLoadSheetsOnly()
115
    {
116
        return $this->loadSheetsOnly;
117
    }
118
119 6
    public function setLoadSheetsOnly($value)
120
    {
121 6
        if ($value === null) {
122
            return $this->setLoadAllSheets();
123
        }
124
125 6
        $this->loadSheetsOnly = is_array($value) ? $value : [$value];
126
127 6
        return $this;
128
    }
129
130 1
    public function setLoadAllSheets()
131
    {
132 1
        $this->loadSheetsOnly = null;
133
134 1
        return $this;
135
    }
136
137 87
    public function getReadFilter()
138
    {
139 87
        return $this->readFilter;
140
    }
141
142 9
    public function setReadFilter(IReadFilter $pValue)
143
    {
144 9
        $this->readFilter = $pValue;
145
146 9
        return $this;
147
    }
148
149 3
    public function getSecuritySCanner()
150
    {
151 3
        if (property_exists($this, 'securityScanner')) {
152 3
            return $this->securityScanner;
153
        }
154
155
        return null;
156
    }
157
158
    /**
159
     * Open file for reading.
160
     *
161
     * @param string $pFilename
162
     *
163
     * @throws Exception
164
     */
165 58
    protected function openFile($pFilename)
166
    {
167 58
        File::assertFile($pFilename);
168
169
        // Open file
170 58
        $this->fileHandle = fopen($pFilename, 'r');
171 58
        if ($this->fileHandle === false) {
172
            throw new Exception('Could not open file ' . $pFilename . ' for reading.');
173
        }
174 58
    }
175
}
176