Completed
Push — master ( 838d3a...3b658f )
by thomas
22:41 queued 01:10
created

OutputCollectionMutator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.29%

Importance

Changes 8
Bugs 2 Features 1
Metric Value
wmc 13
c 8
b 2
f 1
lcom 1
cbo 3
dl 0
loc 98
ccs 33
cts 35
cp 0.9429
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A current() 0 4 1
A offsetGet() 0 8 2
A done() 0 9 2
A slice() 0 10 3
A null() 0 5 1
A add() 0 8 1
A set() 0 5 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction\Mutator;
4
5
use BitWasp\Bitcoin\Collection\MutableCollection;
6
use BitWasp\Bitcoin\Collection\Transaction\TransactionOutputCollection;
7
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
8
9
class OutputCollectionMutator extends MutableCollection
10
{
11
    /**
12
     * @param TransactionOutputInterface[] $outputs
13
     */
14 135
    public function __construct(array $outputs)
15
    {
16
        /** @var OutputMutator[] $set */
17 135
        $this->set = new \SplFixedArray(count($outputs));
18 135
        foreach ($outputs as $i => $output) {
19
            /** @var int $i */
20 72
            $this->set[$i] = new OutputMutator($output);
21 135
        }
22 135
    }
23
24
    /**
25
     * @return OutputMutator
26
     */
27
    public function current()
28
    {
29
        return $this->set->current();
30
    }
31
32
    /**
33
     * @param int $offset
34
     * @return OutputMutator
35
     */
36 18
    public function offsetGet($offset)
37
    {
38 18
        if (!$this->set->offsetExists($offset)) {
39 6
            throw new \OutOfRangeException('Nothing found at this offset');
40
        }
41
42 12
        return $this->set->offsetGet($offset);
43
    }
44
45
    /**
46
     * @return TransactionOutputCollection
47
     */
48 117
    public function done()
49
    {
50 117
        $set = [];
51 117
        foreach ($this->set as $mutator) {
52 60
            $set[] = $mutator->done();
53 117
        }
54
55 117
        return new TransactionOutputCollection($set);
56
    }
57
58
    /**
59
     * @param int|string $start
60
     * @param int|string $length
61
     * @return $this
62
     */
63 30
    public function slice($start, $length)
64
    {
65 30
        $end = count($this->set);
66 30
        if ($start > $end || $length > $end) {
67 6
            throw new \RuntimeException('Invalid start or length');
68
        }
69
70 24
        $this->set = \SplFixedArray::fromArray(array_slice($this->set->toArray(), $start, $length));
71 24
        return $this;
72
    }
73
74
    /**
75
     * @return $this
76
     */
77 12
    public function null()
78
    {
79 12
        $this->slice(0, 0);
80 12
        return $this;
81
    }
82
83
    /**
84
     * @param TransactionOutputInterface $output
85
     * @return $this
86
     */
87 6
    public function add(TransactionOutputInterface $output)
88
    {
89 6
        $size = $this->set->getSize();
90 6
        $this->set->setSize($size + 1);
91
92 6
        $this->set[$size] = new OutputMutator($output);
93 6
        return $this;
94
    }
95
96
    /**
97
     * @param int $i
98
     * @param TransactionOutputInterface $output
99
     * @return $this
100
     */
101 6
    public function set($i, TransactionOutputInterface $output)
102
    {
103 6
        $this->set[$i] = new OutputMutator($output);
104 6
        return $this;
105
    }
106
}
107