Test Failed
Push — develop ( f6d190...8ff4ed )
by Daniel
04:04
created

ImageMetadata   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 3
A getHeight() 0 3 1
A getImagineKey() 0 3 1
A getFilePath() 0 3 1
A getWidth() 0 3 1
A getPublicPath() 0 3 1
1
<?php
2
3
namespace Silverback\ApiComponentBundle\DTO\File;
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"})
13
     * @var int
14
     */
15
    private $width;
16
17
    /**
18
     * @Groups({"component", "content"})
19
     * @var int
20
     */
21
    private $height;
22
23
    /**
24
     * @Groups({"component", "content"})
25
     * @var string
26
     */
27
    private $filePath;
28
29
    /**
30
     * @Groups({"component", "content"})
31
     * @var string
32
     */
33
    private $publicPath;
34
35
    /**
36
     * @Groups({"component", "content"})
37
     * @var string|null
38
     */
39
    private $imagineKey;
40
41
    public function __construct(
42
        string $filePath,
43
        string $publicPath,
44
        ?string $imagineKey = null
45
    ) {
46
        $this->filePath = $filePath;
47
        $this->publicPath = $publicPath;
48
49
        if (!file_exists($filePath)) {
50
            throw new FileMissingException(sprintf('The file %s does not exist while constructing %s', $filePath, self::class));
51
        }
52
53
        if (false === \exif_imagetype($filePath)) {
54
            throw new FileNotImageException(sprintf('The file %s is not an image while constructing %s', $filePath, self::class));
55
        }
56
57
        [$this->width, $this->height] = getimagesize($filePath);
58
        $this->imagineKey = $imagineKey;
59
    }
60
61
    /**
62
     * @return int
63
     */
64
    public function getWidth(): int
65
    {
66
        return $this->width;
67
    }
68
69
    /**
70
     * @return int
71
     */
72
    public function getHeight(): int
73
    {
74
        return $this->height;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getFilePath(): string
81
    {
82
        return $this->filePath;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getPublicPath(): string
89
    {
90
        return $this->publicPath;
91
    }
92
93
    /**
94
     * @return null|string
95
     */
96
    public function getImagineKey(): ?string
97
    {
98
        return $this->imagineKey;
99
    }
100
}
101