Completed
Pull Request — master (#446)
by thomas
157:14 queued 87:18
created

TransactionOutputSerializer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 53
ccs 21
cts 21
cp 1
rs 10
wmc 4
lcom 1
cbo 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A serialize() 0 7 1
A fromParser() 0 7 1
A parse() 0 4 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Transaction;
4
5
use BitWasp\Bitcoin\Serializer\Types;
6
use BitWasp\Buffertools\Buffer;
7
use BitWasp\Buffertools\BufferInterface;
8
use BitWasp\Buffertools\Parser;
9
use BitWasp\Bitcoin\Script\Script;
10
use BitWasp\Bitcoin\Transaction\TransactionOutput;
11
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
12
13
class TransactionOutputSerializer
14
{
15
    /**
16
     * @var \BitWasp\Buffertools\Types\Uint64
17
     */
18
    private $uint64le;
19 2692
20
    /**
21 2692
     * @var \BitWasp\Buffertools\Types\VarString
22 2692
     */
23
    private $varstring;
24
25
    public function __construct()
26
    {
27 2692
        $this->uint64le = Types::uint64le();
28
        $this->varstring = Types::varstring();
29 2692
    }
30 2692
31 2692
    /**
32 2692
     * @param TransactionOutputInterface $output
33
     * @return BufferInterface
34
     */
35
    public function serialize(TransactionOutputInterface $output)
36
    {
37
        return new Buffer(
38
            $this->uint64le->write($output->getValue()) .
39 2596
            $this->varstring->write($output->getScript()->getBuffer())
40
        );
41 2596
    }
42 2596
43 2596
    /**
44 1298
     * @param Parser $parser
45
     * @return TransactionOutput
46
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
47
     */
48
    public function fromParser(Parser $parser)
49
    {
50
        return new TransactionOutput(
51
            $this->uint64le->read($parser),
52 198
            new Script($this->varstring->read($parser))
53
        );
54 198
    }
55
56 198
    /**
57
     * @param string $string
58 198
     * @return TransactionOutput
59
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
60 198
     */
61 99
    public function parse($string)
62 198
    {
63 99
        return $this->fromParser(new Parser($string));
64
    }
65
}
66