Completed
Push — master ( a858c2...867828 )
by David
01:09
created

Image::content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GNAHotelSolutions\ImageCacher;
4
5
class Image
6
{
7
    /** @var string */
8
    protected $rootPath;
9
10
    /** @var string */
11
    protected $name;
12
13
    /** @var string */
14
    protected $path;
15
16
    /** @var int */
17
    protected $width;
18
19
    /** @var int */
20
    protected $height;
21
22
    /** @var string */
23
    protected $type;
24
25
    public function __construct(string $image, string $rootPath = '')
26
    {
27
        $this->rootPath = rtrim($rootPath, '/');
28
29
        [$this->name, $this->path] = $this->splitNameAndPath($image);
30
31
        $this->verify();
32
33
        $this->extractImageInformation();
34
    }
35
36
    public function verify(): void
37
    {
38
        if (! file_exists($this->getOriginalFullPath())) {
39
            throw new \Exception("file [{$this->getOriginalFullPath()}] not found.");
40
        }
41
    }
42
43
    protected function splitNameAndPath(string $image): array
44
    {
45
        $pieces = explode('/', $image);
46
47
        return [array_pop($pieces), implode('/', $pieces)];
48
    }
49
50
    protected function extractImageInformation(): self
51
    {
52
        $information = getimagesize($this->getOriginalFullPath());
53
54
        $this->width = $information[0];
55
        $this->height = $information[1];
56
        $this->type = explode('/', $information['mime'])[1];
57
58
        return $this;
59
    }
60
61
    public function getOriginalName(): string
62
    {
63
        if ($this->getPath() === '') {
64
            return $this->getName();
65
        }
66
67
        return "{$this->getPath()}/{$this->getName()}";
68
    }
69
70
    public function getOriginalFullPath(): string
71
    {
72
        if ($this->rootPath === '') {
73
            return $this->getOriginalName();
74
        }
75
76
        return "{$this->rootPath}/{$this->getOriginalName()}";
77
    }
78
79
    public function getPath(): string
80
    {
81
        return $this->path;
82
    }
83
84
    public function getName(): string
85
    {
86
        return $this->name;
87
    }
88
89
    public function getType(): string
90
    {
91
        return $this->type;
92
    }
93
94
    public function getWidth(): int
95
    {
96
        return $this->width;
97
    }
98
99
    public function getHeight(): int
100
    {
101
        return $this->height;
102
    }
103
104
    public function getAspectRatio(): float
105
    {
106
        return round($this->width / $this->height, 2);
107
    }
108
109
    public function isSmallerThan(?int $width, ?int $height): bool
110
    {
111
        return $this->width <= $width && $this->height <= $height;
112
    }
113
114
    public function content(): string
115
    {
116
        return file_get_contents($this->getOriginalFullPath());
117
    }
118
}
119