Completed
Push — master ( ac7d60...f9cacf )
by Denis
04:30
created

CollectionIterator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 80
ccs 0
cts 16
cp 0
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A current() 0 4 1
A next() 0 4 1
A valid() 0 6 2
A key() 0 4 1
A rewind() 0 6 1
1
<?php
2
3
namespace Lan\Ebs\Sdk\Classes;
4
5
use ArrayIterator;
6
7
class CollectionIterator extends ArrayIterator
8
{
9
    /**
10
     * @var Collection
11
     */
12
    private $collection;
13
14
    private $data = [];
15
16
    /**
17
     * CollectionIterator constructor.
18
     *
19
     * @param Collection $collection
20
     */
21
    public function __construct(Collection $collection)
22
    {
23
        $this->collection = $collection;
24
    }
25
26
    /**
27
     * Return the current element
28
     *
29
     * @link   http://php.net/manual/en/iterator.current.php
30
     * @return Model
31
     * @throws \Exception
32
     */
33
    public function current()
34
    {
35
        return $this->collection->createModel(current($this->data));
36
    }
37
38
    /**
39
     * Move forward to next element
40
     *
41
     * @link   http://php.net/manual/en/iterator.next.php
42
     * @return Model
43
     */
44
    public function next()
45
    {
46
        return next($this->data);
47
    }
48
49
    /**
50
     * Checks if current position is valid
51
     *
52
     * @link   http://php.net/manual/en/iterator.valid.php
53
     * @return boolean
54
     */
55
    public function valid()
56
    {
57
        $key = $this->key();
58
59
        return ($key !== null && $key !== false);
60
    }
61
62
    /**
63
     * Return the key of the current element
64
     *
65
     * @link   http://php.net/manual/en/iterator.key.php
66
     * @return integer
67
     */
68
    public function key()
69
    {
70
        return key($this->data);
71
    }
72
73
    /**
74
     * Rewind the Iterator to the first element
75
     *
76
     * @link   http://php.net/manual/en/iterator.rewind.php
77
     * @return void
78
     * @throws \Exception
79
     */
80
    public function rewind()
81
    {
82
        $this->data = $this->collection->load()->getData();
83
84
        reset($this->data);
85
    }
86
}