Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
File
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
6 / 6
13
100.00% covered (success)
100.00%
1 / 1
 getPath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPath
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 getContents
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExtension
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMimeType
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getIndexedFilename
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation\Shape\Drawing;
21
22use PhpOffice\Common\File as CommonFile;
23use PhpOffice\PhpPresentation\Exception\FileNotFoundException;
24
25class File extends AbstractDrawingAdapter
26{
27    /**
28     * @var string
29     */
30    protected $path = '';
31
32    /**
33     * Get Path.
34     */
35    public function getPath(): string
36    {
37        return $this->path;
38    }
39
40    /**
41     * Set Path.
42     *
43     * @param string $pValue File path
44     * @param bool $pVerifyFile Verify file
45     */
46    public function setPath(string $pValue = '', bool $pVerifyFile = true): self
47    {
48        if ($pVerifyFile) {
49            if (!file_exists($pValue)) {
50                throw new FileNotFoundException($pValue);
51            }
52        }
53        $this->path = $pValue;
54
55        if ($pVerifyFile) {
56            if (0 == $this->width && 0 == $this->height) {
57                [$this->width, $this->height] = getimagesize($this->getPath());
58            }
59        }
60
61        return $this;
62    }
63
64    public function getContents(): string
65    {
66        return CommonFile::fileGetContents($this->getPath());
67    }
68
69    public function getExtension(): string
70    {
71        return pathinfo($this->getPath(), PATHINFO_EXTENSION);
72    }
73
74    public function getMimeType(): string
75    {
76        if (!CommonFile::fileExists($this->getPath())) {
77            throw new FileNotFoundException($this->getPath());
78        }
79        $image = getimagesizefromstring(CommonFile::fileGetContents($this->getPath()));
80
81        if (is_array($image)) {
82            return image_type_to_mime_type($image[2]);
83        }
84
85        return mime_content_type($this->getPath());
86    }
87
88    public function getIndexedFilename(): string
89    {
90        $output = str_replace('.' . $this->getExtension(), '', pathinfo($this->getPath(), PATHINFO_FILENAME));
91        $output .= $this->getImageIndex();
92        $output .= '.' . $this->getExtension();
93        $output = str_replace(' ', '_', $output);
94
95        return $output;
96    }
97}