Completed
Push — master ( 2d02b0...698bc6 )
by Thibaud
19:46 queued 17:30
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
/*
4
 * This file is part of alchemy/resource-component.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Alchemy\Resource\Metadata;
13
14
class LazyMetadataIterator implements MetadataIterator
15
{
16
    /**
17
     * @var MetadataIterator
18
     */
19
    private $iterator;
20
21
    /**
22
     * @var callable
23
     */
24
    private $factory;
25
26
    /**
27
     * @param callable $factory A callback that returns a MetadataIterator instance
28
     */
29 12
    public function __construct(callable $factory)
30
    {
31 12
        $this->factory = $factory;
32 12
    }
33
34 8
    private function getIterator()
35
    {
36 8
        if ($this->iterator === null) {
37 8
            $factory = $this->factory;
38 8
            $this->iterator = $factory();
39 6
        }
40
41 8
        return $this->iterator;
42
    }
43
44
    /**
45
     * (PHP 5 &gt;= 5.0.0)<br/>
46
     * Move forward to next element
47
     * @link http://php.net/manual/en/iterator.next.php
48
     * @return void Any returned value is ignored.
49
     */
50 4
    public function next()
51
    {
52 4
        $this->getIterator()->next();
53 4
    }
54
55
    /**
56
     * (PHP 5 &gt;= 5.0.0)<br/>
57
     * Return the key of the current element
58
     * @link http://php.net/manual/en/iterator.key.php
59
     * @return mixed scalar on success, or null on failure.
60
     */
61 4
    public function key()
62
    {
63 4
        return $this->getIterator()->key();
64
    }
65
66
    /**
67
     * (PHP 5 &gt;= 5.0.0)<br/>
68
     * Checks if current position is valid
69
     * @link http://php.net/manual/en/iterator.valid.php
70
     * @return boolean The return value will be casted to boolean and then evaluated.
71
     * Returns true on success or false on failure.
72
     */
73 8
    public function valid()
74
    {
75 8
        return $this->getIterator()->valid();
76
    }
77
78
    /**
79
     * (PHP 5 &gt;= 5.0.0)<br/>
80
     * Rewind the Iterator to the first element
81
     * @link http://php.net/manual/en/iterator.rewind.php
82
     * @return void Any returned value is ignored.
83
     */
84 8
    public function rewind()
85
    {
86 8
        $this->getIterator()->rewind();
87 8
    }
88
89
    /**
90
     * @return Metadata
91
     */
92 4
    public function current()
93
    {
94 4
        return $this->getIterator()->current();
95
    }
96
}
97