Completed
Pull Request — master (#338)
by Adrien
02:31
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 \ZipArchive */
18
    protected $zip;
19
20
    /** @var \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings */
21
    protected $sharedStringsHelper;
22
23
    /** @var SheetIterator To iterator over the XLSX sheets */
24
    protected $sheetIterator;
25
26
27
    /**
28
     * Returns the reader's current options
29
     *
30
     * @return ReaderOptions
31
     */
32 111
    protected function getOptions()
33 3
    {
34 111
        if (!isset($this->options)) {
35 111
            $this->options = new ReaderOptions();
36 111
        }
37 111
        return $this->options;
38
    }
39
40
    /**
41
     * @param string $tempFolder Temporary folder where the temporary files will be created
42
     * @return Reader
43
     */
44
    public function setTempFolder($tempFolder)
45
    {
46
        $this->getOptions()->setTempFolder($tempFolder);
47
        return $this;
48
    }
49
50
    /**
51
     * Returns whether stream wrappers are supported
52
     *
53
     * @return bool
54
     */
55 6
    protected function doesSupportStreamWrapper()
56
    {
57 6
        return false;
58
    }
59
60
    /**
61
     * Opens the file at the given file path to make it ready to be read.
62
     * It also parses the sharedStrings.xml file to get all the shared strings available in memory
63
     * and fetches all the available sheets.
64
     *
65
     * @param  string $filePath Path of the file to be read
66
     * @return void
67
     * @throws \Box\Spout\Common\Exception\IOException If the file at the given path or its content cannot be read
68
     * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
69
     */
70 108
    protected function openReader($filePath)
71
    {
72 108
        $this->zip = new \ZipArchive();
73
74 108
        if ($this->zip->open($filePath) === true) {
75 105
            $this->sharedStringsHelper = new SharedStringsHelper($filePath, $this->getOptions()->getTempFolder());
76
77 105
            if ($this->sharedStringsHelper->hasSharedStrings()) {
78
                // Extracts all the strings from the sheets for easy access in the future
79 87
                $this->sharedStringsHelper->extractSharedStrings();
80 81
            }
81
82 99
            $this->sheetIterator = new SheetIterator($filePath, $this->getOptions(), $this->sharedStringsHelper, $this->globalFunctionsHelper);
83 96
        } else {
84 3
            throw new IOException("Could not open $filePath for reading.");
85
        }
86 96
    }
87
88
    /**
89
     * Returns an iterator to iterate over sheets.
90
     *
91
     * @return SheetIterator To iterate over sheets
92
     */
93 96
    protected function getConcreteSheetIterator()
94
    {
95 96
        return $this->sheetIterator;
96
    }
97
98
    /**
99
     * Closes the reader. To be used after reading the file.
100
     *
101
     * @return void
102
     */
103 93
    protected function closeReader()
104
    {
105 93
        if ($this->zip) {
106 93
            $this->zip->close();
107 93
        }
108
109 93
        if ($this->sharedStringsHelper) {
110 93
            $this->sharedStringsHelper->cleanup();
111 93
        }
112 93
    }
113
}
114