Completed
Pull Request — master (#715)
by
unknown
01:52
created

WriterMultiSheetsAbstract::closeWriter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Box\Spout\Writer;
4
5
use Box\Spout\Common\Creator\HelperFactory;
6
use Box\Spout\Common\Entity\Row;
7
use Box\Spout\Common\Exception\IOException;
8
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
9
use Box\Spout\Common\Manager\OptionsManagerInterface;
10
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
11
use Box\Spout\Writer\Common\Entity\Options;
12
use Box\Spout\Writer\Common\Entity\Sheet;
13
use Box\Spout\Writer\Common\Entity\Worksheet;
14
use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface;
15
use Box\Spout\Writer\Exception\SheetNotFoundException;
16
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
17
use Box\Spout\Writer\Exception\WriterNotOpenedException;
18
19
/**
20
 * Class WriterMultiSheetsAbstract
21
 *
22
 * @abstract
23
 */
24
abstract class WriterMultiSheetsAbstract extends WriterAbstract
25
{
26
    /** @var ManagerFactoryInterface */
27
    private $managerFactory;
28
29
    /** @var WorkbookManagerInterface */
30
    private $workbookManager;
31
32
    /**
33
     * @param OptionsManagerInterface $optionsManager
34
     * @param GlobalFunctionsHelper $globalFunctionsHelper
35
     * @param HelperFactory $helperFactory
36
     * @param ManagerFactoryInterface $managerFactory
37
     */
38 107
    public function __construct(
39
        OptionsManagerInterface $optionsManager,
40
        GlobalFunctionsHelper $globalFunctionsHelper,
41
        HelperFactory $helperFactory,
42
        ManagerFactoryInterface $managerFactory
43
    ) {
44 107
        parent::__construct($optionsManager, $globalFunctionsHelper, $helperFactory);
45 107
        $this->managerFactory = $managerFactory;
46 107
    }
47
48
    /**
49
     * Sets whether new sheets should be automatically created when the max rows limit per sheet is reached.
50
     * This must be set before opening the writer.
51
     *
52
     * @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached
53
     * @return WriterMultiSheetsAbstract
54
     * @throws WriterAlreadyOpenedException If the writer was already opened
55
     */
56 33
    public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically)
57
    {
58 33
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
59
60 31
        $this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, $shouldCreateNewSheetsAutomatically);
61
62 31
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 91
    protected function openWriter()
69
    {
70 91
        if (!$this->workbookManager) {
71 91
            $this->workbookManager = $this->managerFactory->createWorkbookManager($this->optionsManager);
72 91
            $this->workbookManager->addNewSheetAndMakeItCurrent();
73
        }
74 91
    }
75
76
    /**
77
     * Returns all the workbook's sheets
78
     *
79
     * @return Sheet[] All the workbook's sheets
80
     * @throws WriterNotOpenedException If the writer has not been opened yet
81
     */
82 14
    public function getSheets()
83
    {
84 14
        $this->throwIfWorkbookIsNotAvailable();
85
86 14
        $externalSheets = [];
87 14
        $worksheets = $this->workbookManager->getWorksheets();
88
89
        /** @var Worksheet $worksheet */
90 14
        foreach ($worksheets as $worksheet) {
91 14
            $externalSheets[] = $worksheet->getExternalSheet();
92
        }
93
94 14
        return $externalSheets;
95
    }
96
97
    /**
98
     * Creates a new sheet and make it the current sheet. The data will now be written to this sheet.
99
     *
100
     * @return Sheet The created sheet
101
     * @throws IOException
102
     * @throws WriterNotOpenedException If the writer has not been opened yet
103
     */
104 15
    public function addNewSheetAndMakeItCurrent()
105
    {
106 15
        $this->throwIfWorkbookIsNotAvailable();
107 15
        $worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent();
108
109 15
        return $worksheet->getExternalSheet();
110
    }
111
112
    /**
113
     * Returns the current sheet
114
     *
115
     * @return Sheet The current sheet
116
     * @throws WriterNotOpenedException If the writer has not been opened yet
117
     */
118 10
    public function getCurrentSheet()
119
    {
120 10
        $this->throwIfWorkbookIsNotAvailable();
121
122 10
        return $this->workbookManager->getCurrentWorksheet()->getExternalSheet();
123
    }
124
125
    /**
126
     * Sets the given sheet as the current one. New data will be written to this sheet.
127
     * The writing will resume where it stopped (i.e. data won't be truncated).
128
     *
129
     * @param Sheet $sheet The sheet to set as current
130
     * @return void
131
     * @throws SheetNotFoundException If the given sheet does not exist in the workbook
132
     * @throws WriterNotOpenedException If the writer has not been opened yet
133
     */
134 4
    public function setCurrentSheet($sheet)
135
    {
136 4
        $this->throwIfWorkbookIsNotAvailable();
137 4
        $this->workbookManager->setCurrentSheet($sheet);
138 4
    }
139
140
    /**
141
     * @param float $width
142
     * @throws WriterNotOpenedException
143
     */
144 5
    public function setDefaultColumnWidth(float $width)
145
    {
146 5
        $this->throwIfWorkbookIsNotAvailable();
147 3
        $this->workbookManager->setDefaultColumnWidth($width);
148 3
    }
149
150
    /**
151
     * @param float $height
152
     * @throws WriterNotOpenedException
153
     */
154 2
    public function setDefaultRowHeight(float $height)
155
    {
156 2
        $this->throwIfWorkbookIsNotAvailable();
157 2
        $this->workbookManager->setDefaultRowHeight($height);
158 2
    }
159
160
    /**
161
     * @param float|null $width
162
     * @param array $columns One or more columns with this width
163
     * @throws WriterNotOpenedException
164
     */
165 6
    public function setColumnWidth($width, ...$columns)
166
    {
167 6
        $this->throwIfWorkbookIsNotAvailable();
168 6
        $this->workbookManager->setColumnWidth($width, ...$columns);
169 6
    }
170
171
    /**
172
     * @param float $width The width to set
173
     * @param int $start First column index of the range
174
     * @param int $end Last column index of the range
175
     * @throws WriterNotOpenedException
176
     */
177 2
    public function setColumnWidthForRange(float $width, int $start, int $end)
178
    {
179 2
        $this->throwIfWorkbookIsNotAvailable();
180 2
        $this->workbookManager->setColumnWidthForRange($width, $start, $end);
181 2
    }
182
183
    /**
184
     * Checks if the workbook has been created. Throws an exception if not created yet.
185
     *
186
     * @return void
187
     * @throws WriterNotOpenedException If the workbook is not created yet
188
     */
189 87
    protected function throwIfWorkbookIsNotAvailable()
190
    {
191 87
        if (empty($this->workbookManager) || !$this->workbookManager->getWorkbook()) {
192 2
            throw new WriterNotOpenedException('The writer must be opened before performing this action.');
193
        }
194 85
    }
195
196
    /**
197
     * {@inheritdoc}
198
     *
199
     * @throws Exception\WriterException
200
     */
201 79
    protected function addRowToWriter(Row $row)
202
    {
203 79
        $this->throwIfWorkbookIsNotAvailable();
204 79
        $this->workbookManager->addRowToCurrentWorksheet($row);
205 77
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210 84
    protected function closeWriter()
211
    {
212 84
        if ($this->workbookManager) {
213 84
            $this->workbookManager->close($this->filePointer);
214
        }
215 84
    }
216
}
217