Collection::immutable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Radowoj\Searcher\SearchResult;
4
5
use ArrayAccess;
6
use ArrayIterator;
7
use Countable;
8
use IteratorAggregate;
9
use Exception;
10
use OutOfRangeException;
11
use InvalidArgumentException;
12
13
class Collection implements ICollection, ArrayAccess, IteratorAggregate, Countable
14
{
15
    protected $items = [];
16
17
    protected $totalMatches = 0;
18
19
20 21
    public function __construct(array $items = [], int $totalMatches = 0)
21
    {
22 21
        foreach($items as $item) {
23 10
            if (!$item instanceof IItem) {
24 10
                throw new InvalidArgumentException('Given items must implement Radowoj\Searcher\SearchResult\IItem interface');
25
            }
26
        }
27 21
        $this->items = $items;
28 21
        $this->totalMatches = $totalMatches;
29 21
    }
30
31
32 2
    public function offsetExists($offset)
33
    {
34 2
        return array_key_exists($offset, $this->items);
35
    }
36
37
38 1
    public function offsetGet($offset)
39
    {
40 1
        if (!$this->offsetExists($offset)) {
41 1
            throw new OutOfRangeException("Invalid collection index: {$offset}");
42
        }
43
44 1
        return $this->items[$offset];
45
    }
46
47
48 2
    protected function immutable()
49
    {
50 2
        throw new Exception("Search result collection is immutable.");
51
    }
52
53
54 1
    public function offsetSet($offset, $value)
55
    {
56 1
        return $this->immutable();
57
    }
58
59
60 1
    public function offsetUnset($offset)
61
    {
62 1
        return $this->immutable();
63
    }
64
65
66 1
    public function getIterator()
67
    {
68 1
        return new ArrayIterator($this->items);
69
    }
70
71
72 3
    public function count()
73
    {
74 3
        return count($this->items);
75
    }
76
77
78 1
    public function totalCount()
79
    {
80 1
        return $this->totalMatches;
81
    }
82
83
}
84