1 | <?php |
||
2 | |||
3 | namespace kalanis\UploadPerPartes\Uploader; |
||
4 | |||
5 | |||
6 | /** |
||
7 | * Class Calculates |
||
8 | * @package kalanis\UploadPerPartes\Uploader |
||
9 | * Calculations over sizes |
||
10 | */ |
||
11 | class Calculates |
||
12 | { |
||
13 | protected const DEFAULT_BYTES_PER_PART = 262144; // 1024*256 |
||
14 | |||
15 | /** @var int<0, max> */ |
||
16 | protected int $bytesPerPart = 0; |
||
17 | |||
18 | 30 | public function __construct(Config $config) |
|
19 | { |
||
20 | 30 | $this->bytesPerPart = empty($config->bytesPerPart) |
|
21 | 4 | ? static::DEFAULT_BYTES_PER_PART |
|
22 | 26 | : max(0, $config->bytesPerPart) |
|
23 | ; |
||
24 | 30 | } |
|
25 | |||
26 | /** |
||
27 | * @return int<0, max> |
||
28 | */ |
||
29 | 17 | public function getBytesPerPart(): int |
|
30 | { |
||
31 | 17 | return $this->bytesPerPart; |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param int<0, max> $length |
||
36 | * @return int<0, max> |
||
37 | */ |
||
38 | 21 | public function calcParts(int $length): int |
|
39 | { |
||
40 | 21 | $partsCounter = abs(intval($length / $this->bytesPerPart)); |
|
41 | 21 | return (($length % $this->bytesPerPart) == 0) ? $partsCounter : $partsCounter + 1; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param Data $data |
||
46 | * @param int $segment |
||
47 | * @return int<0, max> |
||
48 | */ |
||
49 | 5 | public function bytesFromSegment(Data $data, int $segment): int |
|
50 | { |
||
51 | 5 | return max(0, $data->bytesPerPart * $segment); |
|
52 | } |
||
53 | } |
||
54 |