Passed
Branch develop (2fd4b5)
by Alexey
01:37
created

ImageInfo::asArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * @author   Ne-Lexa
6
 * @license  MIT
7
 * @link     https://github.com/Ne-Lexa/google-play-scraper
8
 */
9
10
namespace Nelexa\GPlay\Model;
11
12
/**
13
 * Contains information about the image.
14
 */
15
class ImageInfo implements \JsonSerializable
16
{
17
    use JsonSerializableTrait;
18
19
    /** @var string Image url. */
20
    private $url;
21
22
    /** @var string Local image filename. */
23
    private $filename;
24
25
    /** @var string Image mime-type. */
26
    private $mimeType;
27
28
    /** @var string Image file extension. */
29
    private $extension;
30
31
    /** @var int Image width. */
32
    private $width;
33
34
    /** @var int Image height. */
35
    private $height;
36
37
    /** @var int Image file size. */
38
    private $filesize;
39
40
    /**
41
     * Creates an object with information about the image saved to disk.
42
     *
43
     * @param string $url Image url.
44
     * @param string $filename Local image filename.
45
     */
46 2
    public function __construct(string $url, string $filename)
47
    {
48 2
        $this->url = $url;
49 2
        $imageInfo = getimagesize($filename);
50 2
        if (!$imageInfo) {
51
            throw new \RuntimeException('Invalid image: ' . $filename);
52
        }
53 2
        $this->filename = $filename;
54 2
        $this->mimeType = $imageInfo['mime'];
55 2
        [$this->width, $this->height, $imageType] = $imageInfo;
56 2
        $this->extension = str_replace(
57 2
            'jpeg',
58 2
            'jpg',
59 2
            image_type_to_extension($imageType, false)
60
        );
61 2
        $this->filesize = filesize($filename);
62 2
    }
63
64
    /**
65
     * Returns the url of the image.
66
     *
67
     * @return string Image url.
68
     */
69
    public function getUrl(): string
70
    {
71
        return $this->url;
72
    }
73
74
    /**
75
     * Returns the path to save the image file.
76
     *
77
     * @return string Image filename.
78
     */
79
    public function getFilename(): string
80
    {
81
        return $this->filename;
82
    }
83
84
    /**
85
     * Returns the mime type of the image.
86
     *
87
     * @return string Image mime-type.
88
     */
89
    public function getMimeType(): string
90
    {
91
        return $this->mimeType;
92
    }
93
94
    /**
95
     * Returns the image file extension.
96
     *
97
     * @return string Image file extension.
98
     */
99
    public function getExtension(): string
100
    {
101
        return $this->extension;
102
    }
103
104
    /**
105
     * Returns the width of the image.
106
     *
107
     * @return int Image width.
108
     */
109 1
    public function getWidth(): int
110
    {
111 1
        return $this->width;
112
    }
113
114
    /**
115
     * Returns the height of the image.
116
     *
117
     * @return int Image height.
118
     */
119 1
    public function getHeight(): int
120
    {
121 1
        return $this->height;
122
    }
123
124
    /**
125
     * Returns the size of the image file.
126
     *
127
     * @return int Image file size.
128
     */
129
    public function getFilesize(): int
130
    {
131
        return $this->filesize;
132
    }
133
134
    /**
135
     * Returns class properties as an array.
136
     *
137
     * @return array Class properties as an array.
138
     */
139
    public function asArray(): array
140
    {
141
        return [
142
            'url' => $this->url,
143
            'path' => $this->filename,
144
            'mimeType' => $this->mimeType,
145
            'extension' => $this->extension,
146
            'width' => $this->width,
147
            'height' => $this->height,
148
        ];
149
    }
150
}
151