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

ScriptWitnessSerializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
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