Completed
Pull Request — develop (#411)
by Franck
06:21
created

DocPropsCustom::render()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 69
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 42
cts 42
cp 1
rs 6.5437
c 0
b 0
f 0
cc 8
eloc 44
nc 12
nop 0
crap 8

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\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\Common\XMLWriter;
6
use PhpOffice\PhpPresentation\DocumentProperties;
7
8
class DocPropsCustom extends AbstractDecoratorWriter
9
{
10
    /**
11
     * @return \PhpOffice\Common\Adapter\Zip\ZipInterface
12
     */
13 122
    public function render()
14
    {
15
        // Variables
16 122
        $pId = 0;
17
18
        // Create XML writer
19 122
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
20
21
        // XML header
22 122
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
23
24
        // Properties
25 122
        $objWriter->startElement('Properties');
26 122
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties');
27 122
        $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
28
29 122
        if ($this->getPresentation()->getPresentationProperties()->isMarkedAsFinal()) {
30
            // property
31 2
            $objWriter->startElement('property');
32 2
            $objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
33 2
            $objWriter->writeAttribute('pid', (++$pId) * 2);
34 2
            $objWriter->writeAttribute('name', '_MarkAsFinal');
35
36
            // property > vt:bool
37 2
            $objWriter->writeElement('vt:bool', 'true');
38
39
            // > property
40 2
            $objWriter->endElement();
41
        }
42
43 122
        $oDocumentProperties = $this->oPresentation->getDocumentProperties();
44 122
        foreach ($oDocumentProperties->getCustomProperties() as $customProperty) {
45 7
            $propertyValue = $oDocumentProperties->getCustomPropertyValue($customProperty);
46 7
            $propertyType = $oDocumentProperties->getCustomPropertyType($customProperty);
47
48 7
            $objWriter->startElement('property');
49 7
            $objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
50 7
            $objWriter->writeAttribute('pid', (++$pId) * 2);
51 7
            $objWriter->writeAttribute('name', $customProperty);
52
            switch ($propertyType) {
53 7
                case DocumentProperties::PROPERTY_TYPE_INTEGER:
54 1
                    $objWriter->writeElement('vt:i4', $propertyValue);
55 1
                    break;
56 6
                case DocumentProperties::PROPERTY_TYPE_FLOAT:
57 1
                    $objWriter->writeElement('vt:r8', $propertyValue);
58 1
                    break;
59 5
                case DocumentProperties::PROPERTY_TYPE_BOOLEAN:
60 1
                    $objWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false');
61 1
                    break;
62 4
                case DocumentProperties::PROPERTY_TYPE_DATE:
63 1
                    $objWriter->startElement('vt:filetime');
64 1
                    $objWriter->writeRaw(date(DATE_W3C, $propertyValue));
65 1
                    $objWriter->endElement();
66 1
                    break;
67
                default:
68 3
                    $objWriter->writeElement('vt:lpwstr', $propertyValue);
69 3
                    break;
70
            }
71 7
            $objWriter->endElement();
72
        }
73
74
        // > Properties
75 122
        $objWriter->endElement();
76
77 122
        $this->oZip->addFromString('docProps/custom.xml', $objWriter->getData());
78
79
        // Return
80 122
        return $this->oZip;
81
    }
82
}
83