Completed
Pull Request — master (#1)
by Thibaud
02:49
created

LazyMetadataIterator::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 9.6666
1
<?php
2
3
namespace Alchemy\Resource\Metadata;
4
5
class LazyMetadataIterator implements MetadataIterator
6
{
7
    /**
8
     * @var MetadataIterator
9
     */
10
    private $iterator;
11
12
    /**
13
     * @var callable
14
     */
15
    private $factory;
16
17
    /**
18
     * @param callable $factory A callback that returns a MetadataIterator instance
19
     */
20 12
    public function __construct(callable $factory)
21
    {
22 12
        $this->factory = $factory;
23 12
    }
24
25 8
    private function getIterator()
26
    {
27 8
        if ($this->iterator === null) {
28 8
            $factory = $this->factory;
29 8
            $this->iterator = $factory();
30 6
        }
31
32 8
        return $this->iterator;
33
    }
34
35
    /**
36
     * (PHP 5 &gt;= 5.0.0)<br/>
37
     * Move forward to next element
38
     * @link http://php.net/manual/en/iterator.next.php
39
     * @return void Any returned value is ignored.
40
     */
41 4
    public function next()
42
    {
43 4
        $this->getIterator()->next();
44 4
    }
45
46
    /**
47
     * (PHP 5 &gt;= 5.0.0)<br/>
48
     * Return the key of the current element
49
     * @link http://php.net/manual/en/iterator.key.php
50
     * @return mixed scalar on success, or null on failure.
51
     */
52 4
    public function key()
53
    {
54 4
        return $this->getIterator()->key();
55
    }
56
57
    /**
58
     * (PHP 5 &gt;= 5.0.0)<br/>
59
     * Checks if current position is valid
60
     * @link http://php.net/manual/en/iterator.valid.php
61
     * @return boolean The return value will be casted to boolean and then evaluated.
62
     * Returns true on success or false on failure.
63
     */
64 8
    public function valid()
65
    {
66 8
        return $this->getIterator()->valid();
67
    }
68
69
    /**
70
     * (PHP 5 &gt;= 5.0.0)<br/>
71
     * Rewind the Iterator to the first element
72
     * @link http://php.net/manual/en/iterator.rewind.php
73
     * @return void Any returned value is ignored.
74
     */
75 8
    public function rewind()
76
    {
77 8
        $this->getIterator()->rewind();
78 8
    }
79
80
    /**
81
     * @return Metadata
82
     */
83 4
    public function current()
84
    {
85 4
        return $this->getIterator()->current();
86
    }
87
}
88