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

ScriptWitnessSerializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 52.94%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 45
ccs 9
cts 17
cp 0.5294
rs 10
wmc 5
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fromParser() 0 10 2
A serialize() 0 10 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Serializer\Script;
4
5
use BitWasp\Bitcoin\Script\ScriptWitness;
6
use BitWasp\Bitcoin\Script\ScriptWitnessInterface;
7
use BitWasp\Bitcoin\Serializer\Types;
8
use BitWasp\Buffertools\BufferInterface;
9
use BitWasp\Buffertools\Buffertools;
10
use BitWasp\Buffertools\Parser;
11
use BitWasp\Buffertools\Template;
12
13
class ScriptWitnessSerializer
14
{
15
    /**
16
     * @var Template
17
     */
18
    private $template;
19
20
    public function __construct()
21
    {
22
        $this->template = new Template([
23
            Types::varstring()
24
        ]);
25
    }
26
27
    /**
28
     * @param Parser $parser
29
     * @param $size
30
     * @return ScriptWitness
31
     */
32
    public function fromParser(Parser $parser, $size)
33
    {
34
        $entries = [];
35
        for ($j = 0; $j < $size; $j++) {
36 66
            list ($data) = $this->template->parse($parser);
37
            $entries[] = $data;
38 66
        }
39 66
40 66
        return new ScriptWitness($entries);
41
    }
42 66
43 66
    /**
44 66
     * @param ScriptWitnessInterface $witness
45 33
     * @return BufferInterface
46 66
     */
47
    public function serialize(ScriptWitnessInterface $witness)
48
    {
49
        $parser = new Parser();
50
        $parser->appendBuffer(Buffertools::numToVarInt($witness->count()));
0 ignored issues
show
Bug introduced by
The method appendBuffer() does not seem to exist on object<BitWasp\Buffertools\Parser>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
        foreach ($witness as $value) {
52
            $parser->appendBuffer($this->template->write([$value]));
0 ignored issues
show
Bug introduced by
The method appendBuffer() does not seem to exist on object<BitWasp\Buffertools\Parser>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
        }
54
55
        return $parser->getBuffer();
56
    }
57
}
58