Completed
Push — master ( fceb66...595279 )
by thomas
73:29 queued 71:27
created

Operation::encode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
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 bool
12
     */
13
    private $push;
14
15
    /**
16
     * @var int
17
     */
18
    private $opCode;
19
20
    /**
21
     * @var BufferInterface
22
     */
23
    private $pushData;
24
25
    /**
26
     * @var int
27
     */
28
    private $pushDataSize;
29
30
    /**
31
     * Operation constructor.
32
     * @param int $opCode
33
     * @param BufferInterface $pushData
34
     * @param int $pushDataSize
35
     */
36 2594
    public function __construct($opCode, BufferInterface $pushData, $pushDataSize = 0)
37
    {
38 2594
        $this->push = $opCode >= 0 && $opCode <= Opcodes::OP_PUSHDATA4;
39 2594
        $this->opCode = $opCode;
40 2594
        $this->pushData = $pushData;
41 2594
        $this->pushDataSize = $pushDataSize;
42 2594
    }
43
44
    /**
45
     * @return bool
46
     */
47 2556
    public function isPush()
48
    {
49 2556
        return $this->push;
50
    }
51
52
    /**
53
     * @return int
54
     */
55 2574
    public function getOp()
56
    {
57 2574
        return $this->opCode;
58
    }
59
60
    /**
61
     * @return BufferInterface
62
     */
63 2560
    public function getData()
64
    {
65 2560
        return $this->pushData;
66
    }
67
68
    /**
69
     * @return int
70
     */
71 1932
    public function getDataSize()
72
    {
73 1932
        if (!$this->push) {
74
            throw new \RuntimeException("Op wasn't a push operation");
75
        }
76
77 1932
        return $this->pushDataSize;
78
    }
79
80
    /**
81
     * @return BufferInterface|int
82
     */
83 2
    public function encode()
84
    {
85 2
        if ($this->push) {
86 2
            return $this->pushData;
87
        } else {
88
            return $this->opCode;
89
        }
90
    }
91
}
92