Completed
Push — feature/6.x ( db50a0...aa9894 )
by Schlaefer
03:28
created

UploaderConfig::setStorageFileSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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