Completed
Push — master ( 5b5792...2b84bd )
by Schlaefer
03:33 queued 11s
created

UploaderConfig::getSize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
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
    /**
35
     * Sets max allowed uploads per user
36
     *
37
     * @param int $max Max uploads
38
     * @return self
39
     */
40
    public function setMaxNumberOfUploadsPerUser(int $max): self
41
    {
42
        $this->maxNumberOfUploads = $max;
43
44
        return $this;
45
    }
46
47
    /**
48
     * Gets max allowed uploads per user
49
     *
50
     * @return int max uploads per user
51
     */
52
    public function getMaxNumberOfUploadsPerUser(): int
53
    {
54
        return $this->maxNumberOfUploads;
55
    }
56
57
    /**
58
     * Set default max file size
59
     *
60
     * @param string|int $number bytes as int or string e.g. "3MB"
61
     * @return self
62
     */
63
    public function setDefaultMaxFileSize($number): self
64
    {
65
        if (is_string($number)) {
66
            $number = (int)Text::parseFileSize($number, $this->defaultSize);
67
        }
68
        if (!is_int($number)) {
69
            throw new \InvalidArgumentException(
70
                'Max upload size isn\'t a number.',
71
                1561364890
72
            );
73
        }
74
75
        $this->defaultSize = $number;
76
77
        return $this;
78
    }
79
80
    /**
81
     * Adds a new mime type which is allowed to be uploaded
82
     *
83
     * @param string $type mime type as in "image/jpeg"
84
     * @param string|int|null $size optional file size different from default file size
85
     * @return self
86
     */
87
    public function addType(string $type, $size = null): self
88
    {
89
        if ($size === null) {
90
            $size = $this->defaultSize;
91
        } elseif (is_string($size)) {
92
            $size = (int)Text::parseFileSize($size, $this->defaultSize);
93
        }
94
        if (!is_int($size)) {
95
            throw new \InvalidArgumentException(
96
                'Upload size isn\'t a number.',
97
                1561364891
98
            );
99
        }
100
        $this->types[$type] = ['size' => $size ?: $this->defaultSize];
101
102
        return $this;
103
    }
104
105
    /**
106
     * Gets all allowed file types as array
107
     *
108
     * @return array file types
109
     */
110
    public function getAllTypes(): array
111
    {
112
        return array_keys($this->types);
113
    }
114
115
    /**
116
     * Gets file size for a a mime type
117
     *
118
     * @param string $type mime-type
119
     * @return int file-size in bytes
120
     * @throws \RuntimeException if mime-type isn't set
121
     */
122
    public function getSize(string $type): int
123
    {
124
        if (!$this->hasType($type)) {
125
            throw new \RuntimeException(
126
                sprintf('Upload type %s not found.', $type),
127
                1561357996
128
            );
129
        }
130
131
        return $this->types[$type]['size'];
132
    }
133
134
    /**
135
     * Gets cache key
136
     *
137
     * @return string cache-key
138
     */
139
    public function getCacheKey(): string
140
    {
141
        return self::CACHE_KEY;
142
    }
143
144
    /**
145
     * Checks if a mime-type is registered
146
     *
147
     * @param string $type mime-type
148
     * @return bool is type registered
149
     */
150
    public function hasType(string $type): bool
151
    {
152
        return !empty($this->types[$type]);
153
    }
154
}
155