Passed
Push — master ( 61b2f8...584670 )
by Chubarov
05:03
created

ProductCollection   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A valid() 0 3 1
A __construct() 0 4 2
A offsetExists() 0 3 1
A offsetGet() 0 3 1
A offsetSet() 0 6 2
A rewind() 0 3 1
A offsetUnset() 0 3 1
A current() 0 3 1
A count() 0 3 1
A next() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Fns;
5
6
class ProductCollection implements \Countable, \Iterator, \ArrayAccess
7
{
8
    private $products;
9
    private $position;
10
11
    public function __construct(array $productCandidates = [])
12
    {
13
        foreach ($productCandidates as $candidate) {
14
            $this->offsetSet('', new Product(json_decode(json_encode($candidate), true)));
15
        }
16
    }
17
18
    public function count()
19
    {
20
        return count($this->products);
21
    }
22
23
    public function current()
24
    {
25
        return $this->products[$this->position];
26
    }
27
28
    public function next()
29
    {
30
        $this->position++;
31
    }
32
33
    public function key()
34
    {
35
        return $this->position;
36
    }
37
38
    public function rewind() : void
39
    {
40
        $this->position = 0;
41
    }
42
43
    public function valid()
44
    {
45
        return isset($this->products[$this->position]);
46
    }
47
48
    public function offsetExists($offset) : bool
49
    {
50
        return isset($this->products[$offset]);
51
    }
52
53
    public function offsetGet($offset) : mixed
54
    {
55
        return $this->products[$offset];
56
    }
57
58
    public function offsetSet($offset, $product)
59
    {
60
        if (empty($offset)) {
61
            $this->products[] = $product;
62
        } else {
63
            $this->products[$offset] = $product;
64
        }
65
    }
66
67
    public function offsetUnset($offset)
68
    {
69
        unset($this->products[$offset]);
70
    }
71
}
72