Completed
Push — master ( 29ec42...778040 )
by thomas
9s
created

TransactionOutputSerializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 65
ccs 24
cts 24
cp 1
rs 10
wmc 5
lcom 1
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTemplate() 0 7 1
A serialize() 0 7 1
A fromParser() 0 13 1
A parse() 0 5 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Transaction;
4
5
use BitWasp\Buffertools\BufferInterface;
6
use BitWasp\Buffertools\Parser;
7
use BitWasp\Bitcoin\Script\Script;
8
use BitWasp\Bitcoin\Transaction\TransactionOutput;
9
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
10
use BitWasp\Buffertools\TemplateFactory;
11
12
class TransactionOutputSerializer
13
{
14
    /**
15
     * @var \BitWasp\Buffertools\Template
16
     */
17
    private $template;
18
19 171
    public function __construct()
20
    {
21 171
        $this->template = $this->getTemplate();
22 171
    }
23
24
    /**
25
     * @return \BitWasp\Buffertools\Template
26
     */
27 171
    private function getTemplate()
28
    {
29 171
        return (new TemplateFactory())
30 171
            ->uint64le()
31 171
            ->varstring()
32 171
            ->getTemplate();
33
    }
34
35
    /**
36
     * @param TransactionOutputInterface $output
37
     * @return BufferInterface
38
     */
39 123
    public function serialize(TransactionOutputInterface $output)
40
    {
41 123
        return $this->template->write([
42 123
            $output->getValue(),
43 123
            $output->getScript()->getBuffer()
44 82
        ]);
45
    }
46
47
    /**
48
     * @param Parser $parser
49
     * @return TransactionOutput
50
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
51
     */
52 114
    public function fromParser(Parser $parser)
53
    {
54 114
        $parse = $this->template->parse($parser);
55
        /** @var int $value */
56 114
        $value = $parse[0];
57
        /** @var BufferInterface $scriptBuf */
58 114
        $scriptBuf = $parse[1];
59
60 114
        return new TransactionOutput(
61 76
            $value,
62 114
            new Script($scriptBuf)
63 76
        );
64
    }
65
66
    /**
67
     * @param string $string
68
     * @return TransactionOutput
69
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
70
     */
71 3
    public function parse($string)
72
    {
73 3
        $parser = new Parser($string);
74 3
        return $this->fromParser($parser);
75
    }
76
}
77