ItemList   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 23
c 1
b 0
f 0
dl 0
loc 69
ccs 24
cts 24
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getDimensionCount() 0 3 1
A validateItem() 0 4 2
A addItem() 0 11 2
A getItems() 0 3 1
1
<?php
2
3
4
namespace Hexogen\KDTree;
5
6
use Hexogen\KDTree\Exception\ValidationException;
7
use Hexogen\KDTree\Interfaces\ItemInterface;
8
use Hexogen\KDTree\Interfaces\ItemListInterface;
9
10
class ItemList implements ItemListInterface
11
{
12
    private $dimensions;
13
    private $items;
14
    private $ids;
15
    private $lastPosition;
16
17
    /**
18
     * ItemList constructor.
19
     * @param int $dimensions
20
     * @throws ValidationException
21
     */
22 78
    public function __construct(int $dimensions)
23
    {
24 78
        if ($dimensions <= 0) {
25 3
            throw new ValidationException('$dimensions should be bigger than 0');
26
        }
27
28 78
        $this->lastPosition = 0;
29 78
        $this->dimensions = $dimensions;
30 78
        $this->items = [];
31 78
        $this->ids = [];
32 78
    }
33
34
    /**
35
     * Add or replace an item in the item list
36
     *
37
     * @api
38
     * @param ItemInterface $item
39
     */
40 63
    public function addItem(ItemInterface $item)
41
    {
42 63
        $this->validateItem($item);
43 60
        $id = $item->getId();
44
45 60
        if (isset($this->ids[$id])) {
46 3
            $index = $this->ids[$id];
47 3
            $this->items[$index] = $item;
48
        } else {
49 60
            $this->items[] = $item;
50 60
            $this->ids[$id] = $this->lastPosition++;
51
        }
52 60
    }
53
54
    /**
55
     * Get all items in the list
56
     * @return ItemInterface[]
57
     */
58 96
    public function getItems(): array
59
    {
60 96
        return $this->items;
61
    }
62
63
    /**
64
     * @return int number of dimensions in item
65
     */
66 96
    public function getDimensionCount(): int
67
    {
68 96
        return $this->dimensions;
69
    }
70
71
    /**
72
     * @param ItemInterface $item
73
     * @throws ValidationException
74
     */
75 63
    private function validateItem(ItemInterface $item)
76
    {
77 63
        if ($item->getDimensionsCount() !== $this->dimensions) {
78 3
            throw new ValidationException('$dValues number dimensions should be equal to ' . $this->dimensions);
79
        }
80 60
    }
81
}
82