Completed
Pull Request — develop (#207)
by Franck
06:53
created

File::setPath()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 8.8571
cc 6
eloc 9
nc 7
nop 2
crap 6
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Shape\Drawing;
4
5
use SplFileInfo;
6
7
class File extends AbstractDrawingAdapter
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $path;
13
14
    /**
15
     * Get Path
16
     *
17
     * @return string
18
     */
19 19
    public function getPath()
20
    {
21 19
        return $this->path;
22
    }
23
24
    /**
25
     * Set Path
26
     *
27
     * @param  string                      $pValue      File path
28
     * @param  boolean                     $pVerifyFile Verify file
29
     * @throws \Exception
30
     * @return \PhpOffice\PhpPresentation\Shape\Drawing
31
     */
32 19
    public function setPath($pValue = '', $pVerifyFile = true)
33
    {
34 19
        if ($pVerifyFile) {
35 14
            if (!file_exists($pValue)) {
36 1
                throw new \Exception("File $pValue not found!");
37
            }
38
        }
39 18
        $this->path = $pValue;
40
41 18
        if ($pVerifyFile) {
42 13
            if ($this->width == 0 && $this->height == 0) {
43 13
                list($this->width, $this->height) = getimagesize($this->getPath());
44
            }
45
        }
46
47 18
        return $this;
48
    }
49
50
    /**
51
     * @return string
52
     */
53 10
    public function getContents()
54
    {
55 10
        return file_get_contents($this->getPath());
56
    }
57
58
59
    /**
60
     * @return string
61
     */
62 13
    public function getExtension()
63
    {
64 13
        return pathinfo($this->getPath(), PATHINFO_EXTENSION);
65
    }
66
67
    /**
68
     * @return string
69
     */
70 10
    public function getMimeType()
71
    {
72 10
        if (!file_exists($this->getPath())) {
73 2
            throw new \Exception('File '.$this->getPath().' does not exist');
74
        }
75 8
        $image = getimagesize($this->getPath());
76 8
        return image_type_to_mime_type($image[2]);
77
    }
78
79
    /**
80
     * @return string
81
     */
82 11
    public function getIndexedFilename()
83
    {
84 11
        $output = str_replace('.' . $this->getExtension(), '', pathinfo($this->getPath(), PATHINFO_FILENAME));
85 11
        $output .= $this->getImageIndex();
86 11
        $output .= '.'.$this->getExtension();
87 11
        $output = str_replace(' ', '_', $output);
88 11
        return $output;
89
    }
90
}
91