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

LazyMetadataIterator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 83
ccs 0
cts 32
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getIterator() 0 9 2
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A current() 0 4 1
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
    public function __construct(callable $factory)
21
    {
22
        $this->factory = $factory;
23
    }
24
25
    private function getIterator()
26
    {
27
        if ($this->iterator === null) {
28
            $factory = $this->factory;
29
            $this->iterator = $factory();
30
        }
31
32
        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
    public function next()
42
    {
43
        $this->getIterator()->next();
44
    }
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
    public function key()
53
    {
54
        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
    public function valid()
65
    {
66
        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
    public function rewind()
76
    {
77
        $this->getIterator()->rewind();
78
    }
79
80
    /**
81
     * @return Metadata
82
     */
83
    public function current()
84
    {
85
        return $this->getIterator()->current();
86
    }
87
}
88