Completed
Pull Request — develop (#178)
by
unknown
09:34
created

Manifest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 83.33%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
lcom 1
cbo 10
dl 0
loc 131
ccs 70
cts 84
cp 0.8333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
D writePart() 0 95 10
A getImageMimeType() 0 19 3
1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation\Writer\ODPresentation;
19
20
use PhpOffice\Common\File;
21
use PhpOffice\PhpPresentation\PhpPresentation;
22
use PhpOffice\PhpPresentation\Shape\Drawing as ShapeDrawing;
23
use PhpOffice\PhpPresentation\Shape\MemoryDrawing;
24
use PhpOffice\PhpPresentation\Slide\Background\Image;
25
use PhpOffice\PhpPresentation\Writer\ODPresentation;
26
27
/**
28
 * \PhpOffice\PhpPresentation\Writer\ODPresentation\Manifest
29
 */
30
class Manifest extends AbstractPart
31
{
32
    /**
33
     * Write Manifest file to XML format
34
     *
35
     * @return string        XML Output
36
     * @throws \Exception
37
     */
38 42
    public function writePart()
39
    {
40 42
        $parentWriter = $this->getParentWriter();
41 42
        if (!$parentWriter instanceof ODPresentation) {
42 1
            throw new \Exception('The $parentWriter is not an instance of \PhpOffice\PhpPresentation\Writer\ODPresentation');
43
        }
44
        
45
        // Create XML writer
46 41
        $objWriter = $this->getXMLWriter();
47
48
        // XML header
49 41
        $objWriter->startDocument('1.0', 'UTF-8');
50
51
        // manifest:manifest
52 41
        $objWriter->startElement('manifest:manifest');
53 41
        $objWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0');
54 41
        $objWriter->writeAttribute('manifest:version', '1.2');
55
56
        // manifest:file-entry
57 41
        $objWriter->startElement('manifest:file-entry');
58 41
        $objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.presentation');
59 41
        $objWriter->writeAttribute('manifest:version', '1.2');
60 41
        $objWriter->writeAttribute('manifest:full-path', '/');
61 41
        $objWriter->endElement();
62
        // manifest:file-entry
63 41
        $objWriter->startElement('manifest:file-entry');
64 41
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
65 41
        $objWriter->writeAttribute('manifest:full-path', 'content.xml');
66 41
        $objWriter->endElement();
67
        // manifest:file-entry
68 41
        $objWriter->startElement('manifest:file-entry');
69 41
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
70 41
        $objWriter->writeAttribute('manifest:full-path', 'meta.xml');
71 41
        $objWriter->endElement();
72
        // manifest:file-entry
73 41
        $objWriter->startElement('manifest:file-entry');
74 41
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
75 41
        $objWriter->writeAttribute('manifest:full-path', 'styles.xml');
76 41
        $objWriter->endElement();
77
        
78
        // Charts
79 41
        foreach ($parentWriter->chartArray as $key => $shape) {
80 14
            $objWriter->startElement('manifest:file-entry');
81 14
            $objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/');
82 14
            $objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.chart');
83 14
            $objWriter->endElement();
84 14
            $objWriter->startElement('manifest:file-entry');
85 14
            $objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/content.xml');
86 14
            $objWriter->writeAttribute('manifest:media-type', 'text/xml');
87 14
            $objWriter->endElement();
88 41
        }
89
90 41
        $arrMedia = array();
91 41
        for ($i = 0; $i < $parentWriter->getDrawingHashTable()->count(); ++$i) {
92 18
            $shape = $parentWriter->getDrawingHashTable()->getByIndex($i);
93 18
            if ($shape instanceof ShapeDrawing) {
94 4
                if (!in_array(md5($shape->getPath()), $arrMedia)) {
95 4
                    $arrMedia[] = md5($shape->getPath());
96 4
                    $mimeType   = $this->getImageMimeType($shape->getPath());
97
98 4
                    $objWriter->startElement('manifest:file-entry');
99 4
                    $objWriter->writeAttribute('manifest:media-type', $mimeType);
100 4
                    $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . md5($shape->getPath()) . '.' . $shape->getExtension());
101 4
                    $objWriter->endElement();
102 4
                }
103 18
            } elseif ($shape instanceof MemoryDrawing) {
104 1
                if (!in_array(str_replace(' ', '_', $shape->getIndexedFilename()), $arrMedia)) {
105 1
                    $arrMedia[] = str_replace(' ', '_', $shape->getIndexedFilename());
106 1
                    $mimeType = $shape->getMimeType();
107
108 1
                    $objWriter->startElement('manifest:file-entry');
109 1
                    $objWriter->writeAttribute('manifest:media-type', $mimeType);
110 1
                    $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . str_replace(' ', '_', $shape->getIndexedFilename()));
111 1
                    $objWriter->endElement();
112 1
                }
113 1
            }
114 18
        }
115
116 41
        foreach ($parentWriter->getPhpPresentation()->getAllSlides() as $numSlide => $oSlide) {
117 41
            $oBkgImage = $oSlide->getBackground();
118 41
            if ($oBkgImage instanceof Image) {
119
                $mimeType   = $this->getImageMimeType($oBkgImage->getPath());
120
121
                $objWriter->startElement('manifest:file-entry');
122
                $objWriter->writeAttribute('manifest:media-type', $mimeType);
123
                $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . str_replace(' ', '_', $oBkgImage->getIndexedFilename($numSlide)));
124
                $objWriter->endElement();
125
            }
126 41
        }
127
128 41
        $objWriter->endElement();
129
130
        // Return
131 41
        return $objWriter->getData();
132
    }
133
134
    /**
135
     * Get image mime type
136
     *
137
     * @param  string    $pFile Filename
138
     * @return string    Mime Type
139
     * @throws \Exception
140
     */
141 4
    private function getImageMimeType($pFile = '')
142
    {
143 4
        if (File::fileExists($pFile)) {
144 4
            if (strpos($pFile, 'zip://') === 0) {
145
                $pZIPFile = str_replace('zip://', '', $pFile);
146
                $pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
147
                $pImgFile = substr($pFile, strpos($pFile, '#') + 1);
148
                $oArchive = new \ZipArchive();
149
                $oArchive->open($pZIPFile);
150
                $image = getimagesizefromstring($oArchive->getFromName($pImgFile));
151
            } else {
152 4
                $image = getimagesize($pFile);
153
            }
154
155 4
            return image_type_to_mime_type($image[2]);
156
        } else {
157
            throw new \Exception("File $pFile does not exist");
158
        }
159
    }
160
}
161