Passed
Push — master ( 1624d7...0be8e1 )
by Daniel
14:55
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 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Serializer;
4
5
use Silverback\ApiComponentBundle\Exception\FileMissingException;
6
use Silverback\ApiComponentBundle\Exception\FileNotImageException;
7
use Symfony\Component\Serializer\Annotation\Groups;
8
9
class ImageMetadata
10
{
11
    /**
12
     * @Groups({"component", "content", "route"})
13
     * @var int
14
     */
15
    private $width;
16
    /**
17
     * @Groups({"component", "content", "route"})
18
     * @var int
19
     */
20
    private $height;
21
    /**
22
     * @Groups({"component", "content", "route"})
23
     * @var string
24
     */
25
    private $filePath;
26
    /**
27
     * @Groups({"component", "content", "route"})
28
     * @var string
29
     */
30
    private $publicPath;
31
    /**
32
     * @Groups({"component", "content", "route"})
33
     * @var string|null
34
     */
35
    private $imagineKey;
36
37 1
    public function __construct(
38
        string $filePath,
39
        string $publicPath,
40
        ?string $imagineKey = null
41
    )
42
    {
43 1
        $this->filePath = $filePath;
44 1
        $this->publicPath = $publicPath;
45
46 1
        if (!file_exists($filePath)) {
47
            throw new FileMissingException(sprintf('The file %s does not exist while constructing %s', $filePath, self::class));
48
        }
49
50 1
        if (false === \exif_imagetype($filePath)) {
51
            throw new FileNotImageException(sprintf('The file %s is not an image while constructing %s', $filePath, self::class));
52
        }
53
54 1
        [$this->width, $this->height] = getimagesize($filePath);
55 1
        $this->imagineKey= $imagineKey;
56 1
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function getWidth(): int
62
    {
63
        return $this->width;
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    public function getHeight(): int
70
    {
71
        return $this->height;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getFilePath(): string
78
    {
79
        return $this->filePath;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getPublicPath(): string
86
    {
87
        return $this->publicPath;
88
    }
89
90
    /**
91
     * @return null|string
92
     */
93
    public function getImagineKey(): ?string
94
    {
95
        return $this->imagineKey;
96
    }
97
}
98