Passed
Push — master ( 3dc4f8...535c32 )
by thomas
15:05
created

NetworkAddressTimestampSerializer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromParser() 0 8 1
A serialize() 0 9 1
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Serializer\Structure;
6
7
use BitWasp\Bitcoin\Networking\Serializer\Ip\IpSerializer;
8
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressTimestamp;
9
use BitWasp\Bitcoin\Serializer\Types;
10
use BitWasp\Buffertools\Buffer;
11
use BitWasp\Buffertools\BufferInterface;
12
use BitWasp\Buffertools\Parser;
13
use BitWasp\Buffertools\TemplateFactory;
14
15
class NetworkAddressTimestampSerializer
16 5
{
17
    /**
18 5
     * @var \BitWasp\Buffertools\Types\Uint16
19 5
     */
20 5
    private $uint16;
21 5
22 5
    /**
23 5
     * @var \BitWasp\Buffertools\Types\Uint32
24
     */
25
    private $uint32;
26
27
    /**
28
     * @var \BitWasp\Buffertools\Types\Uint64
29
     */
30 3
    private $uint64le;
31
32 3
    /**
33 3
     * @var \BitWasp\Buffertools\Types\ByteString
34 3
     */
35 3
    private $bytestring16;
36 3
37 2
    public function __construct()
38
    {
39
        $this->uint16 = Types::uint16();
40
        $this->uint32 = Types::uint32();
41
        $this->uint64le = Types::uint64le();
42
        $this->bytestring16 = Types::bytestring(16);
43
    }
44 5
45
    /**
46 5
     * @param NetworkAddressTimestamp $addr
47 5
     * @return BufferInterface
48 5
     */
49 3
    public function serialize(NetworkAddressTimestamp $addr): BufferInterface
50 3
    {
51 5
        return new Buffer(
52
            sprintf(
53 3
                "%s%s%s%s",
54
                $this->uint32->write($addr->getTimestamp()),
55
                $this->uint64le->write($addr->getServices()),
56
                $addr->getIp()->getBuffer()->getBinary(),
57
                $this->uint16->write($addr->getPort())
58
            )
59
        );
60
    }
61
62
    /**
63
     * @param Parser $parser
64
     * @return NetworkAddressTimestamp
65
     */
66
    public function fromParser(Parser $parser): NetworkAddressTimestamp
67
    {
68
        $ipSerializer = new IpSerializer();
69
        return new NetworkAddressTimestamp(
70
            (int) $this->uint32->read($parser),
71
            (int) $this->uint64le->read($parser),
72
            $ipSerializer->fromParser($parser),
73
            (int) $this->uint16->read($parser)
74
        );
75
    }
76
}
77