Completed
Pull Request — develop (#207)
by Franck
06:53
created

MetaInfManifest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 9
dl 0
loc 98
ccs 63
cts 63
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C render() 0 92 8
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\ODPresentation;
4
5
use PhpOffice\Common\Adapter\Zip\ZipInterface;
6
use PhpOffice\Common\File;
7
use PhpOffice\Common\XMLWriter;
8
use PhpOffice\PhpPresentation\Shape\Drawing as ShapeDrawing;
9
use PhpOffice\PhpPresentation\Slide\Background\Image;
10
use PhpOffice\PhpPresentation\Writer\ODPresentation;
11
12
class MetaInfManifest extends AbstractDecoratorWriter
13
{
14
    /**
15
     * @return ZipInterface
16
     */
17 59
    public function render()
18
    {
19
        // Create XML writer
20 59
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
21
22
        // XML header
23 59
        $objWriter->startDocument('1.0', 'UTF-8');
24
25
        // manifest:manifest
26 59
        $objWriter->startElement('manifest:manifest');
27 59
        $objWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0');
28 59
        $objWriter->writeAttribute('manifest:version', '1.2');
29
30
        // manifest:file-entry
31 59
        $objWriter->startElement('manifest:file-entry');
32 59
        $objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.presentation');
33 59
        $objWriter->writeAttribute('manifest:full-path', '/');
34 59
        $objWriter->writeAttribute('manifest:version', '1.2');
35 59
        $objWriter->endElement();
36
        // manifest:file-entry
37 59
        $objWriter->startElement('manifest:file-entry');
38 59
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
39 59
        $objWriter->writeAttribute('manifest:full-path', 'content.xml');
40 59
        $objWriter->endElement();
41
        // manifest:file-entry
42 59
        $objWriter->startElement('manifest:file-entry');
43 59
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
44 59
        $objWriter->writeAttribute('manifest:full-path', 'meta.xml');
45 59
        $objWriter->endElement();
46
        // manifest:file-entry
47 59
        $objWriter->startElement('manifest:file-entry');
48 59
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
49 59
        $objWriter->writeAttribute('manifest:full-path', 'styles.xml');
50 59
        $objWriter->endElement();
51
52
        // Charts
53 59
        foreach ($this->getArrayChart() as $key => $shape) {
54 21
            $objWriter->startElement('manifest:file-entry');
55 21
            $objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.chart');
56 21
            $objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/');
57 21
            $objWriter->endElement();
58 21
            $objWriter->startElement('manifest:file-entry');
59 21
            $objWriter->writeAttribute('manifest:media-type', 'text/xml');
60 21
            $objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/content.xml');
61 21
            $objWriter->endElement();
62
        }
63
64 59
        $arrMedia = array();
65 59
        for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
66 30
            $shape = $this->getDrawingHashTable()->getByIndex($i);
67 30
            if (! ($shape instanceof ShapeDrawing\AbstractDrawingAdapter)) {
68 21
                continue;
69
            }
70 10
            $arrMedia[] = $shape->getIndexedFilename();
71 10
            $objWriter->startElement('manifest:file-entry');
72 10
            $objWriter->writeAttribute('manifest:media-type', $shape->getMimeType());
73 8
            $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . $shape->getIndexedFilename());
74 8
            $objWriter->endElement();
75
        }
76
77 57
        foreach ($this->getPresentation()->getAllSlides() as $numSlide => $oSlide) {
78 57
            $oBkgImage = $oSlide->getBackground();
79 57
            if ($oBkgImage instanceof Image) {
80 1
                $arrayImage = getimagesize($oBkgImage->getPath());
81 1
                $mimeType  = image_type_to_mime_type($arrayImage[2]);
82
83 1
                $objWriter->startElement('manifest:file-entry');
84 1
                $objWriter->writeAttribute('manifest:media-type', $mimeType);
85 1
                $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . str_replace(' ', '_', $oBkgImage->getIndexedFilename($numSlide)));
86 57
                $objWriter->endElement();
87
            }
88
        }
89
90 57
        if ($this->getPresentation()->getPresentationProperties()->getThumbnailPath()) {
91 1
            $pathThumbnail = $this->getPresentation()->getPresentationProperties()->getThumbnailPath();
92
            // Size : 128x128 pixel
93
            // PNG : 8bit, non-interlaced with full alpha transparency
94 1
            $gdImage = imagecreatefromstring(file_get_contents($pathThumbnail));
95 1
            if ($gdImage) {
96 1
                imagedestroy($gdImage);
97 1
                $objWriter->startElement('manifest:file-entry');
98 1
                $objWriter->writeAttribute('manifest:media-type', 'image/png');
99 1
                $objWriter->writeAttribute('manifest:full-path', 'Thumbnails/thumbnail.png');
100 1
                $objWriter->endElement();
101
            }
102
        }
103
104 57
        $objWriter->endElement();
105
106 57
        $this->getZip()->addFromString('META-INF/manifest.xml', $objWriter->getData());
107 57
        return $this->getZip();
108
    }
109
}
110