Failed Conditions
Push — master ( 4b6ad7...9fab89 )
by Adrien
06:57
created

Rels::writeWorkbookRelationships()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 66
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 4.0004

Importance

Changes 0
Metric Value
cc 4
eloc 36
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 66
ccs 33
cts 34
cp 0.9706
crap 4.0004
rs 9.344

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
8
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
9
10
class Rels extends WriterPart
11
{
12
    /**
13
     * Write relationships to XML format.
14
     *
15
     * @param Spreadsheet $spreadsheet
16
     *
17
     * @throws WriterException
18
     *
19
     * @return string XML Output
20
     */
21 100
    public function writeRelationships(Spreadsheet $spreadsheet)
22
    {
23
        // Create XML writer
24 100
        $objWriter = null;
25 100
        if ($this->getParentWriter()->getUseDiskCaching()) {
26
            $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
27
        } else {
28 100
            $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
29
        }
30
31
        // XML header
32 100
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
33
34
        // Relationships
35 100
        $objWriter->startElement('Relationships');
36 100
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
37
38 100
        $customPropertyList = $spreadsheet->getProperties()->getCustomProperties();
39 100
        if (!empty($customPropertyList)) {
40
            // Relationship docProps/app.xml
41 2
            $this->writeRelationship(
42
                $objWriter,
43 2
                4,
44 2
                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties',
45 2
                'docProps/custom.xml'
46
            );
47
        }
48
49
        // Relationship docProps/app.xml
50 100
        $this->writeRelationship(
51
            $objWriter,
52 100
            3,
53 100
            'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
54 100
            'docProps/app.xml'
55
        );
56
57
        // Relationship docProps/core.xml
58 100
        $this->writeRelationship(
59
            $objWriter,
60 100
            2,
61 100
            'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
62 100
            'docProps/core.xml'
63
        );
64
65
        // Relationship xl/workbook.xml
66 100
        $this->writeRelationship(
67
            $objWriter,
68 100
            1,
69 100
            'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
70 100
            'xl/workbook.xml'
71
        );
72
        // a custom UI in workbook ?
73 100
        if ($spreadsheet->hasRibbon()) {
74
            $this->writeRelationShip(
75
                $objWriter,
76
                5,
77
                'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility',
78
                $spreadsheet->getRibbonXMLData('target')
79
            );
80
        }
81
82 100
        $objWriter->endElement();
83
84 100
        return $objWriter->getData();
85
    }
86
87
    /**
88
     * Write workbook relationships to XML format.
89
     *
90
     * @param Spreadsheet $spreadsheet
91
     *
92
     * @throws WriterException
93
     *
94
     * @return string XML Output
95
     */
96 100
    public function writeWorkbookRelationships(Spreadsheet $spreadsheet)
97
    {
98
        // Create XML writer
99 100
        $objWriter = null;
100 100
        if ($this->getParentWriter()->getUseDiskCaching()) {
101
            $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
102
        } else {
103 100
            $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
104
        }
105
106
        // XML header
107 100
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
108
109
        // Relationships
110 100
        $objWriter->startElement('Relationships');
111 100
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
112
113
        // Relationship styles.xml
114 100
        $this->writeRelationship(
115
            $objWriter,
116 100
            1,
117 100
            'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
118 100
            'styles.xml'
119
        );
120
121
        // Relationship theme/theme1.xml
122 100
        $this->writeRelationship(
123
            $objWriter,
124 100
            2,
125 100
            'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
126 100
            'theme/theme1.xml'
127
        );
128
129
        // Relationship sharedStrings.xml
130 100
        $this->writeRelationship(
131
            $objWriter,
132 100
            3,
133 100
            'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
134 100
            'sharedStrings.xml'
135
        );
136
137
        // Relationships with sheets
138 100
        $sheetCount = $spreadsheet->getSheetCount();
139 100
        for ($i = 0; $i < $sheetCount; ++$i) {
140 100
            $this->writeRelationship(
141
                $objWriter,
142 100
                ($i + 1 + 3),
143 100
                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
144 100
                'worksheets/sheet' . ($i + 1) . '.xml'
145
            );
146
        }
147
        // Relationships for vbaProject if needed
148
        // id : just after the last sheet
149 100
        if ($spreadsheet->hasMacros()) {
150 1
            $this->writeRelationShip(
151
                $objWriter,
152 1
                ($i + 1 + 3),
153 1
                'http://schemas.microsoft.com/office/2006/relationships/vbaProject',
154 1
                'vbaProject.bin'
155
            );
156 1
            ++$i; //increment i if needed for an another relation
157
        }
158
159 100
        $objWriter->endElement();
160
161 100
        return $objWriter->getData();
162
    }
163
164
    /**
165
     * Write worksheet relationships to XML format.
166
     *
167
     * Numbering is as follows:
168
     *     rId1                 - Drawings
169
     *  rId_hyperlink_x     - Hyperlinks
170
     *
171
     * @param \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet
172
     * @param int $pWorksheetId
173
     * @param bool $includeCharts Flag indicating if we should write charts
174
     *
175
     * @throws WriterException
176
     *
177
     * @return string XML Output
178
     */
179 100
    public function writeWorksheetRelationships(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet, $pWorksheetId = 1, $includeCharts = false)
180
    {
181
        // Create XML writer
182 100
        $objWriter = null;
183 100
        if ($this->getParentWriter()->getUseDiskCaching()) {
184
            $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
185
        } else {
186 100
            $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
187
        }
188
189
        // XML header
190 100
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
191
192
        // Relationships
193 100
        $objWriter->startElement('Relationships');
194 100
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
195
196
        // Write drawing relationships?
197 100
        $d = 0;
198 100
        $drawingOriginalIds = [];
199 100
        $unparsedLoadedData = $pWorksheet->getParent()->getUnparsedLoadedData();
200 100
        if (isset($unparsedLoadedData['sheets'][$pWorksheet->getCodeName()]['drawingOriginalIds'])) {
201 4
            $drawingOriginalIds = $unparsedLoadedData['sheets'][$pWorksheet->getCodeName()]['drawingOriginalIds'];
202
        }
203
204 100
        if ($includeCharts) {
205 15
            $charts = $pWorksheet->getChartCollection();
206
        } else {
207 86
            $charts = [];
208
        }
209
210 100
        if (($pWorksheet->getDrawingCollection()->count() > 0) || (count($charts) > 0) || $drawingOriginalIds) {
211 27
            $relPath = '../drawings/drawing' . $pWorksheetId . '.xml';
212 27
            $rId = ++$d;
213
214 27
            if (isset($drawingOriginalIds[$relPath])) {
215 4
                $rId = (int) (substr($drawingOriginalIds[$relPath], 3));
216
            }
217
218 27
            $this->writeRelationship(
219
                $objWriter,
220
                $rId,
221 27
                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
222 27
                $relPath
223
            );
224
        }
225
226
        // Write hyperlink relationships?
227 100
        $i = 1;
228 100
        foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
229 10
            if (!$hyperlink->isInternal()) {
230 10
                $this->writeRelationship(
231
                    $objWriter,
232 10
                    '_hyperlink_' . $i,
0 ignored issues
show
Bug introduced by
'_hyperlink_' . $i of type string is incompatible with the type integer expected by parameter $pId of PhpOffice\PhpSpreadsheet...ls::writeRelationship(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

232
                    /** @scrutinizer ignore-type */ '_hyperlink_' . $i,
Loading history...
233 10
                    'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
234 10
                    $hyperlink->getUrl(),
235 10
                    'External'
236
                );
237
238 10
                ++$i;
239
            }
240
        }
241
242
        // Write comments relationship?
243 100
        $i = 1;
244 100
        if (count($pWorksheet->getComments()) > 0) {
245 10
            $this->writeRelationship(
246
                $objWriter,
247 10
                '_comments_vml' . $i,
248 10
                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
249 10
                '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
250
            );
251
252 10
            $this->writeRelationship(
253
                $objWriter,
254 10
                '_comments' . $i,
255 10
                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
256 10
                '../comments' . $pWorksheetId . '.xml'
257
            );
258
        }
259
260
        // Write header/footer relationship?
261 100
        $i = 1;
262 100
        if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
263 1
            $this->writeRelationship(
264
                $objWriter,
265 1
                '_headerfooter_vml' . $i,
266 1
                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
267 1
                '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
268
            );
269
        }
270
271 100
        $this->writeUnparsedRelationship($pWorksheet, $objWriter, 'ctrlProps', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/ctrlProp');
272 100
        $this->writeUnparsedRelationship($pWorksheet, $objWriter, 'vmlDrawings', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing');
273 100
        $this->writeUnparsedRelationship($pWorksheet, $objWriter, 'printerSettings', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings');
274
275 100
        $objWriter->endElement();
276
277 100
        return $objWriter->getData();
278
    }
279
280 100
    private function writeUnparsedRelationship(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet, XMLWriter $objWriter, $relationship, $type)
281
    {
282 100
        $unparsedLoadedData = $pWorksheet->getParent()->getUnparsedLoadedData();
283 100
        if (!isset($unparsedLoadedData['sheets'][$pWorksheet->getCodeName()][$relationship])) {
284 100
            return;
285
        }
286
287 4
        foreach ($unparsedLoadedData['sheets'][$pWorksheet->getCodeName()][$relationship] as $rId => $value) {
288 4
            $this->writeRelationship(
289
                $objWriter,
290
                $rId,
291
                $type,
292 4
                $value['relFilePath']
293
            );
294
        }
295 4
    }
296
297
    /**
298
     * Write drawing relationships to XML format.
299
     *
300
     * @param \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet
301
     * @param int &$chartRef Chart ID
302
     * @param bool $includeCharts Flag indicating if we should write charts
303
     *
304
     * @throws WriterException
305
     *
306
     * @return string XML Output
307
     */
308 25
    public function writeDrawingRelationships(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet, &$chartRef, $includeCharts = false)
309
    {
310
        // Create XML writer
311 25
        $objWriter = null;
312 25
        if ($this->getParentWriter()->getUseDiskCaching()) {
313
            $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
314
        } else {
315 25
            $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
316
        }
317
318
        // XML header
319 25
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
320
321
        // Relationships
322 25
        $objWriter->startElement('Relationships');
323 25
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
324
325
        // Loop through images and write relationships
326 25
        $i = 1;
327 25
        $iterator = $pWorksheet->getDrawingCollection()->getIterator();
328 25
        while ($iterator->valid()) {
329 12
            if ($iterator->current() instanceof \PhpOffice\PhpSpreadsheet\Worksheet\Drawing
330 12
                || $iterator->current() instanceof MemoryDrawing) {
331
                // Write relationship for image drawing
332
                /** @var \PhpOffice\PhpSpreadsheet\Worksheet\Drawing $drawing */
333 12
                $drawing = $iterator->current();
334 12
                $this->writeRelationship(
335
                    $objWriter,
336
                    $i,
337 12
                    'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
338 12
                    '../media/' . str_replace(' ', '', $drawing->getIndexedFilename())
339
                );
340
341 12
                $i = $this->writeDrawingHyperLink($objWriter, $drawing, $i);
342
            }
343
344 12
            $iterator->next();
345 12
            ++$i;
346
        }
347
348 25
        if ($includeCharts) {
349
            // Loop through charts and write relationships
350 14
            $chartCount = $pWorksheet->getChartCount();
351 14
            if ($chartCount > 0) {
352 14
                for ($c = 0; $c < $chartCount; ++$c) {
353 14
                    $this->writeRelationship(
354
                        $objWriter,
355 14
                        $i++,
356 14
                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
357 14
                        '../charts/chart' . ++$chartRef . '.xml'
358
                    );
359
                }
360
            }
361
        }
362
363 25
        $objWriter->endElement();
364
365 25
        return $objWriter->getData();
366
    }
367
368
    /**
369
     * Write header/footer drawing relationships to XML format.
370
     *
371
     * @param \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet
372
     *
373
     * @throws WriterException
374
     *
375
     * @return string XML Output
376
     */
377 1
    public function writeHeaderFooterDrawingRelationships(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet)
378
    {
379
        // Create XML writer
380 1
        $objWriter = null;
381 1
        if ($this->getParentWriter()->getUseDiskCaching()) {
382
            $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
383
        } else {
384 1
            $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
385
        }
386
387
        // XML header
388 1
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
389
390
        // Relationships
391 1
        $objWriter->startElement('Relationships');
392 1
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
393
394
        // Loop through images and write relationships
395 1
        foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
396
            // Write relationship for image drawing
397 1
            $this->writeRelationship(
398
                $objWriter,
399
                $key,
400 1
                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
401 1
                '../media/' . $value->getIndexedFilename()
402
            );
403
        }
404
405 1
        $objWriter->endElement();
406
407 1
        return $objWriter->getData();
408
    }
409
410
    /**
411
     * Write Override content type.
412
     *
413
     * @param XMLWriter $objWriter XML Writer
414
     * @param int $pId Relationship ID. rId will be prepended!
415
     * @param string $pType Relationship type
416
     * @param string $pTarget Relationship target
417
     * @param string $pTargetMode Relationship target mode
418
     *
419
     * @throws WriterException
420
     */
421 100
    private function writeRelationship(XMLWriter $objWriter, $pId, $pType, $pTarget, $pTargetMode = '')
422
    {
423 100
        if ($pType != '' && $pTarget != '') {
424
            // Write relationship
425 100
            $objWriter->startElement('Relationship');
426 100
            $objWriter->writeAttribute('Id', 'rId' . $pId);
427 100
            $objWriter->writeAttribute('Type', $pType);
428 100
            $objWriter->writeAttribute('Target', $pTarget);
429
430 100
            if ($pTargetMode != '') {
431 12
                $objWriter->writeAttribute('TargetMode', $pTargetMode);
432
            }
433
434 100
            $objWriter->endElement();
435
        } else {
436
            throw new WriterException('Invalid parameters passed.');
437
        }
438 100
    }
439
440
    /**
441
     * @param $objWriter
442
     * @param \PhpOffice\PhpSpreadsheet\Worksheet\Drawing $drawing
443
     * @param $i
444
     *
445
     * @throws WriterException
446
     *
447
     * @return int
448
     */
449 12
    private function writeDrawingHyperLink($objWriter, $drawing, $i)
450
    {
451 12
        if ($drawing->getHyperlink() === null) {
452 10
            return $i;
453
        }
454
455 2
        ++$i;
456 2
        $this->writeRelationship(
457 2
            $objWriter,
458 2
            $i,
459
            'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
460 2
            $drawing->getHyperlink()->getUrl(),
461 2
            $drawing->getHyperlink()->getTypeHyperlink()
462
        );
463
464 2
        return $i;
465
    }
466
}
467