Completed
Pull Request — develop_3.0 (#427)
by Adrien
02:25
created

WriterMultiSheetsAbstract::getCurrentSheet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
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\Exception\WriterNotOpenedException;
8
use Box\Spout\Writer\Factory\InternalFactoryInterface;
9
use Box\Spout\Writer\Manager\WorkbookManagerInterface;
10
11
/**
12
 * Class WriterMultiSheetsAbstract
13
 *
14
 * @package Box\Spout\Writer
15
 * @abstract
16
 */
17
abstract class WriterMultiSheetsAbstract extends WriterAbstract
18
{
19
20
    /** @var InternalFactoryInterface */
21
    private $internalFactory;
22
23
    /** @var WorkbookManagerInterface */
24
    private $workbookManager;
25
26
    /**
27
     * @param OptionsManagerInterface $optionsManager
28
     * @param InternalFactoryInterface $internalFactory
29
     */
30 99
    public function __construct(OptionsManagerInterface $optionsManager, InternalFactoryInterface $internalFactory)
31
    {
32 99
        parent::__construct($optionsManager);
33 99
        $this->internalFactory = $internalFactory;
34 99
    }
35
36
    /**
37
     * Sets whether new sheets should be automatically created when the max rows limit per sheet is reached.
38
     * This must be set before opening the writer.
39
     *
40
     * @api
41
     * @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached
42
     * @return WriterMultiSheetsAbstract
43
     * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
44
     */
45 35
    public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically)
46
    {
47 35
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
48
49 33
        $this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, $shouldCreateNewSheetsAutomatically);
50 33
        return $this;
51
    }
52
53
    /**
54
     * Configures the write and sets the current sheet pointer to a new sheet.
55
     *
56
     * @return void
57
     * @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
58
     */
59 89
    protected function openWriter()
60
    {
61 89
        if (!$this->workbookManager) {
62 89
            $this->workbookManager = $this->internalFactory->createWorkbookManager($this->optionsManager);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->internalFactory->...($this->optionsManager) of type object<Box\Spout\Writer\...ternalFactoryInterface> is incompatible with the declared type object<Box\Spout\Writer\...rkbookManagerInterface> of property $workbookManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63 89
            $this->workbookManager->addNewSheetAndMakeItCurrent();
0 ignored issues
show
Bug introduced by
The method addNewSheetAndMakeItCurrent() does not seem to exist on object<Box\Spout\Writer\...ternalFactoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
        }
65 89
    }
66
67
    /**
68
     * Returns all the workbook's sheets
69
     *
70
     * @api
71
     * @return Common\Sheet[] All the workbook's sheets
72
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
73
     */
74 14
    public function getSheets()
75
    {
76 14
        $this->throwIfWorkbookIsNotAvailable();
77
78 14
        $externalSheets = [];
79 14
        $worksheets = $this->workbookManager->getWorksheets();
80
81
        /** @var Common\Internal\WorksheetInterface $worksheet */
82 14
        foreach ($worksheets as $worksheet) {
83 14
            $externalSheets[] = $worksheet->getExternalSheet();
84
        }
85
86 14
        return $externalSheets;
87
    }
88
89
    /**
90
     * Creates a new sheet and make it the current sheet. The data will now be written to this sheet.
91
     *
92
     * @api
93
     * @return Common\Sheet The created sheet
94
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
95
     */
96 15
    public function addNewSheetAndMakeItCurrent()
97
    {
98 15
        $this->throwIfWorkbookIsNotAvailable();
99 15
        $worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent();
100
101 15
        return $worksheet->getExternalSheet();
102
    }
103
104
    /**
105
     * Returns the current sheet
106
     *
107
     * @api
108
     * @return Common\Sheet The current sheet
109
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
110
     */
111 8
    public function getCurrentSheet()
112
    {
113 8
        $this->throwIfWorkbookIsNotAvailable();
114 8
        return $this->workbookManager->getCurrentWorksheet()->getExternalSheet();
115
    }
116
117
    /**
118
     * Sets the given sheet as the current one. New data will be written to this sheet.
119
     * The writing will resume where it stopped (i.e. data won't be truncated).
120
     *
121
     * @api
122
     * @param Common\Sheet $sheet The sheet to set as current
123
     * @return void
124
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
125
     * @throws \Box\Spout\Writer\Exception\SheetNotFoundException If the given sheet does not exist in the workbook
126
     */
127 4
    public function setCurrentSheet($sheet)
128
    {
129 4
        $this->throwIfWorkbookIsNotAvailable();
130 4
        $this->workbookManager->setCurrentSheet($sheet);
131 4
    }
132
133
    /**
134
     * Checks if the workbook has been created. Throws an exception if not created yet.
135
     *
136
     * @return void
137
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the workbook is not created yet
138
     */
139 70
    protected function throwIfWorkbookIsNotAvailable()
140
    {
141 70
        if (!$this->workbookManager->getWorkbook()) {
142
            throw new WriterNotOpenedException('The writer must be opened before performing this action.');
143
        }
144 70
    }
145
146
    /**
147
     * Adds data to the currently opened writer.
148
     * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
149
     * with the creation of new worksheets if one worksheet has reached its maximum capicity.
150
     *
151
     * @param array $dataRow Array containing data to be written.
152
     *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
153
     * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row.
154
     * @return void
155
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet
156
     * @throws \Box\Spout\Common\Exception\IOException If unable to write data
157
     */
158 64
    protected function addRowToWriter(array $dataRow, $style)
159
    {
160 64
        $this->throwIfWorkbookIsNotAvailable();
161 64
        $this->workbookManager->addRowToCurrentWorksheet($dataRow, $style);
162 61
    }
163
164
    /**
165
     * Closes the writer, preventing any additional writing.
166
     *
167
     * @return void
168
     */
169 70
    protected function closeWriter()
170
    {
171 70
        if ($this->workbookManager) {
172 70
            $this->workbookManager->close($this->filePointer);
173
        }
174 70
    }
175
}
176
177