Completed
Pull Request — develop (#207)
by Franck
12:42 queued 05:23
created

ZipFile   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 91
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 4 1
A setPath() 0 5 1
A getContents() 0 12 1
A getExtension() 0 6 1
A getMimeType() 0 18 3
A getIndexedFilename() 0 9 1
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Shape\Drawing;
4
5
use PhpOffice\Common\File as CommonFile;
6
7
class ZipFile extends AbstractDrawingAdapter
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $path;
13
14
    /**
15
     * Get Path
16
     *
17
     * @return string
18
     */
19
    public function getPath()
20
    {
21
        return $this->path;
22
    }
23
24
    /**
25
     * Set Path
26
     *
27
     * @param  string                      $pValue      File path
28
     * @return \PhpOffice\PhpPresentation\Shape\Drawing
29
     */
30
    public function setPath($pValue = '')
31
    {
32
        $this->path = $pValue;
33
        return $this;
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function getContents()
40
    {
41
        $imagePath         = substr($this->getPath(), 6);
42
        $imagePathSplitted = explode('#', $imagePath);
43
44
        $imageZip = new \ZipArchive();
45
        $imageZip->open($imagePathSplitted[0]);
46
        $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
47
        $imageZip->close();
48
        unset($imageZip);
49
        return $imageContents;
50
    }
51
52
53
    /**
54
     * @return string
55
     */
56
    public function getExtension()
57
    {
58
        $imagePath         = substr($this->getPath(), 6);
59
        $imagePathSplitted = explode('#', $imagePath);
60
        return pathinfo($imagePathSplitted[1], PATHINFO_EXTENSION);
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getMimeType()
67
    {
68
        $pZIPFile = str_replace('zip://', '', $this->getPath());
69
        $pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
70
        if (!CommonFile::fileExists($pZIPFile)) {
71
            throw new \Exception("File $pZIPFile does not exist");
72
        }
73
        $pImgFile = substr($this->getPath(), strpos($this->getPath(), '#') + 1);
74
        $oArchive = new \ZipArchive();
75
        $oArchive->open($pZIPFile);
76
        if (!function_exists('getimagesizefromstring')) {
77
            $uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($pImgFile));
78
            $image = getimagesize($uri);
79
        } else {
80
            $image = getimagesizefromstring($oArchive->getFromName($pImgFile));
81
        }
82
        return image_type_to_mime_type($image[2]);
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getIndexedFilename()
89
    {
90
        $output = substr($this->getPath(), strpos($this->getPath(), '#') + 1);
91
        $output = str_replace('.' . $this->getExtension(), '', $output);
92
        $output .= $this->getImageIndex();
93
        $output .= $this->getExtension();
94
        $output = str_replace(' ', '_', $output);
95
        return $output;
96
    }
97
}