GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 1dd304...6c6f43 )
by Mewes
06:12
created

SheetWrapper::end()   B

Complexity

Conditions 9
Paths 3

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9.3188

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 16
cts 19
cp 0.8421
rs 7.7404
c 0
b 0
f 0
cc 9
nc 3
nop 0
crap 9.3188
1
<?php
2
3
namespace MewesK\TwigSpreadsheetBundle\Wrapper;
4
5
use PhpOffice\PhpSpreadsheet\Exception;
6
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
7
use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension;
8
use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension;
9
10
/**
11
 * Class SheetWrapper.
12
 */
13
class SheetWrapper extends BaseWrapper
14
{
15
    /**
16
     * @var int
17
     */
18
    const COLUMN_DEFAULT = 1;
19
    /**
20
     * @var int
21
     */
22
    const ROW_DEFAULT = 1;
23
24
    /**
25
     * @var DocumentWrapper
26
     */
27
    protected $documentWrapper;
28
29
    /**
30
     * @var Worksheet|null
31
     */
32
    protected $object;
33
    /**
34
     * @var null|int
35
     */
36
    protected $row;
37
    /**
38
     * @var null|int
39
     */
40
    protected $column;
41
42
    /**
43
     * SheetWrapper constructor.
44
     *
45
     * @param array             $context
46
     * @param \Twig_Environment $environment
47
     * @param DocumentWrapper   $documentWrapper
48
     */
49 112
    public function __construct(array $context, \Twig_Environment $environment, DocumentWrapper $documentWrapper)
50
    {
51 112
        parent::__construct($context, $environment);
52
53 112
        $this->documentWrapper = $documentWrapper;
54
55 112
        $this->object = null;
56 112
        $this->row = null;
57 112
        $this->column = null;
58 112
    }
59
60
    /**
61
     * @param int|string|null $index
62
     * @param array $properties
63
     *
64
     * @throws \PhpOffice\PhpSpreadsheet\Exception
65
     * @throws \RuntimeException
66
     * @throws \LogicException
67
     */
68 100
    public function start($index, array $properties = [])
69
    {
70 100
        if ($this->documentWrapper->getObject() === null) {
71
            throw new \LogicException();
72
        }
73
74 100
        if (\is_int($index) && $index < $this->documentWrapper->getObject()->getSheetCount()) {
75
            $this->object = $this->documentWrapper->getObject()->setActiveSheetIndex($index);
76 100
        } elseif (\is_string($index)) {
77 95
            if (!$this->documentWrapper->getObject()->sheetNameExists($index)) {
78
                // create new sheet with a name
79 95
                $this->documentWrapper->getObject()->createSheet()->setTitle($index);
80
            }
81 95
            $this->object = $this->documentWrapper->getObject()->setActiveSheetIndexByName($index);
82
        } else {
83
            // create new sheet without a name
84 5
            $this->documentWrapper->getObject()->createSheet();
85 5
            $this->object = $this->documentWrapper->getObject()->setActiveSheetIndex(0);
86
        }
87
88 100
        $this->parameters['index'] = $index;
89 100
        $this->parameters['properties'] = $properties;
90
91 100
        $this->setProperties($properties);
92 100
    }
93
94
    /**
95
     * @throws \Exception
96
     * @throws \LogicException
97
     */
98 92
    public function end()
99
    {
100 92
        if ($this->object === null) {
101
            throw new \LogicException();
102
        }
103
104
        // auto-size columns
105
        if (
106 92
            isset($this->parameters['properties']['columnDimension']) &&
107 92
            \is_array($this->parameters['properties']['columnDimension'])
108
        ) {
109
            /**
110
             * @var array $columnDimension
111
             */
112 6
            $columnDimension = $this->parameters['properties']['columnDimension'];
113 6
            foreach ($columnDimension as $key => $value) {
114 6
                if (isset($value['autoSize'])) {
115 6
                    if ($key === 'default') {
116
                        try {
117 6
                            $cellIterator = $this->object->getRowIterator()->current()->getCellIterator();
118 6
                            $cellIterator->setIterateOnlyExistingCells(true);
119
120
                            foreach ($cellIterator as $cell) {
121
                                $this->object->getColumnDimension($cell->getColumn())->setAutoSize($value['autoSize']);
122
                            }
123 6
                        } catch (Exception $e) {
124
                            // ignore exceptions thrown when no cells are defined
125
                        }
126
                    } else {
127 6
                        $this->object->getColumnDimension($key)->setAutoSize($value['autoSize']);
128
                    }
129
                }
130
            }
131
        }
132
133 92
        $this->object = null;
134 92
        $this->parameters = [];
135 92
        $this->row = null;
136 92
    }
137
138 77
    public function increaseRow()
139
    {
140 77
        $this->row = $this->row === null ? self::ROW_DEFAULT : $this->row + 1;
141 77
    }
142
143 73
    public function increaseColumn()
144
    {
145 73
        $this->column = $this->column === null ? self::COLUMN_DEFAULT : $this->column + 1;
146 73
    }
147
148
    /**
149
     * @return Worksheet
150
     */
151 87
    public function getObject(): Worksheet
152
    {
153 87
        return $this->object;
154
    }
155
156
    /**
157
     * @param Worksheet $object
158
     */
159
    public function setObject(Worksheet $object)
160
    {
161
        $this->object = $object;
162
    }
163
164
    /**
165
     * @return int|null
166
     */
167 73
    public function getRow()
168
    {
169 73
        return $this->row;
170
    }
171
172
    /**
173
     * @param int|null $row
174
     */
175 3
    public function setRow($row)
176
    {
177 3
        $this->row = $row;
178 3
    }
179
180
    /**
181
     * @return int|null
182
     */
183 73
    public function getColumn()
184
    {
185 73
        return $this->column;
186
    }
187
188
    /**
189
     * @param int|null $column
190
     */
191 73
    public function setColumn($column)
192
    {
193 73
        $this->column = $column;
194 73
    }
195
196
    /**
197
     * {@inheritdoc}
198
     *
199
     * @throws \PhpOffice\PhpSpreadsheet\Exception
200
     */
201
    protected function configureMappings(): array
202
    {
203
        return [
204
            'autoFilter' => function ($value) { $this->object->setAutoFilter($value); },
205
            'columnDimension' => [
206 112
                '__multi' => function ($column = 'default'): ColumnDimension {
207 6
                    return $column === 'default' ?
208 6
                        $this->object->getDefaultColumnDimension() :
209 6
                        $this->object->getColumnDimension($column);
210 112
                },
211
                'autoSize' => function ($value, ColumnDimension $object) { $object->setAutoSize($value); },
212
                'collapsed' => function ($value, ColumnDimension $object) { $object->setCollapsed($value); },
213
                'columnIndex' => function ($value, ColumnDimension $object) { $object->setColumnIndex($value); },
214
                'outlineLevel' => function ($value, ColumnDimension $object) { $object->setOutlineLevel($value); },
215
                'visible' => function ($value, ColumnDimension $object) { $object->setVisible($value); },
216
                'width' => function ($value, ColumnDimension $object) { $object->setWidth($value); },
217
                'xfIndex' => function ($value, ColumnDimension $object) { $object->setXfIndex($value); },
218
            ],
219
            'pageMargins' => [
220
                'top' => function ($value) { $this->object->getPageMargins()->setTop($value); },
221
                'bottom' => function ($value) { $this->object->getPageMargins()->setBottom($value); },
222
                'left' => function ($value) { $this->object->getPageMargins()->setLeft($value); },
223
                'right' => function ($value) { $this->object->getPageMargins()->setRight($value); },
224
                'header' => function ($value) { $this->object->getPageMargins()->setHeader($value); },
225
                'footer' => function ($value) { $this->object->getPageMargins()->setFooter($value); },
226
            ],
227
            'pageSetup' => [
228
                'fitToHeight' => function ($value) { $this->object->getPageSetup()->setFitToHeight($value); },
229
                'fitToPage' => function ($value) { $this->object->getPageSetup()->setFitToPage($value); },
230
                'fitToWidth' => function ($value) { $this->object->getPageSetup()->setFitToWidth($value); },
231
                'horizontalCentered' => function ($value) { $this->object->getPageSetup()->setHorizontalCentered($value); },
232
                'orientation' => function ($value) { $this->object->getPageSetup()->setOrientation($value); },
233
                'paperSize' => function ($value) { $this->object->getPageSetup()->setPaperSize($value); },
234
                'printArea' => function ($value) { $this->object->getPageSetup()->setPrintArea($value); },
235
                'scale' => function ($value) { $this->object->getPageSetup()->setScale($value); },
236
                'verticalCentered' => function ($value) { $this->object->getPageSetup()->setVerticalCentered($value); },
237
            ],
238
            'printGridlines' => function ($value) { $this->object->setPrintGridlines($value); },
239
            'protection' => [
240
                'autoFilter' => function ($value) { $this->object->getProtection()->setAutoFilter($value); },
241
                'deleteColumns' => function ($value) { $this->object->getProtection()->setDeleteColumns($value); },
242
                'deleteRows' => function ($value) { $this->object->getProtection()->setDeleteRows($value); },
243
                'formatCells' => function ($value) { $this->object->getProtection()->setFormatCells($value); },
244
                'formatColumns' => function ($value) { $this->object->getProtection()->setFormatColumns($value); },
245
                'formatRows' => function ($value) { $this->object->getProtection()->setFormatRows($value); },
246
                'insertColumns' => function ($value) { $this->object->getProtection()->setInsertColumns($value); },
247
                'insertHyperlinks' => function ($value) { $this->object->getProtection()->setInsertHyperlinks($value); },
248
                'insertRows' => function ($value) { $this->object->getProtection()->setInsertRows($value); },
249
                'objects' => function ($value) { $this->object->getProtection()->setObjects($value); },
250
                'password' => function ($value) { $this->object->getProtection()->setPassword($value); },
251
                'pivotTables' => function ($value) { $this->object->getProtection()->setPivotTables($value); },
252
                'scenarios' => function ($value) { $this->object->getProtection()->setScenarios($value); },
253
                'selectLockedCells' => function ($value) { $this->object->getProtection()->setSelectLockedCells($value); },
254
                'selectUnlockedCells' => function ($value) { $this->object->getProtection()->setSelectUnlockedCells($value); },
255
                'sheet' => function ($value) { $this->object->getProtection()->setSheet($value); },
256
                'sort' => function ($value) { $this->object->getProtection()->setSort($value); },
257
            ],
258
            'rightToLeft' => function ($value) { $this->object->setRightToLeft($value); },
259
            'rowDimension' => [
260 112
                '__multi' => function ($column = 'default'): RowDimension {
261 6
                    return $column === 'default' ?
262 6
                        $this->object->getDefaultRowDimension() :
263 6
                        $this->object->getRowDimension($column);
264 112
                },
265
                'collapsed' => function ($value, RowDimension $object) { $object->setCollapsed($value); },
266
                'outlineLevel' => function ($value, RowDimension $object) { $object->setOutlineLevel($value); },
267
                'rowHeight' => function ($value, RowDimension $object) { $object->setRowHeight($value); },
268
                'rowIndex' => function ($value, RowDimension $object) { $object->setRowIndex($value); },
269
                'visible' => function ($value, RowDimension $object) { $object->setVisible($value); },
270
                'xfIndex' => function ($value, RowDimension $object) { $object->setXfIndex($value); },
271
                'zeroHeight' => function ($value, RowDimension $object) { $object->setZeroHeight($value); },
272
            ],
273
            'sheetState' => function ($value) { $this->object->setSheetState($value); },
274
            'showGridlines' => function ($value) { $this->object->setShowGridlines($value); },
275
            'tabColor' => function ($value) { $this->object->getTabColor()->setRGB($value); },
276
            'zoomScale' => function ($value) { $this->object->getSheetView()->setZoomScale($value); },
277
        ];
278
    }
279
}
280