Calculates   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 41
ccs 12
cts 12
cp 1
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A calcParts() 0 4 2
A getBytesPerPart() 0 3 1
A __construct() 0 5 2
A bytesFromSegment() 0 3 1
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
The expression return $length % $this->...ter : $partsCounter + 1 could return the type double which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
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