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

WriterMultiSheetsAbstract::openWriter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
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
     * @throws WriterAlreadyOpenedException If the writer was already opened
54
     * @return WriterMultiSheetsAbstract
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,
61 31
            $shouldCreateNewSheetsAutomatically);
62
63 31
        return $this;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 91
    protected function openWriter()
70
    {
71 91
        if (!$this->workbookManager) {
72 91
            $this->workbookManager = $this->managerFactory->createWorkbookManager($this->optionsManager);
73 91
            $this->workbookManager->addNewSheetAndMakeItCurrent();
74
        }
75 91
    }
76
77
    /**
78
     * Returns all the workbook's sheets
79
     *
80
     * @return Sheet[] All the workbook's sheets
81
     * @throws WriterNotOpenedException If the writer has not been opened yet
82
     */
83 14
    public function getSheets()
84
    {
85 14
        $this->throwIfWorkbookIsNotAvailable();
86
87 14
        $externalSheets = [];
88 14
        $worksheets = $this->workbookManager->getWorksheets();
89
90
        /** @var Worksheet $worksheet */
91 14
        foreach ($worksheets as $worksheet) {
92 14
            $externalSheets[] = $worksheet->getExternalSheet();
93
        }
94
95 14
        return $externalSheets;
96
    }
97
98
    /**
99
     * Creates a new sheet and make it the current sheet. The data will now be written to this sheet.
100
     *
101
     * @throws IOException
102
     * @throws WriterNotOpenedException If the writer has not been opened yet
103
     * @return Sheet The created sheet
104
     */
105 15
    public function addNewSheetAndMakeItCurrent()
106
    {
107 15
        $this->throwIfWorkbookIsNotAvailable();
108 15
        $worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent();
109
110 15
        return $worksheet->getExternalSheet();
111
    }
112
113
    /**
114
     * Returns the current sheet
115
     *
116
     * @throws WriterNotOpenedException If the writer has not been opened yet
117
     * @return Sheet The current sheet
118
     */
119 10
    public function getCurrentSheet()
120
    {
121 10
        $this->throwIfWorkbookIsNotAvailable();
122
123 10
        return $this->workbookManager->getCurrentWorksheet()->getExternalSheet();
124
    }
125
126
    /**
127
     * Sets the given sheet as the current one. New data will be written to this sheet.
128
     * The writing will resume where it stopped (i.e. data won't be truncated).
129
     *
130
     * @param Sheet $sheet The sheet to set as current
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
     * @return void
134
     */
135 4
    public function setCurrentSheet($sheet)
136
    {
137 4
        $this->throwIfWorkbookIsNotAvailable();
138 4
        $this->workbookManager->setCurrentSheet($sheet);
139 4
    }
140
141
    /**
142
     * @param float $width
143
     * @throws WriterNotOpenedException
144
     */
145 5
    public function setDefaultColumnWidth(float $width)
146
    {
147 5
        $this->throwIfWorkbookIsNotAvailable();
148 3
        $this->workbookManager->setDefaultColumnWidth($width);
149 3
    }
150
151
    /**
152
     * @param float $height
153
     * @throws WriterNotOpenedException
154
     */
155 2
    public function setDefaultRowHeight(float $height)
156
    {
157 2
        $this->throwIfWorkbookIsNotAvailable();
158 2
        $this->workbookManager->setDefaultRowHeight($height);
159 2
    }
160
161
    /**
162
     * @param float|null $width
163
     * @param array $columns One or more columns with this width
164
     * @throws WriterNotOpenedException
165
     */
166 6
    public function setColumnWidth($width, ...$columns)
167
    {
168 6
        $this->throwIfWorkbookIsNotAvailable();
169 6
        $this->workbookManager->setColumnWidth($width, ...$columns);
170 6
    }
171
172
    /**
173
     * @param float $width The width to set
174
     * @param int $start First column index of the range
175
     * @param int $end Last column index of the range
176
     * @throws WriterNotOpenedException
177
     */
178 2
    public function setColumnWidthForRange(float $width, int $start, int $end)
179
    {
180 2
        $this->throwIfWorkbookIsNotAvailable();
181 2
        $this->workbookManager->setColumnWidthForRange($width, $start, $end);
182 2
    }
183
184
    /**
185
     * Checks if the workbook has been created. Throws an exception if not created yet.
186
     *
187
     * @throws WriterNotOpenedException If the workbook is not created yet
188
     * @return void
189
     */
190 87
    protected function throwIfWorkbookIsNotAvailable()
191
    {
192 87
        if (empty($this->workbookManager) || !$this->workbookManager->getWorkbook()) {
193 2
            throw new WriterNotOpenedException('The writer must be opened before performing this action.');
194
        }
195 85
    }
196
197
    /**
198
     * {@inheritdoc}
199
     *
200
     * @throws Exception\WriterException
201
     */
202 79
    protected function addRowToWriter(Row $row)
203
    {
204 79
        $this->throwIfWorkbookIsNotAvailable();
205 79
        $this->workbookManager->addRowToCurrentWorksheet($row);
206 77
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211 84
    protected function closeWriter()
212
    {
213 84
        if ($this->workbookManager) {
214 84
            $this->workbookManager->close($this->filePointer);
215
        }
216 84
    }
217
}
218