OutputCollectionMutator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 96
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

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