Completed
Push — master ( 0be8e1...585ca2 )
by Daniel
59:26 queued 46:02
created

ImageMetadata::getFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silverback\ApiComponentBundle\Dto\File;
6
7
use Silverback\ApiComponentBundle\Exception\FileMissingException;
8
use Silverback\ApiComponentBundle\Exception\FileNotImageException;
9
use Symfony\Component\Serializer\Annotation\Groups;
10
11
class ImageMetadata
12
{
13
    /**
14
     * @Groups({"default"})
15
     * @var int
16
     */
17
    private $width;
18
19
    /**
20
     * @Groups({"default"})
21
     * @var int
22
     */
23
    private $height;
24
25
    /**
26
     * @Groups({"default"})
27
     * @var string
28
     */
29
    private $filePath;
30
31
    /**
32
     * @Groups({"default"})
33
     * @var string
34
     */
35
    private $publicPath;
36
37
    /**
38
     * @Groups({"default"})
39
     * @var string|null
40
     */
41
    private $imagineKey;
42
43
    public function __construct(
44
        string $filePath,
45
        string $publicPath,
46
        ?string $imagineKey = null
47
    ) {
48
        $this->filePath = $filePath;
49
        $this->publicPath = $publicPath;
50
51
        if (!file_exists($filePath)) {
52
            throw new FileMissingException(sprintf('The file %s does not exist while constructing %s', $filePath, self::class));
53
        }
54
55
        if (mime_content_type($filePath) === 'image/svg+xml') {
56
            $xmlget = simplexml_load_string(file_get_contents($filePath));
57
            $xmlattributes = $xmlget->attributes();
58
            $this->width = (int) $xmlattributes->width;
59
            $this->height = (int) $xmlattributes->height;
60
        } else {
61
            if (false === \exif_imagetype($filePath)) {
62
                throw new FileNotImageException(sprintf('The file %s is not an image while constructing %s', $filePath, self::class));
63
            }
64
65
            [$this->width, $this->height] = getimagesize($filePath);
66
            $this->imagineKey = $imagineKey;
67
        }
68
    }
69
70
    /**
71
     * @return int
72
     */
73
    public function getWidth(): int
74
    {
75
        return $this->width;
76
    }
77
78
    /**
79
     * @return int
80
     */
81
    public function getHeight(): int
82
    {
83
        return $this->height;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getFilePath(): string
90
    {
91
        return $this->filePath;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getPublicPath(): string
98
    {
99
        return $this->publicPath;
100
    }
101
102
    /**
103
     * @return null|string
104
     */
105
    public function getImagineKey(): ?string
106
    {
107
        return $this->imagineKey;
108
    }
109
}
110