Completed
Pull Request — develop (#216)
by Franck
07:08
created

Pictures::render()   C

Complexity

Conditions 10
Paths 24

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 10.0023

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 52
ccs 34
cts 35
cp 0.9714
rs 6.2553
cc 10
eloc 35
nc 24
nop 0
crap 10.0023

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
                    $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 6
                    } 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
                    }
42
43 7
                    $this->getZip()->addFromString('Pictures/' . md5($shape->getPath()).'.'.$shape->getExtension(), $imageContents);
44
                }
45
            } 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
                }
55
            }
56
        }
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 57
                $this->getZip()->addFromString('Pictures/'.$oBkgImage->getIndexedFilename($keySlide), file_get_contents($oBkgImage->getPath()));
63
            }
64
        }
65
        
66 57
        return $this->getZip();
67
    }
68
}
69