Completed
Pull Request — master (#808)
by
unknown
11:17
created

WriterMultiSheetsAbstract::setColumnWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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