Passed
Push — v2 ( 0a3b79...966528 )
by Daniel
05:01
created

ImageMetadata::getImagineKey()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Dto\File;
15
16
use Silverback\ApiComponentBundle\Exception\FileMissingException;
17
use Silverback\ApiComponentBundle\Exception\FileNotImageException;
18
use function exif_imagetype;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
final class ImageMetadata
24
{
25
    private int $width = 0;
26
    private int $height = 0;
27
    private string $filePath;
28
    private string $publicPath;
29
30
    public function __construct(
31
        string $filePath,
32
        string $publicPath
33
    ) {
34
        $this->filePath = $filePath;
35
        $this->publicPath = $publicPath;
36
37
        if (!file_exists($filePath)) {
38
            throw new FileMissingException(sprintf('The file %s does not exist while constructing %s', $filePath, self::class));
39
        }
40
41
        if ('image/svg+xml' === mime_content_type($filePath)) {
42
            $xmlGet = simplexml_load_string(file_get_contents($filePath));
43
            $xmlAttributes = $xmlGet->attributes();
44
            $this->width = (int) $xmlAttributes->width;
45
            $this->height = (int) $xmlAttributes->height;
46
        } else {
47
            if (false === exif_imagetype($filePath)) {
48
                throw new FileNotImageException(sprintf('The file %s is not an image while constructing %s', $filePath, self::class));
49
            }
50
51
            [$this->width, $this->height] = getimagesize($filePath);
52
        }
53
    }
54
55
    public function getWidth(): int
56
    {
57
        return $this->width;
58
    }
59
60
    public function getHeight(): int
61
    {
62
        return $this->height;
63
    }
64
65
    public function getFilePath(): string
66
    {
67
        return $this->filePath;
68
    }
69
70
    public function getPublicPath(): string
71
    {
72
        return $this->publicPath;
73
    }
74
}
75