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 ( 3e189a...03d551 )
by Mewes
15:04
created

SheetWrapper::getObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 118
    public function __construct(array $context, \Twig_Environment $environment, DocumentWrapper $documentWrapper)
50
    {
51 118
        parent::__construct($context, $environment);
52
53 118
        $this->documentWrapper = $documentWrapper;
54
55 118
        $this->object = null;
56 118
        $this->row = null;
57 118
        $this->column = null;
58 118
    }
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 106
    public function start($index, array $properties = [])
69
    {
70 106
        if ($this->documentWrapper->getObject() === null) {
71
            throw new \LogicException();
72
        }
73
74 106
        if (\is_int($index) && $index < $this->documentWrapper->getObject()->getSheetCount()) {
75
            $this->object = $this->documentWrapper->getObject()->setActiveSheetIndex($index);
76 106
        } elseif (\is_string($index)) {
77 101
            if (!$this->documentWrapper->getObject()->sheetNameExists($index)) {
78
                // create new sheet with a name
79 101
                $this->documentWrapper->getObject()->createSheet()->setTitle($index);
80
            }
81 101
            $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 106
        $this->parameters['index'] = $index;
89 106
        $this->parameters['properties'] = $properties;
90
91 106
        $this->setProperties($properties);
92 106
    }
93
94
    /**
95
     * @throws \Exception
96
     * @throws \LogicException
97
     */
98 98
    public function end()
99
    {
100 98
        if ($this->object === null) {
101
            throw new \LogicException();
102
        }
103
104
        // auto-size columns
105
        if (
106 98
            isset($this->parameters['properties']['columnDimension']) &&
107 98
            \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 98
        $this->parameters = [];
134 98
        $this->object = null;
135 98
        $this->row = null;
136 98
        $this->column = null;
137 98
    }
138
139 83
    public function increaseRow()
140
    {
141 83
        $this->row = $this->row === null ? self::ROW_DEFAULT : $this->row + 1;
142 83
    }
143
144 79
    public function increaseColumn()
145
    {
146 79
        $this->column = $this->column === null ? self::COLUMN_DEFAULT : $this->column + 1;
147 79
    }
148
149
    /**
150
     * @return Worksheet
151
     */
152 93
    public function getObject(): Worksheet
153
    {
154 93
        return $this->object;
155
    }
156
157
    /**
158
     * @param Worksheet $object
159
     */
160
    public function setObject(Worksheet $object)
161
    {
162
        $this->object = $object;
163
    }
164
165
    /**
166
     * @return int|null
167
     */
168 79
    public function getRow()
169
    {
170 79
        return $this->row;
171
    }
172
173
    /**
174
     * @param int|null $row
175
     */
176 3
    public function setRow($row)
177
    {
178 3
        $this->row = $row;
179 3
    }
180
181
    /**
182
     * @return int|null
183
     */
184 79
    public function getColumn()
185
    {
186 79
        return $this->column;
187
    }
188
189
    /**
190
     * @param int|null $column
191
     */
192 79
    public function setColumn($column)
193
    {
194 79
        $this->column = $column;
195 79
    }
196
197
    /**
198
     * {@inheritdoc}
199
     *
200
     * @throws \PhpOffice\PhpSpreadsheet\Exception
201
     */
202
    protected function configureMappings(): array
203
    {
204
        return [
205
            'autoFilter' => function ($value) { $this->object->setAutoFilter($value); },
206
            'columnDimension' => [
207 118
                '__multi' => function ($index = 'default'): ColumnDimension {
208 6
                    return $index === 'default' ?
209 6
                        $this->object->getDefaultColumnDimension() :
210 6
                        $this->object->getColumnDimension($index);
211 118
                },
212
                'autoSize' => function ($value, ColumnDimension $object) { $object->setAutoSize($value); },
213
                'collapsed' => function ($value, ColumnDimension $object) { $object->setCollapsed($value); },
214
                'columnIndex' => function ($value, ColumnDimension $object) { $object->setColumnIndex($value); },
215
                'outlineLevel' => function ($value, ColumnDimension $object) { $object->setOutlineLevel($value); },
216
                'visible' => function ($value, ColumnDimension $object) { $object->setVisible($value); },
217
                'width' => function ($value, ColumnDimension $object) { $object->setWidth($value); },
218
                'xfIndex' => function ($value, ColumnDimension $object) { $object->setXfIndex($value); },
219
            ],
220
            'pageMargins' => [
221
                'top' => function ($value) { $this->object->getPageMargins()->setTop($value); },
222
                'bottom' => function ($value) { $this->object->getPageMargins()->setBottom($value); },
223
                'left' => function ($value) { $this->object->getPageMargins()->setLeft($value); },
224
                'right' => function ($value) { $this->object->getPageMargins()->setRight($value); },
225
                'header' => function ($value) { $this->object->getPageMargins()->setHeader($value); },
226
                'footer' => function ($value) { $this->object->getPageMargins()->setFooter($value); },
227
            ],
228
            'pageSetup' => [
229
                'fitToHeight' => function ($value) { $this->object->getPageSetup()->setFitToHeight($value); },
230
                'fitToPage' => function ($value) { $this->object->getPageSetup()->setFitToPage($value); },
231
                'fitToWidth' => function ($value) { $this->object->getPageSetup()->setFitToWidth($value); },
232
                'horizontalCentered' => function ($value) { $this->object->getPageSetup()->setHorizontalCentered($value); },
233
                'orientation' => function ($value) { $this->object->getPageSetup()->setOrientation($value); },
234
                'paperSize' => function ($value) { $this->object->getPageSetup()->setPaperSize($value); },
235
                'printArea' => function ($value) { $this->object->getPageSetup()->setPrintArea($value); },
236
                'scale' => function ($value) { $this->object->getPageSetup()->setScale($value); },
237
                'verticalCentered' => function ($value) { $this->object->getPageSetup()->setVerticalCentered($value); },
238
            ],
239
            'printGridlines' => function ($value) { $this->object->setPrintGridlines($value); },
240
            'protection' => [
241
                'autoFilter' => function ($value) { $this->object->getProtection()->setAutoFilter($value); },
242
                'deleteColumns' => function ($value) { $this->object->getProtection()->setDeleteColumns($value); },
243
                'deleteRows' => function ($value) { $this->object->getProtection()->setDeleteRows($value); },
244
                'formatCells' => function ($value) { $this->object->getProtection()->setFormatCells($value); },
245
                'formatColumns' => function ($value) { $this->object->getProtection()->setFormatColumns($value); },
246
                'formatRows' => function ($value) { $this->object->getProtection()->setFormatRows($value); },
247
                'insertColumns' => function ($value) { $this->object->getProtection()->setInsertColumns($value); },
248
                'insertHyperlinks' => function ($value) { $this->object->getProtection()->setInsertHyperlinks($value); },
249
                'insertRows' => function ($value) { $this->object->getProtection()->setInsertRows($value); },
250
                'objects' => function ($value) { $this->object->getProtection()->setObjects($value); },
251
                'password' => function ($value) { $this->object->getProtection()->setPassword($value); },
252
                'pivotTables' => function ($value) { $this->object->getProtection()->setPivotTables($value); },
253
                'scenarios' => function ($value) { $this->object->getProtection()->setScenarios($value); },
254
                'selectLockedCells' => function ($value) { $this->object->getProtection()->setSelectLockedCells($value); },
255
                'selectUnlockedCells' => function ($value) { $this->object->getProtection()->setSelectUnlockedCells($value); },
256
                'sheet' => function ($value) { $this->object->getProtection()->setSheet($value); },
257
                'sort' => function ($value) { $this->object->getProtection()->setSort($value); },
258
            ],
259
            'rightToLeft' => function ($value) { $this->object->setRightToLeft($value); },
260
            'rowDimension' => [
261 118
                '__multi' => function ($index = 'default'): RowDimension {
262 6
                    return $index === 'default' ?
263 6
                        $this->object->getDefaultRowDimension() :
264 6
                        $this->object->getRowDimension($index);
265 118
                },
266
                'collapsed' => function ($value, RowDimension $object) { $object->setCollapsed($value); },
267
                'outlineLevel' => function ($value, RowDimension $object) { $object->setOutlineLevel($value); },
268
                'rowHeight' => function ($value, RowDimension $object) { $object->setRowHeight($value); },
269
                'rowIndex' => function ($value, RowDimension $object) { $object->setRowIndex($value); },
270
                'visible' => function ($value, RowDimension $object) { $object->setVisible($value); },
271
                'xfIndex' => function ($value, RowDimension $object) { $object->setXfIndex($value); },
272
                'zeroHeight' => function ($value, RowDimension $object) { $object->setZeroHeight($value); },
273
            ],
274
            'sheetState' => function ($value) { $this->object->setSheetState($value); },
275
            'showGridlines' => function ($value) { $this->object->setShowGridlines($value); },
276
            'tabColor' => function ($value) { $this->object->getTabColor()->setRGB($value); },
277
            'zoomScale' => function ($value) { $this->object->getSheetView()->setZoomScale($value); },
278
        ];
279
    }
280
}
281