Completed
Pull Request — master (#446)
by thomas
105:00 queued 101:56
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
dl 0
loc 53
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
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\Script\Script;
6
use BitWasp\Bitcoin\Serializer\Types;
7
use BitWasp\Bitcoin\Transaction\TransactionOutput;
8
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
9
use BitWasp\Buffertools\Buffer;
10
use BitWasp\Buffertools\BufferInterface;
11
use BitWasp\Buffertools\Parser;
12
13
class TransactionOutputSerializer
14
{
15
    /**
16
     * @var \BitWasp\Buffertools\Types\Uint64
17
     */
18
    private $uint64le;
19
20
    /**
21
     * @var \BitWasp\Buffertools\Types\VarString
22
     */
23
    private $varstring;
24
25 1336
    public function __construct()
26
    {
27 1336
        $this->uint64le = Types::uint64le();
28 1336
        $this->varstring = Types::varstring();
29 1336
    }
30
31
    /**
32
     * @param TransactionOutputInterface $output
33
     * @return BufferInterface
34
     */
35 1304
    public function serialize(TransactionOutputInterface $output)
36
    {
37 1304
        return new Buffer(
38 1304
            $this->uint64le->write($output->getValue()) .
39 1304
            $this->varstring->write($output->getScript()->getBuffer())
40
        );
41
    }
42
43
    /**
44
     * @param Parser $parser
45
     * @return TransactionOutput
46
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
47
     */
48 66
    public function fromParser(Parser $parser)
49
    {
50 66
        return new TransactionOutput(
51 66
            $this->uint64le->read($parser),
52 66
            new Script($this->varstring->read($parser))
53
        );
54
    }
55
56
    /**
57
     * @param string $string
58
     * @return TransactionOutput
59
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
60
     */
61 2
    public function parse($string)
62
    {
63 2
        return $this->fromParser(new Parser($string));
64
    }
65
}
66