AbstractInventory::getItem()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Messages;
6
7
use BitWasp\Bitcoin\Networking\NetworkSerializable;
8
use BitWasp\Bitcoin\Networking\Structure\Inventory;
9
10
abstract class AbstractInventory extends NetworkSerializable implements \Countable
11
{
12
    /**
13
     * @var Inventory[]
14
     */
15
    private $items = [];
16
17
    /**
18 27
     * @param Inventory[] $vector
19
     */
20 27
    public function __construct(array $vector)
21 21
    {
22 18
        foreach ($vector as $item) {
23 27
            $this->addItem($item);
24
        }
25
    }
26
27
    /**
28 21
     * @param Inventory $item
29
     */
30 21
    private function addItem(Inventory $item)
31 21
    {
32
        $this->items[] = $item;
33
    }
34
35
    /**
36 12
     * @return int
37
     */
38 12
    public function count(): int
39
    {
40
        return count($this->items);
41
    }
42
43
    /**
44 21
     * @return Inventory[]
45
     */
46 21
    public function getItems(): array
47
    {
48
        return $this->items;
49
    }
50
51
    /**
52
     * @param int $index
53 12
     * @return Inventory
54
     */
55 12
    public function getItem(int $index): Inventory
56 6
    {
57
        if (false === isset($this->items[$index])) {
58
            throw new \InvalidArgumentException('No item found at that index');
59 6
        }
60
61
        return $this->items[$index];
62
    }
63
}
64