Completed
Pull Request — develop (#208)
by Franck
08:10
created

Pictures   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 8
dl 0
loc 59
ccs 42
cts 42
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C render() 0 52 10
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\ODPresentation;
4
5
use PhpOffice\Common\Adapter\Zip\ZipInterface;
6
use PhpOffice\PhpPresentation\Shape\Drawing;
7
use PhpOffice\PhpPresentation\Shape\MemoryDrawing;
8
use PhpOffice\PhpPresentation\Slide\Background\Image;
9
10
class Pictures extends AbstractDecoratorWriter
11
{
12
    /**
13
     * @return ZipInterface
14
     */
15
16 57
    public function render()
17
    {
18 57
        $arrMedia = array();
19 57
        for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
20 28
            $shape = $this->getDrawingHashTable()->getByIndex($i);
21 28
            if ($shape instanceof Drawing) {
22 7
                if (!in_array(md5($shape->getPath()), $arrMedia)) {
23 7
                    $arrMedia[] = md5($shape->getPath());
24
25 7
                    $imagePath = $shape->getPath();
26
27 7
                    $imageContents = file_get_contents($imagePath);
28 7
                    if (strpos($imagePath, 'zip://') !== false) {
29 1
                        $imagePath = substr($imagePath, 6);
30 1
                        $imagePathSplitted = explode('#', $imagePath);
31
32 1
                        $imageZip = new \ZipArchive();
33 1
                        $imageZip->open($imagePathSplitted[0]);
34 1
                        $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
35 1
                        $imageZip->close();
36 1
                        unset($imageZip);
37 7
                    } elseif (strpos($imagePath, 'data:image/') === 0) {
38 1
                        list(, $imageContents) = explode(';', $imagePath);
39 1
                        list(, $imageContents) = explode(',', $imageContents);
40 1
                        $imageContents = base64_decode($imageContents);
41 1
                    }
42
43 7
                    $this->getZip()->addFromString('Pictures/' . md5($shape->getPath()).'.'.$shape->getExtension(), $imageContents);
44 7
                }
45 28
            } elseif ($shape instanceof MemoryDrawing) {
46 1
                if (!in_array(str_replace(' ', '_', $shape->getIndexedFilename()), $arrMedia)) {
47 1
                    $arrMedia[] = str_replace(' ', '_', $shape->getIndexedFilename());
48 1
                    ob_start();
49 1
                    call_user_func($shape->getRenderingFunction(), $shape->getImageResource());
50 1
                    $imageContents = ob_get_contents();
51 1
                    ob_end_clean();
52
53 1
                    $this->getZip()->addFromString('Pictures/' . str_replace(' ', '_', $shape->getIndexedFilename()), $imageContents);
54 1
                }
55 1
            }
56 28
        }
57
58 57
        foreach ($this->getPresentation()->getAllSlides() as $keySlide => $oSlide) {
59
            // Add background image slide
60 57
            $oBkgImage = $oSlide->getBackground();
61 57
            if ($oBkgImage instanceof Image) {
62 1
                $this->getZip()->addFromString('Pictures/'.$oBkgImage->getIndexedFilename($keySlide), file_get_contents($oBkgImage->getPath()));
63 1
            }
64 57
        }
65
        
66 57
        return $this->getZip();
67
    }
68
}
69