NetworkAddressTimestampSerializer::fromParser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
14
class NetworkAddressTimestampSerializer
15
{
16 5
    /**
17
     * @var \BitWasp\Buffertools\Types\Uint16
18 5
     */
19 5
    private $uint16;
20 5
21 5
    /**
22 5
     * @var \BitWasp\Buffertools\Types\Uint32
23 5
     */
24
    private $uint32;
25
26
    /**
27
     * @var \BitWasp\Buffertools\Types\Uint64
28
     */
29
    private $uint64le;
30 3
31
    /**
32 3
     * @var \BitWasp\Buffertools\Types\ByteString
33 3
     */
34 3
    private $bytestring16;
35 3
36 3
    public function __construct()
37 2
    {
38
        $this->uint16 = Types::uint16();
39
        $this->uint32 = Types::uint32();
40
        $this->uint64le = Types::uint64le();
41
        $this->bytestring16 = Types::bytestring(16);
42
    }
43
44 5
    /**
45
     * @param NetworkAddressTimestamp $addr
46 5
     * @return BufferInterface
47 5
     */
48 5
    public function serialize(NetworkAddressTimestamp $addr): BufferInterface
49 3
    {
50 3
        return new Buffer(
51 5
            sprintf(
52
                "%s%s%s%s",
53 3
                $this->uint32->write($addr->getTimestamp()),
54
                $this->uint64le->write($addr->getServices()),
55
                $addr->getIp()->getBuffer()->getBinary(),
56
                $this->uint16->write($addr->getPort())
57
            )
58
        );
59
    }
60
61
    /**
62
     * @param Parser $parser
63
     * @return NetworkAddressTimestamp
64
     */
65
    public function fromParser(Parser $parser): NetworkAddressTimestamp
66
    {
67
        $ipSerializer = new IpSerializer();
68
        return new NetworkAddressTimestamp(
69
            (int) $this->uint32->read($parser),
70
            (int) $this->uint64le->read($parser),
71
            $ipSerializer->fromParser($parser),
72
            (int) $this->uint16->read($parser)
73
        );
74
    }
75
}
76