Passed
Pull Request — master (#41)
by Nicolas
07:46 queued 33s
created

ChunkSize   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 43
ccs 23
cts 23
cp 1
rs 10
c 1
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 4
A toBytes() 0 14 2
1
<?php
2
3
namespace Puzzle\AMQP\Messages\Chunks;
4
5
final class ChunkSize
6
{
7
    private
8
        $size,
9
        $unit;
10
11 12
    public function __construct($size)
12
    {
13 12
        $exception = new \InvalidArgumentException("Given chunk size is not valid");
14
15 12
        if(preg_match("~^(\d+)(K|M)?$~", $size, $matches) !== 1)
16 12
        {
17 5
            throw $exception;
18
        }
19
20 7
        $this->size = (int) $matches[1];
21 7
        if($this->size <= 0)
22 7
        {
23 2
            throw $exception;
24
        }
25
26 5
        $this->unit = null;
27 5
        if(isset($matches[2]))
28 5
        {
29 2
            $this->unit = $matches[2];
30 2
        }
31 5
    }
32
33 7
    public function toBytes()
34
    {
35
        $unitsConversion = [
36 7
            "K" => 1024,
37 7
            "M" => pow(1024, 2),
38 7
        ];
39
40 7
        if(empty($this->unit))
41 7
        {
42 3
            return $this->size;
43
        }
44
45 4
        return $this->size * $unitsConversion[$this->unit];
46
    }
47
}
48