Completed
Pull Request — master (#339)
by thomas
104:00 queued 33:35
created

ScriptFactory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 95.24%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 87
ccs 20
cts 21
cp 0.9524
rs 10
wmc 15
lcom 0
cbo 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fromHex() 0 4 2
A create() 0 4 3
A sequence() 0 4 1
A scriptPubKey() 0 4 1
A p2sh() 0 4 2
A info() 0 4 1
A getNativeConsensus() 0 4 2
A getBitcoinConsensus() 0 4 1
A consensus() 0 8 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Script;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
7
use BitWasp\Bitcoin\Math\Math;
8
use BitWasp\Bitcoin\Script\Consensus\BitcoinConsensus;
9
use BitWasp\Bitcoin\Script\Consensus\NativeConsensus;
10
use BitWasp\Bitcoin\Script\Factory\OutputScriptFactory;
11
use BitWasp\Bitcoin\Script\Factory\P2shScriptFactory;
12
use BitWasp\Bitcoin\Script\Factory\ScriptCreator;
13
use BitWasp\Bitcoin\Script\Factory\ScriptInfoFactory;
14
use BitWasp\Buffertools\Buffer;
15
use BitWasp\Buffertools\BufferInterface;
16
17
class ScriptFactory
18
{
19
    /**
20
     * @param BufferInterface|string $string
21
     * @return Script
22
     */
23
    public static function fromHex($string)
24 24
    {
25
        return self::create($string instanceof BufferInterface ? $string : Buffer::hex($string))->getScript();
26 24
    }
27
28
    /**
29
     * @param BufferInterface|null $buffer
30
     * @param Opcodes|null $opcodes
31
     * @param Math|null $math
32
     * @return ScriptCreator
33
     */
34
    public static function create(BufferInterface $buffer = null, Opcodes $opcodes = null, Math $math = null)
35 2428
    {
36
        return new ScriptCreator($math ?: Bitcoin::getMath(), $opcodes ?: new Opcodes(), $buffer);
37 2428
    }
38
39
    /**
40
     * @param int[]|\BitWasp\Bitcoin\Script\Interpreter\Number[]|BufferInterface[] $sequence
41
     * @return ScriptInterface
42
     */
43
    public static function sequence(array $sequence)
44 2338
    {
45
        return self::create()->sequence($sequence)->getScript();
46 2338
    }
47
48
    /**
49
     * @return OutputScriptFactory
50
     */
51
    public static function scriptPubKey()
52 126
    {
53
        return new OutputScriptFactory();
54 126
    }
55
56
    /**
57
     * @param Opcodes|null $opcodes
58
     * @return P2shScriptFactory
59
     */
60
    public static function p2sh(Opcodes $opcodes = null)
61 21
    {
62
        return new P2shScriptFactory(self::scriptPubKey(), $opcodes ?: new Opcodes());
63 21
    }
64
65
    /**
66
     * @param ScriptInterface $script
67
     * @return ScriptInfo\ScriptInfoInterface
68
     */
69
    public static function info(ScriptInterface $script)
70 3
    {
71
        return (new ScriptInfoFactory())->load($script);
72 3
    }
73
74
    /**
75
     * @param EcAdapterInterface|null $ecAdapter
76
     * @return NativeConsensus
77
     */
78 6
    public static function getNativeConsensus(EcAdapterInterface $ecAdapter = null)
79
    {
80
        return new NativeConsensus($ecAdapter ?: Bitcoin::getEcAdapter());
81 6
    }
82 6
83 6
    /**
84 6
     * @return BitcoinConsensus
85 4
     */
86
    public static function getBitcoinConsensus()
87
    {
88
        return new BitcoinConsensus();
89
    }
90
91
    /**
92
     * @param EcAdapterInterface|null $ecAdapter
93 33
     * @return \BitWasp\Bitcoin\Script\Consensus\ConsensusInterface
94
     */
95 33
    public static function consensus(EcAdapterInterface $ecAdapter = null)
96
    {
97
        if (extension_loaded('bitcoinconsensus')) {
98
            return self::getBitcoinConsensus();
99
        } else {
100
            return self::getNativeConsensus($ecAdapter);
101
        }
102
    }
103
}
104