Completed
Branch feature/fix374 (6cfc47)
by Schlaefer
09:41
created

UploaderConfig::getJpegCompressionFactor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace ImageUploader\Lib;
14
15
use Cake\Utility\Text;
16
17
/**
18
 * Configuration class for the uploader
19
 */
20
class UploaderConfig
21
{
22
    /** @var string key used for CakePHP upload cache */
23
    private const CACHE_KEY = 'uploadsThumbnails';
24
25
    /** @var int max upload size in bytes */
26
    private $defaultSize = 2000000;
27
28
    /** @var int max number of allowed uploads per user */
29
    private $maxNumberOfUploads = 10;
30
31
    /** @var array allowed mime types */
32
    private $types = [];
33
34
    /** @var int Threshold (and rough target) for resizing images in bytes */
35
    private $defaultResize = 650000;
36
37
    /** @var int Jpeg compression factor between 0 and 100 */
38
    private $jpegCompression = 92;
39
40
    /**
41
     * Set JPEG compression factor
42
     *
43
     * @param int $compression Number between 0 and 100
44
     * @return self
45
     * @throws \InvalidArgumentException if compression factor isn't valid.
46
     */
47
    public function setJpegCompressionFactor(int $compression): self
48
    {
49
        if ($compression < 0 || $compression > 100) {
50
            throw new \InvalidArgumentException(
51
                "Jpeg compression factor must be between 0 and 100.",
52
                1596187723
53
            );
54
        }
55
        $this->jpegCompression = $compression;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Get Jpeg compression factor
62
     *
63
     * @return int
64
     */
65
    public function getJpegCompressionFactor(): int
66
    {
67
        return $this->jpegCompression;
68
    }
69
70
    /**
71
     * Set default max file size when resizing a type
72
     *
73
     * @param string|int $size Size in bytes as int or string e.g. "3MB"
74
     * @return self
75
     * @throws \InvalidArgumentException if size isn't valid.
76
     */
77
    public function setDefaultMaxResize($size): self
78
    {
79
        if (is_string($size)) {
80
            $size = (int)Text::parseFileSize($size, $this->defaultResize);
81
        }
82
        if (!is_int($size)) {
0 ignored issues
show
introduced by
The condition is_int($size) is always true.
Loading history...
83
            throw new \InvalidArgumentException(
84
                'Default max resize isn\'t a number.',
85
                1596199482
86
            );
87
        }
88
        $this->defaultResize = $size;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Get max file size when resizing a type
95
     *
96
     * @return int Size in bytes
97
     */
98
    public function getMaxResize(): int
99
    {
100
        return $this->defaultResize;
101
    }
102
103
    /**
104
     * Sets max allowed uploads per user
105
     *
106
     * @param int $max Max uploads
107
     * @return self
108
     */
109
    public function setMaxNumberOfUploadsPerUser(int $max): self
110
    {
111
        $this->maxNumberOfUploads = $max;
112
113
        return $this;
114
    }
115
116
    /**
117
     * Gets max allowed uploads per user
118
     *
119
     * @return int max uploads per user
120
     */
121
    public function getMaxNumberOfUploadsPerUser(): int
122
    {
123
        return $this->maxNumberOfUploads;
124
    }
125
126
    /**
127
     * Set default max file size
128
     *
129
     * @param string|int $number bytes as int or string e.g. "3MB"
130
     * @return self
131
     */
132
    public function setDefaultMaxFileSize($number): self
133
    {
134
        if (is_string($number)) {
135
            $number = (int)Text::parseFileSize($number, $this->defaultSize);
136
        }
137
        if (!is_int($number)) {
0 ignored issues
show
introduced by
The condition is_int($number) is always true.
Loading history...
138
            throw new \InvalidArgumentException(
139
                'Max upload size isn\'t a number.',
140
                1561364890
141
            );
142
        }
143
144
        $this->defaultSize = $number;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Adds a new mime type which is allowed to be uploaded
151
     *
152
     * @param string $type mime type as in "image/jpeg"
153
     * @param string|int|null $size optional file size different from default file size
154
     * @return self
155
     */
156
    public function addType(string $type, $size = null): self
157
    {
158
        if ($size === null) {
159
            $size = $this->defaultSize;
160
        } elseif (is_string($size)) {
161
            $size = (int)Text::parseFileSize($size, $this->defaultSize);
162
        }
163
        if (!is_int($size)) {
0 ignored issues
show
introduced by
The condition is_int($size) is always true.
Loading history...
164
            throw new \InvalidArgumentException(
165
                'Upload size isn\'t a number.',
166
                1561364891
167
            );
168
        }
169
        $this->types[$type] = ['size' => $size ?: $this->defaultSize];
170
171
        return $this;
172
    }
173
174
    /**
175
     * Gets all allowed file types as array
176
     *
177
     * @return array file types
178
     */
179
    public function getAllTypes(): array
180
    {
181
        return array_keys($this->types);
182
    }
183
184
    /**
185
     * Gets file size for a a mime type
186
     *
187
     * @param string $type mime-type
188
     * @return int file-size in bytes
189
     * @throws \RuntimeException if mime-type isn't set
190
     */
191
    public function getSize(string $type): int
192
    {
193
        if (!$this->hasType($type)) {
194
            throw new \RuntimeException(
195
                sprintf('Upload type %s not found.', $type),
196
                1561357996
197
            );
198
        }
199
200
        return $this->types[$type]['size'];
201
    }
202
203
    /**
204
     * Gets cache key
205
     *
206
     * @return string cache-key
207
     */
208
    public function getCacheKey(): string
209
    {
210
        return self::CACHE_KEY;
211
    }
212
213
    /**
214
     * Checks if a mime-type is registered
215
     *
216
     * @param string $type mime-type
217
     * @return bool is type registered
218
     */
219
    public function hasType(string $type): bool
220
    {
221
        return !empty($this->types[$type]);
222
    }
223
}
224