Result   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 172
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setCursor() 0 4 1
A setCollection() 0 4 1
A rewind() 0 4 1
A valid() 0 4 1
A key() 0 4 1
A next() 0 4 1
A current() 0 16 2
A count() 0 4 1
A sort() 0 6 1
A hint() 0 6 1
A limit() 0 6 1
A skip() 0 6 1
1
<?php
2
/*
3
 MIT License
4
 Copyright (c) 2010 - 2018 Peter Petermann
5
6
 Permission is hereby granted, free of charge, to any person
7
 obtaining a copy of this software and associated documentation
8
 files (the "Software"), to deal in the Software without
9
 restriction, including without limitation the rights to use,
10
 copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 copies of the Software, and to permit persons to whom the
12
 Software is furnished to do so, subject to the following
13
 conditions:
14
15
 The above copyright notice and this permission notice shall be
16
 included in all copies or substantial portions of the Software.
17
18
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
 OTHER DEALINGS IN THE SOFTWARE.
26
27
*/
28
namespace King23\Mongo;
29
30
use Psr\Container\ContainerInterface;
31
32
class Result implements \Iterator, \Countable
33
{
34
    /**
35
     * @var \MongoCursor
36
     */
37
    protected $myResultCursor = null;
38
39
    /**
40
     * @var string
41
     */
42
    private $collection;
43
    /**
44
     * @var ContainerInterface
45
     */
46
    private $container;
47
    /**
48
     * @var ClassMapInterface
49
     */
50
    private $classMapInterface;
51
52
    /**
53
     * @param ContainerInterface $container
54
     * @param ClassMapInterface $classMapInterface
55
     */
56
    public function __construct(
57
        ContainerInterface $container,
58
        ClassMapInterface $classMapInterface
59
    ) {
60
        $this->container = $container;
61
        $this->classMapInterface = $classMapInterface;
62
    }
63
64
    /**
65
     * @param \MongoCursor $cursor
66
     */
67
    public function setCursor($cursor)
68
    {
69
        $this->myResultCursor = $cursor;
70
    }
71
72
    /**
73
     * @param string $collection
74
     */
75
    public function setCollection($collection)
76
    {
77
        $this->collection = $collection;
78
    }
79
80
    /**
81
     * Iterator::rewind
82
     */
83
    public function rewind()
84
    {
85
        $this->myResultCursor->rewind();
86
    }
87
88
    /**
89
     * Iterator::valid
90
     *
91
     * @return bool
92
     */
93
    public function valid()
94
    {
95
        return $this->myResultCursor->valid();
96
    }
97
98
    /**
99
     * Iterator::key
100
     *
101
     * @return string key
102
     */
103
    public function key()
104
    {
105
        return $this->myResultCursor->key();
106
    }
107
108
    /**
109
     * Iterator::next
110
     */
111
    public function next()
112
    {
113
        $this->myResultCursor->next();
114
    }
115
116
    /**
117
     * Iterator::current
118
     * return current specific object
119
     *
120
     * @return MongoObject
121
     * @throws Exception
122
     * @throws \MongoException
123
     */
124
    public function current()
125
    {
126
        $documentData = $this->myResultCursor->current();
127
        if (is_null($documentData)) {
128
            return null; 
129
        }
130
        /** @var MongoObject $document */
131
        $document = $this->container->get(
132
            $this->classMapInterface->getClassForResult($this->collection, $documentData)
133
        );
134
135
        $document->setCollection($this->collection);
136
        $document->loadFromArray($documentData);
137
138
        return $document;
139
    }
140
141
    /**
142
     * count method on the cursor, allows to get result count
143
     *
144
     * @param bool $foundOnly
145
     * @return int
146
     */
147
    public function count($foundOnly = false)
148
    {
149
        return $this->myResultCursor->count($foundOnly);
150
    }
151
152
    /**
153
     * @return Result a sort on the cursor
154
     * @param array $sortoptions
155
     * @returns Result
156
     * @throws \MongoCursorException
157
     */
158
    public function sort(array $sortoptions)
159
    {
160
        $this->myResultCursor = $this->myResultCursor->sort($sortoptions);
161
162
        return $this;
163
    }
164
165
    /**
166
     * @param array $hintoptions
167
     * @return Result
168
     * @throws \MongoCursorException
169
     */
170
    public function hint(array $hintoptions)
171
    {
172
        $this->myResultCursor = $this->myResultCursor->hint($hintoptions);
173
174
        return $this;
175
    }
176
177
    /**
178
     * @param  $amount
179
     * @return Result
180
     *
181
     * @throws \MongoCursorException
182
     */
183
    public function limit($amount)
184
    {
185
        $this->myResultCursor = $this->myResultCursor->limit($amount);
186
187
        return $this;
188
    }
189
190
    /**
191
     * Skip the first $num results
192
     *
193
     * @param integer $num
194
     * @return Result
195
     * @throws \MongoCursorException
196
     */
197
    public function skip($num)
198
    {
199
        $this->myResultCursor->skip($num);
200
201
        return $this;
202
    }
203
}
204