Completed
Pull Request — master (#524)
by thomas
71:45
created

Operation::encode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 2
cts 2
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Parser;
4
5
use BitWasp\Bitcoin\Script\Opcodes;
6
use BitWasp\Buffertools\BufferInterface;
7
8
class Operation
9
{
10
    /**
11
     * @var int[]
12
     */
13
    protected static $logical = [
14
        Opcodes::OP_IF, Opcodes::OP_NOTIF, Opcodes::OP_ELSE, Opcodes::OP_ENDIF,
15
    ];
16
17
    /**
18
     * @var bool
19
     */
20
    private $push;
21
22
    /**
23
     * @var int
24
     */
25
    private $opCode;
26
27
    /**
28
     * @var BufferInterface
29
     */
30
    private $pushData;
31
32
    /**
33
     * @var int
34
     */
35
    private $pushDataSize;
36 2566
37
    /**
38 2566
     * Operation constructor.
39 2566
     * @param int $opCode
40 2566
     * @param BufferInterface $pushData
41 2566
     * @param int $pushDataSize
42 2566
     */
43
    public function __construct($opCode, BufferInterface $pushData, $pushDataSize = 0)
44
    {
45
        $this->push = $opCode >= 0 && $opCode <= Opcodes::OP_PUSHDATA4;
46
        $this->opCode = $opCode;
47 2528
        $this->pushData = $pushData;
48
        $this->pushDataSize = $pushDataSize;
49 2528
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function encode()
55 2546
    {
56
        if ($this->push) {
57 2546
            return $this->pushData;
58
        } else {
59
            return $this->opCode;
60
        }
61
    }
62
63 2532
    /**
64
     * @return bool
65 2532
     */
66
    public function isPush()
67
    {
68
        return $this->push;
69
    }
70
71 1900
    /**
72
     * @return bool
73 1900
     */
74
    public function isLogical()
75
    {
76
        return !$this->isPush() && in_array($this->opCode, self::$logical);
77 1900
    }
78
79
80
    /**
81
     * @return int
82
     */
83
    public function getOp()
84
    {
85
        return $this->opCode;
86
    }
87
88
    /**
89
     * @return BufferInterface
90
     */
91
    public function getData()
92
    {
93
        return $this->pushData;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function getDataSize()
100
    {
101
        if (!$this->push) {
102
            throw new \RuntimeException("Op wasn't a push operation");
103
        }
104
105
        return $this->pushDataSize;
106
    }
107
}
108