Completed
Push — develop ( 312b7a...53bbae )
by Franck
16s
created

ZipFile   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 101
ccs 18
cts 42
cp 0.4286
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 4 1
A setPath() 0 5 1
A getExtension() 0 4 1
A getZipFileOut() 0 6 2
A getZipFileIn() 0 6 2
A getContents() 0 13 2
A getMimeType() 0 15 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 2
    public function getPath()
20
    {
21 2
        return $this->path;
22
    }
23
24
    /**
25
     * Set Path
26
     *
27
     * @param  string                      $pValue      File path
28
     * @return \PhpOffice\PhpPresentation\Shape\Drawing
29
     */
30 2
    public function setPath($pValue = '')
31
    {
32 2
        $this->path = $pValue;
33 2
        return $this;
34
    }
35
36
    /**
37
     * @return string
38
     */
39 1
    public function getContents()
40
    {
41 1
        if (!CommonFile::fileExists($this->getZipFileOut())) {
42 1
            throw new \Exception('File '.$this->getZipFileOut().' does not exist');
43
        }
44
45
        $imageZip = new \ZipArchive();
46
        $imageZip->open($this->getZipFileOut());
47
        $imageContents = $imageZip->getFromName($this->getZipFileIn());
48
        $imageZip->close();
49
        unset($imageZip);
50
        return $imageContents;
51
    }
52
53
54
    /**
55
     * @return string
56
     */
57 1
    public function getExtension()
58
    {
59 1
        return pathinfo($this->getZipFileIn(), PATHINFO_EXTENSION);
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getMimeType()
66
    {
67
        if (!CommonFile::fileExists($this->getZipFileOut())) {
68
            throw new \Exception('File '.$this->getZipFileOut().' does not exist');
69
        }
70
        $oArchive = new \ZipArchive();
71
        $oArchive->open($this->getZipFileOut());
72
        if (!function_exists('getimagesizefromstring')) {
73
            $uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($this->getZipFileIn()));
74
            $image = getimagesize($uri);
75
        } else {
76
            $image = getimagesizefromstring($oArchive->getFromName($this->getZipFileIn()));
77
        }
78
        return image_type_to_mime_type($image[2]);
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getIndexedFilename()
85
    {
86
        $output = pathinfo($this->getZipFileIn(), PATHINFO_FILENAME);
87
        $output = str_replace('.' . $this->getExtension(), '', $output);
88
        $output .= $this->getImageIndex();
89
        $output .= '.'.$this->getExtension();
90
        $output = str_replace(' ', '_', $output);
91
        return $output;
92
    }
93
94 1
    protected function getZipFileOut()
95
    {
96 1
        $path = str_replace('zip://', '', $this->getPath());
97 1
        $path = explode('#', $path);
98 1
        return empty($path[0]) ? '' : $path[0];
99
    }
100
101 1
    protected function getZipFileIn()
102
    {
103 1
        $path = str_replace('zip://', '', $this->getPath());
104 1
        $path = explode('#', $path);
105 1
        return empty($path[1]) ? '' : $path[1];
106
    }
107
}
108