Completed
Push — master ( 11f27c...4713a5 )
by Andreas
08:33
created

HydratingIterator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 72
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A rewind() 0 4 1
A valid() 0 4 1
A hydrate() 0 4 2
A wrapTraversable() 0 6 2
1
<?php declare(strict_types = 1);
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB\Iterator;
21
22
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
23
use Doctrine\ODM\MongoDB\UnitOfWork;
24
25
/**
26
 * Iterator that wraps a traversable and hydrates results into objects
27
 *
28
 * @internal
29
 */
30
final class HydratingIterator implements \Iterator
31
{
32
    private $iterator;
33
    private $unitOfWork;
34
    private $class;
35
    private $unitOfWorkHints;
36
37 116
    public function __construct(\Traversable $traversable, UnitOfWork $unitOfWork, ClassMetadata $class, array $unitOfWorkHints = [])
38
    {
39 116
        $this->iterator = $this->wrapTraversable($traversable);
40 116
        $this->unitOfWork = $unitOfWork;
41 116
        $this->class = $class;
42 116
        $this->unitOfWorkHints = $unitOfWorkHints;
43 116
    }
44
45
    /**
46
     * @see http://php.net/iterator.current
47
     * @return mixed
48
     */
49 108
    public function current()
50
    {
51 108
        return $this->hydrate($this->iterator->current());
52
    }
53
54
    /**
55
     * @see http://php.net/iterator.mixed
56
     * @return mixed
57
     */
58 116
    public function key()
59
    {
60 116
        return $this->iterator->key();
61
    }
62
63
    /**
64
     * @see http://php.net/iterator.next
65
     * @return void
66
     */
67 65
    public function next()
68
    {
69 65
        $this->iterator->next();
70 65
    }
71
72
    /**
73
     * @see http://php.net/iterator.rewind
74
     * @return void
75
     */
76 115
    public function rewind()
77
    {
78 115
        $this->iterator->rewind();
79 115
    }
80
81
    /**
82
     * @see http://php.net/iterator.valid
83
     * @return boolean
84
     */
85 116
    public function valid()
86
    {
87 116
        return $this->key() !== null;
88
    }
89
90 108
    private function hydrate($document)
91
    {
92 108
        return $document !== null ? $this->unitOfWork->getOrCreateDocument($this->class->name, $document, $this->unitOfWorkHints) : null;
93
    }
94
95 116
    private function wrapTraversable(\Traversable $traversable): \Generator
96
    {
97 116
        foreach ($traversable as $key => $value) {
98 108
            yield $key => $value;
99
        }
100 75
    }
101
}
102