Completed
Push — master ( 771781...0978d3 )
by Adrien
02:54
created

Reader::openReader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Box\Spout\Reader\XLSX;
4
5
use Box\Spout\Common\Exception\IOException;
6
use Box\Spout\Reader\AbstractReader;
7
use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper;
8
9
/**
10
 * Class Reader
11
 * This class provides support to read data from a XLSX file
12
 *
13
 * @package Box\Spout\Reader\XLSX
14
 */
15
class Reader extends AbstractReader
16
{
17
    /** @var string Temporary folder where the temporary files will be created */
18
    protected $tempFolder;
19
20
    /** @var \ZipArchive */
21
    protected $zip;
22
23
    /** @var \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings */
24
    protected $sharedStringsHelper;
25
26
    /** @var SheetIterator To iterator over the XLSX sheets */
27
    protected $sheetIterator;
28
29
30
    /**
31
     * @param string $tempFolder Temporary folder where the temporary files will be created
32
     * @return Reader
33
     */
34 3
    public function setTempFolder($tempFolder)
35
    {
36
        $this->tempFolder = $tempFolder;
37
        return $this;
38 3
    }
39
40
    /**
41
     * Returns whether stream wrappers are supported
42
     *
43
     * @return bool
44
     */
45 6
    protected function doesSupportStreamWrapper()
46
    {
47 6
        return false;
48
    }
49
50
    /**
51
     * Opens the file at the given file path to make it ready to be read.
52
     * It also parses the sharedStrings.xml file to get all the shared strings available in memory
53
     * and fetches all the available sheets.
54
     *
55
     * @param  string $filePath Path of the file to be read
56
     * @return void
57
     * @throws \Box\Spout\Common\Exception\IOException If the file at the given path or its content cannot be read
58
     * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
59
     */
60 108
    protected function openReader($filePath)
61
    {
62 108
        $this->zip = new \ZipArchive();
63
64 108
        if ($this->zip->open($filePath) === true) {
65 105
            $this->sharedStringsHelper = new SharedStringsHelper($filePath, $this->tempFolder);
66
67 105
            if ($this->sharedStringsHelper->hasSharedStrings()) {
68
                // Extracts all the strings from the sheets for easy access in the future
69 87
                $this->sharedStringsHelper->extractSharedStrings();
70 81
            }
71
72 99
            $this->sheetIterator = new SheetIterator($filePath, $this->sharedStringsHelper, $this->globalFunctionsHelper, $this->shouldFormatDates, $this->shouldPreserveEmptyRows);
73 96
        } else {
74 3
            throw new IOException("Could not open $filePath for reading.");
75
        }
76 96
    }
77
78
    /**
79
     * Returns an iterator to iterate over sheets.
80
     *
81
     * @return SheetIterator To iterate over sheets
82
     */
83 96
    public function getConcreteSheetIterator()
84
    {
85 96
        return $this->sheetIterator;
86
    }
87
88
    /**
89
     * Closes the reader. To be used after reading the file.
90
     *
91
     * @return void
92
     */
93 93
    protected function closeReader()
94
    {
95 93
        if ($this->zip) {
96 93
            $this->zip->close();
97 93
        }
98
99 93
        if ($this->sharedStringsHelper) {
100 93
            $this->sharedStringsHelper->cleanup();
101 93
        }
102 93
    }
103
}
104