Completed
Pull Request — master (#523)
by thomas
73:16
created

Operation::encode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 6
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 2566
    public function __construct($opCode, BufferInterface $pushData, $pushDataSize = 0)
37
    {
38 2566
        $this->push = $opCode >= 0 && $opCode <= Opcodes::OP_PUSHDATA4;
39 2566
        $this->opCode = $opCode;
40 2566
        $this->pushData = $pushData;
41 2566
        $this->pushDataSize = $pushDataSize;
42 2566
    }
43
44
    /**
45
     * @return bool
46
     */
47 2528
    public function isPush()
48
    {
49 2528
        return $this->push;
50
    }
51
52
    /**
53
     * @return int
54
     */
55 2546
    public function getOp()
56
    {
57 2546
        return $this->opCode;
58
    }
59
60
    /**
61
     * @return BufferInterface
62
     */
63 2532
    public function getData()
64
    {
65 2532
        return $this->pushData;
66
    }
67
68
    /**
69
     * @return int
70
     */
71 1900
    public function getDataSize()
72
    {
73 1900
        if (!$this->push) {
74
            throw new \RuntimeException("Op wasn't a push operation");
75
        }
76
77 1900
        return $this->pushDataSize;
78
    }
79
80
    /**
81
     * @return BufferInterface|int
82
     */
83
    public function encode()
84
    {
85
        if ($this->push) {
86
            return $this->pushData;
87
        } else {
88
            return $this->opCode;
89
        }
90
    }
91
}
92