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

PrimingIterator::primeReferences()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 0
crap 5
1
<?php declare(strict_types = 1);
2
3
namespace Doctrine\ODM\MongoDB\Iterator;
4
5
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
6
use Doctrine\ODM\MongoDB\Query\ReferencePrimer;
7
8
final class PrimingIterator implements Iterator
9
{
10
    private $iterator;
11
    private $class;
12
    private $referencePrimer;
13
    private $primers;
14
    private $unitOfWorkHints;
15
    private $referencesPrimed = false;
16
17 22
    public function __construct(\Iterator $iterator, ClassMetadata $class, ReferencePrimer $referencePrimer, array $primers, array $unitOfWorkHints = [])
18
    {
19 22
        $this->iterator = $iterator;
20 22
        $this->class = $class;
21 22
        $this->referencePrimer = $referencePrimer;
22 22
        $this->primers = $primers;
23 22
        $this->unitOfWorkHints = $unitOfWorkHints;
24 22
    }
25
26 9
    public function toArray(): array
27
    {
28 9
        return iterator_to_array($this);
29
    }
30
31 22
    public function current()
32
    {
33 22
        $this->primeReferences();
34
35 20
        return $this->iterator->current();
36
    }
37
38 20
    public function next()
39
    {
40 20
        $this->iterator->next();
41 20
    }
42
43 9
    public function key()
44
    {
45 9
        return $this->iterator->key();
46
    }
47
48 22
    public function valid()
49
    {
50 22
        return $this->iterator->valid();
51
    }
52
53 22
    public function rewind()
54
    {
55 22
        $this->iterator->rewind();
56 22
    }
57
58 22
    private function primeReferences()
59
    {
60 22
        if ($this->referencesPrimed || empty($this->primers)) {
61 20
            return;
62
        }
63
64 22
        $this->referencesPrimed = true;
65
66 22
        foreach ($this->primers as $fieldName => $primer) {
67 22
            $primer = is_callable($primer) ? $primer : null;
68 22
            $this->referencePrimer->primeReferences($this->class, $this, $fieldName, $this->unitOfWorkHints, $primer);
69
        }
70
71 20
        $this->rewind();
72 20
    }
73
}
74