Completed
Push — master ( 3a7cc6...2fd33a )
by Jacob
8s
created

CursorRecordSet::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace As3\Modlr\Persister\MongoDb;
4
5
use As3\Modlr\Metadata\EntityMetadata;
6
use As3\Modlr\Persister\RecordSetInterface;
7
use As3\Modlr\Store\Store;
8
use Doctrine\MongoDB\Cursor;
9
10
/**
11
 * Represents records from MongoDb via a Cursor.
12
 *
13
 * @author  Jacob Bare <[email protected]>
14
 */
15
class CursorRecordSet implements RecordSetInterface
16
{
17
    /**
18
     * @var Cursor
19
     */
20
    private $cursor;
21
22
    /**
23
     * @var Hydrator
24
     */
25
    private $hydrator;
26
27
    /**
28
     * @var EntityMetadata
29
     */
30
    private $metadata;
31
32
    /**
33
     * @var Store
34
     */
35
    private $store;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param   EntityMetadata  $metadata
41
     * @param   Cursor          $cursor
42
     * @param   Store           $store
43
     * @param   Hydrator        $hydrator
44
     */
45
    public function __construct(EntityMetadata $metadata, Cursor $cursor, Store $store, Hydrator $hydrator)
46
    {
47
        $this->metadata = $metadata;
48
        $this->cursor = $cursor;
49
        $this->hydrator = $hydrator;
50
        $this->store = $store;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function count()
57
    {
58
        return $this->cursor->count(true);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function current()
65
    {
66
        $current = $this->cursor->current();
67
        return $this->hydrator->normalize($this->metadata, $current, $this->store);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function key()
74
    {
75
        return $this->cursor->key();
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getSingleResult()
82
    {
83
        $record = $this->cursor->getSingleResult();
84
        if (!is_array($record)) {
85
            return;
86
        }
87
        return $this->hydrator->normalize($this->metadata, $record, $this->store);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function next()
94
    {
95
        $this->cursor->next();
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function rewind()
102
    {
103
        $this->cursor->rewind();
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function totalCount()
110
    {
111
        return $this->cursor->count(false);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function valid()
118
    {
119
        return $this->cursor->valid();
120
    }
121
}
122