Completed
Push — develop ( f71a4a...08d532 )
by Franck
17s
created

ZipFile::getZipFileOut()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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