Completed
Pull Request — develop (#197)
by Franck
08:11
created

Pictures::render()   C

Complexity

Conditions 10
Paths 24

Size

Total Lines 53
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
ccs 42
cts 42
cp 1
rs 6.5333
cc 10
eloc 36
nc 24
nop 0
crap 10

How to fix   Long Method    Complexity   

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\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
                    if (strpos($imagePath, 'zip://') !== false) {
28 1
                        $imagePath = substr($imagePath, 6);
29 1
                        $imagePathSplitted = explode('#', $imagePath);
30
31 1
                        $imageZip = new \ZipArchive();
32 1
                        $imageZip->open($imagePathSplitted[0]);
33 1
                        $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
34 1
                        $imageZip->close();
35 1
                        unset($imageZip);
36 7
                    } elseif (strpos($imagePath, 'data:image/') === 0) {
37 1
                        list(, $imageContents) = explode(';', $imagePath);
38 1
                        list(, $imageContents) = explode(',', $imageContents);
39 1
                        $imageContents = base64_decode($imageContents);
40 1
                    } else {
41 5
                        $imageContents = file_get_contents($imagePath);
42
                    }
43
44 7
                    $this->getZip()->addFromString('Pictures/' . md5($shape->getPath()).'.'.$shape->getExtension(), $imageContents);
45 7
                }
46 28
            } elseif ($shape instanceof MemoryDrawing) {
47 1
                if (!in_array(str_replace(' ', '_', $shape->getIndexedFilename()), $arrMedia)) {
48 1
                    $arrMedia[] = str_replace(' ', '_', $shape->getIndexedFilename());
49 1
                    ob_start();
50 1
                    call_user_func($shape->getRenderingFunction(), $shape->getImageResource());
51 1
                    $imageContents = ob_get_contents();
52 1
                    ob_end_clean();
53
54 1
                    $this->getZip()->addFromString('Pictures/' . str_replace(' ', '_', $shape->getIndexedFilename()), $imageContents);
55 1
                }
56 1
            }
57 28
        }
58
59 57
        foreach ($this->getPresentation()->getAllSlides() as $keySlide => $oSlide) {
60
            // Add background image slide
61 57
            $oBkgImage = $oSlide->getBackground();
62 57
            if ($oBkgImage instanceof Image) {
63 1
                $this->getZip()->addFromString('Pictures/'.$oBkgImage->getIndexedFilename($keySlide), file_get_contents($oBkgImage->getPath()));
64 1
            }
65 57
        }
66
        
67 57
        return $this->getZip();
68
    }
69
}
70