Completed
Pull Request — master (#41)
by Nicolas
07:53
created

ChunkMetadata::buildFromHeaders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Puzzle\AMQP\Messages\Chunks;
4
5
final class ChunkMetadata
6
{
7
    private
8
        $playhead,
9
        $offset,
10
        $size;
11
12 4
    public function __construct($playhead, $offset, $size)
13
    {
14 4
        $this->playhead = $playhead;
15 4
        $this->offset = $offset;
16 4
        $this->size = $size;
17 4
    }
18
19 2
    public static function buildFromHeaders(array $headers)
20
    {
21 2
        $requiredKeys = ['playhead', 'offset', 'size'];
22
23 2
        foreach($requiredKeys as $key)
24
        {
25 2
            if(! isset($headers[$key]))
26 2
            {
27 1
                throw new \InvalidArgumentException("Missing $key in chunk metadata");
28
            }
29 1
        }
30
31 1
        return new self($headers['playhead'], $headers['offset'], $headers['size']);
32
    }
33
34 1
    public function playhead()
35
    {
36 1
        return $this->playhead;
37
    }
38
39 1
    public function offset()
40
    {
41 1
        return $this->offset;
42
    }
43
44 4
    public function size()
45
    {
46 4
        return $this->size;
47
    }
48
49 2
    public function toHeaders()
50
    {
51
        return [
52 2
            'offset' => $this->offset,
53 2
            'playhead' => $this->playhead,
54 2
            'size' => $this->size,
55 2
        ];
56
    }
57
}
58