Completed
Pull Request — develop_3.0 (#457)
by Adrien
02:34
created

Reader::setTempFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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