Completed
Push — master ( 40a4e1...e12674 )
by thomas
37:14 queued 34:53
created

ParsedScript::getPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Path;
4
5
use BitWasp\Bitcoin\Script\ScriptInterface;
6
7
class ParsedScript
8
{
9
    /**
10
     * @var ScriptInterface
11
     */
12
    private $script;
13
14
    /**
15
     * @var LogicOpNode
16
     */
17
    private $ast;
18
19
    /**
20
     * @var array
21
     */
22
    private $descriptorMap;
23
24
    /**
25
     * ParsedScript constructor.
26
     * @param ScriptInterface $script
27
     * @param LogicOpNode $ast
28
     * @param ScriptBranch[] $branches
29
     */
30 182
    public function __construct(ScriptInterface $script, LogicOpNode $ast, array $branches)
31
    {
32 182
        if (!$ast->isRoot()) {
33 2
            throw new \RuntimeException("LogicOpNode was not for root");
34
        }
35
36 180
        $descriptorIdx = 0;
37 180
        $keyedIdxMap = []; // descriptor => ScriptBranch
38 180
        foreach ($branches as $branch) {
39 180
            $descriptor = $branch->getPath();
40 180
            $descriptorKey = json_encode($descriptor);
41 180
            if (array_key_exists($descriptorKey, $keyedIdxMap)) {
42 2
                throw new \RuntimeException("Duplicate logical pathway, invalid ScriptBranch found");
43
            }
44
45 180
            $keyedIdxMap[$descriptorKey] = $branch;
46 180
            $descriptorIdx++;
47
        }
48
49 178
        $this->descriptorMap = $keyedIdxMap;
50 178
        $this->script = $script;
51 178
        $this->ast = $ast;
52 178
    }
53
54
    /**
55
     * @return bool
56
     */
57 174
    public function hasMultipleBranches()
58
    {
59 174
        return $this->ast->hasChildren();
60
    }
61
62
    /**
63
     * Returns a list of paths for this script. This is not
64
     * always guaranteed to be in order, so take care that
65
     * you actually work out the paths in advance of signing,
66
     * and hard code them somehow.
67
     *
68
     * @return array[] - array of paths
69
     */
70
    public function getPaths()
71
    {
72 38
        return array_map(function (ScriptBranch $branch) {
73 38
            return $branch->getPath();
74 38
        }, $this->descriptorMap);
75
    }
76
77
    /**
78
     * Look up the branch by it's path
79
     *
80
     * @param array $branchDesc
81
     * @return bool|ScriptBranch
82
     */
83 178
    public function getBranchByPath(array $branchDesc)
84
    {
85 178
        $key = json_encode($branchDesc);
86 178
        if (!array_key_exists($key, $this->descriptorMap)) {
87 2
            throw new \RuntimeException("Unknown logical pathway");
88
        }
89
90 176
        return $this->descriptorMap[$key];
91
    }
92
}
93