Passed
Pull Request — master (#98)
by thomas
03:01
created

Headers::addHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Messages;
6
7
use BitWasp\Bitcoin\Networking\Message;
8
use BitWasp\Bitcoin\Networking\NetworkSerializable;
9
use BitWasp\Bitcoin\Networking\Serializer\Message\HeadersSerializer;
10
use BitWasp\Buffertools\BufferInterface;
11
12
class Headers extends NetworkSerializable implements \Countable
13
{
14
    /**
15
     * @var BufferInterface[]
16
     */
17
    private $headers = [];
18
19
    public function __construct(BufferInterface ...$headers)
20
    {
21 12
        $this->headers = $headers;
22
    }
23 12
24 9
    /**
25 8
     * @return string
26 12
     * @see https://en.bitcoin.it/wiki/Protocol_documentation#headers
27
     */
28
    public function getNetworkCommand(): string
29
    {
30
        return Message::HEADERS;
31
    }
32 9
33
    /**
34 9
     * @return BufferInterface[]
35 9
     */
36
    public function getHeaders(): array
37
    {
38
        return $this->headers;
39
    }
40 6
41
    /**
42 6
     * @return int
43
     */
44
    public function count(): int
45
    {
46
        return count($this->headers);
47
    }
48 9
49
    /**
50 9
     * @param int $index
51
     * @return BufferInterface
52
     */
53
    public function getHeader(int $index): BufferInterface
54
    {
55
        if (!array_key_exists($index, $this->headers)) {
56 6
            throw new \InvalidArgumentException('No header exists at this index');
57
        }
58 6
59
        return $this->headers[$index];
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     * @see \BitWasp\Bitcoin\SerializableInterface::getBuffer()
65 6
     */
66
    public function getBuffer(): BufferInterface
67 6
    {
68 3
        return (new HeadersSerializer())->serialize($this);
69
    }
70
}
71