Failed Conditions
Pull Request — master (#738)
by
unknown
02:18
created

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