Completed
Pull Request — develop_3.0 (#427)
by Adrien
04:16 queued 01:53
created

throwIfWorkbookIsNotAvailable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace Box\Spout\Writer;
4
5
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
6
use Box\Spout\Writer\Common\Options;
7
use Box\Spout\Writer\Entity\Worksheet;
8
use Box\Spout\Writer\Exception\WriterNotOpenedException;
9
use Box\Spout\Writer\Factory\InternalFactoryInterface;
10
use Box\Spout\Writer\Manager\WorkbookManagerInterface;
11
12
/**
13
 * Class WriterMultiSheetsAbstract
14
 *
15
 * @package Box\Spout\Writer
16
 * @abstract
17
 */
18
abstract class WriterMultiSheetsAbstract extends WriterAbstract
19
{
20
21
    /** @var InternalFactoryInterface */
22
    private $internalFactory;
23
24
    /** @var WorkbookManagerInterface */
25
    private $workbookManager;
26
27
    /**
28
     * @param OptionsManagerInterface $optionsManager
29
     * @param InternalFactoryInterface $internalFactory
30
     */
31 99
    public function __construct(OptionsManagerInterface $optionsManager, InternalFactoryInterface $internalFactory)
32
    {
33 99
        parent::__construct($optionsManager);
34 99
        $this->internalFactory = $internalFactory;
35 99
    }
36
37
    /**
38
     * Sets whether new sheets should be automatically created when the max rows limit per sheet is reached.
39
     * This must be set before opening the writer.
40
     *
41
     * @api
42
     * @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached
43
     * @return WriterMultiSheetsAbstract
44
     * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
45
     */
46 35
    public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically)
47
    {
48 35
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
49
50 33
        $this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, $shouldCreateNewSheetsAutomatically);
51 33
        return $this;
52
    }
53
54
    /**
55
     * Configures the write and sets the current sheet pointer to a new sheet.
56
     *
57
     * @return void
58
     * @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
59
     */
60 89
    protected function openWriter()
61
    {
62 89
        if (!$this->workbookManager) {
63 89
            $this->workbookManager = $this->internalFactory->createWorkbookManager($this->optionsManager);
64 89
            $this->workbookManager->addNewSheetAndMakeItCurrent();
65
        }
66 89
    }
67
68
    /**
69
     * Returns all the workbook's sheets
70
     *
71
     * @api
72
     * @return Common\Sheet[] All the workbook's sheets
73
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
74
     */
75 14
    public function getSheets()
76
    {
77 14
        $this->throwIfWorkbookIsNotAvailable();
78
79 14
        $externalSheets = [];
80 14
        $worksheets = $this->workbookManager->getWorksheets();
81
82
        /** @var Worksheet $worksheet */
83 14
        foreach ($worksheets as $worksheet) {
84 14
            $externalSheets[] = $worksheet->getExternalSheet();
85
        }
86
87 14
        return $externalSheets;
88
    }
89
90
    /**
91
     * Creates a new sheet and make it the current sheet. The data will now be written to this sheet.
92
     *
93
     * @api
94
     * @return Common\Sheet The created sheet
95
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
96
     */
97 15
    public function addNewSheetAndMakeItCurrent()
98
    {
99 15
        $this->throwIfWorkbookIsNotAvailable();
100 15
        $worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent();
101
102 15
        return $worksheet->getExternalSheet();
103
    }
104
105
    /**
106
     * Returns the current sheet
107
     *
108
     * @api
109
     * @return Common\Sheet The current sheet
110
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
111
     */
112 8
    public function getCurrentSheet()
113
    {
114 8
        $this->throwIfWorkbookIsNotAvailable();
115 8
        return $this->workbookManager->getCurrentWorksheet()->getExternalSheet();
116
    }
117
118
    /**
119
     * Sets the given sheet as the current one. New data will be written to this sheet.
120
     * The writing will resume where it stopped (i.e. data won't be truncated).
121
     *
122
     * @api
123
     * @param Common\Sheet $sheet The sheet to set as current
124
     * @return void
125
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
126
     * @throws \Box\Spout\Writer\Exception\SheetNotFoundException If the given sheet does not exist in the workbook
127
     */
128 4
    public function setCurrentSheet($sheet)
129
    {
130 4
        $this->throwIfWorkbookIsNotAvailable();
131 4
        $this->workbookManager->setCurrentSheet($sheet);
132 4
    }
133
134
    /**
135
     * Checks if the workbook has been created. Throws an exception if not created yet.
136
     *
137
     * @return void
138
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the workbook is not created yet
139
     */
140 70
    protected function throwIfWorkbookIsNotAvailable()
141
    {
142 70
        if (!$this->workbookManager->getWorkbook()) {
143
            throw new WriterNotOpenedException('The writer must be opened before performing this action.');
144
        }
145 70
    }
146
147
    /**
148
     * Adds data to the currently opened writer.
149
     * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
150
     * with the creation of new worksheets if one worksheet has reached its maximum capicity.
151
     *
152
     * @param array $dataRow Array containing data to be written.
153
     *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
154
     * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row.
155
     * @return void
156
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet
157
     * @throws \Box\Spout\Common\Exception\IOException If unable to write data
158
     */
159 64
    protected function addRowToWriter(array $dataRow, $style)
160
    {
161 64
        $this->throwIfWorkbookIsNotAvailable();
162 64
        $this->workbookManager->addRowToCurrentWorksheet($dataRow, $style);
163 61
    }
164
165
    /**
166
     * Closes the writer, preventing any additional writing.
167
     *
168
     * @return void
169
     */
170 70
    protected function closeWriter()
171
    {
172 70
        if ($this->workbookManager) {
173 70
            $this->workbookManager->close($this->filePointer);
174
        }
175 70
    }
176
}
177
178