Failed Conditions
Pull Request — master (#738)
by
unknown
03:16
created

WriterMultiSheetsAbstract::mergeCells()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
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\Helper\GlobalFunctionsHelper;
8
use Box\Spout\Common\Manager\OptionsManagerInterface;
9
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
10
use Box\Spout\Writer\Common\Entity\Options;
11
use Box\Spout\Writer\Common\Entity\Sheet;
12
use Box\Spout\Writer\Common\Entity\Worksheet;
13
use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface;
14
use Box\Spout\Writer\Exception\SheetNotFoundException;
15
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
16
use Box\Spout\Writer\Exception\WriterNotOpenedException;
17
18
/**
19
 * Class WriterMultiSheetsAbstract
20
 *
21
 * @abstract
22
 */
23
abstract class WriterMultiSheetsAbstract extends WriterAbstract
24
{
25
    /** @var ManagerFactoryInterface */
26
    private $managerFactory;
27
28
    /** @var WorkbookManagerInterface */
29
    private $workbookManager;
30
31
    /**
32
     * @param OptionsManagerInterface $optionsManager
33
     * @param GlobalFunctionsHelper $globalFunctionsHelper
34
     * @param HelperFactory $helperFactory
35
     * @param ManagerFactoryInterface $managerFactory
36
     */
37 92
    public function __construct(
38
        OptionsManagerInterface $optionsManager,
39
        GlobalFunctionsHelper $globalFunctionsHelper,
40
        HelperFactory $helperFactory,
41
        ManagerFactoryInterface $managerFactory
42
    ) {
43 92
        parent::__construct($optionsManager, $globalFunctionsHelper, $helperFactory);
44 92
        $this->managerFactory = $managerFactory;
45 92
    }
46
47
    /**
48
     * Sets whether new sheets should be automatically created when the max rows limit per sheet is reached.
49
     * This must be set before opening the writer.
50
     *
51
     * @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached
52
     * @throws WriterAlreadyOpenedException If the writer was already opened
53
     * @return WriterMultiSheetsAbstract
54
     */
55 33
    public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically)
56
    {
57 33
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
58
59 31
        $this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, $shouldCreateNewSheetsAutomatically);
60
61 31
        return $this;
62
    }
63
64
    /**
65
     * Set columns widths as list. If value is null will set column with default width (8.43)
66
     * @param array $columnWidths
67
     * @return WriterMultiSheetsAbstract
68
     */
69
    public function setColumnWidths(array $columnWidths)
70
    {
71
        $this->optionsManager->setOption(Options::COLUMN_WIDTHS, $columnWidths);
72
73
        return $this;
74
    }
75
76
    /**
77
     * Undocumented function
78
     *
79
     * @param array $range
0 ignored issues
show
Documentation introduced by
There is no parameter named $range. Did you maybe mean $range1?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
80
     * @return void
81
     */
82
    public function mergeCells(array $range1, array $range2)
83
    {
84
        $this->optionsManager->addOption(Options::MERGE_CELLS, [$range1, $range2]);
85
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 80
    protected function openWriter()
93
    {
94 80
        if (!$this->workbookManager) {
95 80
            $this->workbookManager = $this->managerFactory->createWorkbookManager($this->optionsManager);
96 80
            $this->workbookManager->addNewSheetAndMakeItCurrent();
97
        }
98 80
    }
99
100
    /**
101
     * Returns all the workbook's sheets
102
     *
103
     * @throws WriterNotOpenedException If the writer has not been opened yet
104
     * @return Sheet[] All the workbook's sheets
105
     */
106 14
    public function getSheets()
107
    {
108 14
        $this->throwIfWorkbookIsNotAvailable();
109
110 14
        $externalSheets = [];
111 14
        $worksheets = $this->workbookManager->getWorksheets();
112
113
        /** @var Worksheet $worksheet */
114 14
        foreach ($worksheets as $worksheet) {
115 14
            $externalSheets[] = $worksheet->getExternalSheet();
116
        }
117
118 14
        return $externalSheets;
119
    }
120
121
    /**
122
     * Creates a new sheet and make it the current sheet. The data will now be written to this sheet.
123
     *
124
     * @throws WriterNotOpenedException If the writer has not been opened yet
125
     * @return Sheet The created sheet
126
     */
127 15
    public function addNewSheetAndMakeItCurrent()
128
    {
129 15
        $this->throwIfWorkbookIsNotAvailable();
130 15
        $worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent();
131
132 15
        return $worksheet->getExternalSheet();
133
    }
134
135
    /**
136
     * Returns the current sheet
137
     *
138
     * @throws WriterNotOpenedException If the writer has not been opened yet
139
     * @return Sheet The current sheet
140
     */
141 10
    public function getCurrentSheet()
142
    {
143 10
        $this->throwIfWorkbookIsNotAvailable();
144
145 10
        return $this->workbookManager->getCurrentWorksheet()->getExternalSheet();
146
    }
147
148
    /**
149
     * Sets the given sheet as the current one. New data will be written to this sheet.
150
     * The writing will resume where it stopped (i.e. data won't be truncated).
151
     *
152
     * @param Sheet $sheet The sheet to set as current
153
     * @throws WriterNotOpenedException If the writer has not been opened yet
154
     * @throws SheetNotFoundException If the given sheet does not exist in the workbook
155
     * @return void
156
     */
157 4
    public function setCurrentSheet($sheet)
158
    {
159 4
        $this->throwIfWorkbookIsNotAvailable();
160 4
        $this->workbookManager->setCurrentSheet($sheet);
161 4
    }
162
163
    /**
164
     * Checks if the workbook has been created. Throws an exception if not created yet.
165
     *
166
     * @throws WriterNotOpenedException If the workbook is not created yet
167
     * @return void
168
     */
169 74
    protected function throwIfWorkbookIsNotAvailable()
170
    {
171 74
        if (!$this->workbookManager->getWorkbook()) {
172
            throw new WriterNotOpenedException('The writer must be opened before performing this action.');
173
        }
174 74
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 68
    protected function addRowToWriter(Row $row)
180
    {
181 68
        $this->throwIfWorkbookIsNotAvailable();
182 68
        $this->workbookManager->addRowToCurrentWorksheet($row);
183 66
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 73
    protected function closeWriter()
189
    {
190 73
        if ($this->workbookManager) {
191 73
            $this->workbookManager->close($this->filePointer);
192
        }
193 73
    }
194
}
195