Completed
Pull Request — master (#13)
by Andreas
02:43
created

MongoCollection::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
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
16
use Alcaeus\MongoDbAdapter\Helper;
17
use Alcaeus\MongoDbAdapter\TypeConverter;
18
19
/**
20
 * Represents a database collection.
21
 * @link http://www.php.net/manual/en/class.mongocollection.php
22
 */
23
class MongoCollection
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    use Helper\ReadPreference;
26
    use Helper\WriteConcern;
27
28
    const ASCENDING = 1;
29
    const DESCENDING = -1;
30
31
    /**
32
     * @var MongoDB
33
     */
34
    public $db = NULL;
35
36
    /**
37
     * @var string
38
     */
39
    protected $name;
40
41
    /**
42
     * @var \MongoDB\Collection
43
     */
44
    protected $collection;
45
46
    /**
47
     * Creates a new collection
48
     * @link http://www.php.net/manual/en/mongocollection.construct.php
49
     * @param MongoDB $db Parent database.
50
     * @param string $name Name for this collection.
51
     * @throws Exception
52
     * @return MongoCollection
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
53
     */
54
    public function __construct(MongoDB $db, $name)
55
    {
56
        $this->db = $db;
57
        $this->name = $name;
58
59
        $this->setReadPreferenceFromArray($db->getReadPreference());
60
        $this->setWriteConcernFromArray($db->getWriteConcern());
61
62
        $this->createCollectionObject();
63
    }
64
65
    /**
66
     * Gets the underlying collection for this object
67
     *
68
     * @internal This part is not of the ext-mongo API and should not be used
69
     * @return \MongoDB\Collection
70
     */
71
    public function getCollection()
72
    {
73
        return $this->collection;
74
    }
75
76
    /**
77
     * String representation of this collection
78
     * @link http://www.php.net/manual/en/mongocollection.--tostring.php
79
     * @return string Returns the full name of this collection.
80
     */
81
    public function __toString()
82
    {
83
        return (string) $this->db . '.' . $this->name;
84
    }
85
86
    /**
87
     * Gets a collection
88
     * @link http://www.php.net/manual/en/mongocollection.get.php
89
     * @param string $name The next string in the collection name.
90
     * @return MongoCollection
91
     */
92
    public function __get($name)
93
    {
94
        // Handle w and wtimeout properties that replicate data stored in $readPreference
95
        if ($name === 'w' || $name === 'wtimeout') {
96
            return $this->getWriteConcern()[$name];
97
        }
98
99
        return $this->db->selectCollection($this->name . '.' . $name);
100
    }
101
102
    /**
103
     * @param string $name
104
     * @param mixed $value
105
     */
106 View Code Duplication
    public function __set($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        if ($name === 'w' || $name === 'wtimeout') {
109
            $this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern());
110
            $this->createCollectionObject();
111
        }
112
    }
113
114
    /**
115
     * @link http://www.php.net/manual/en/mongocollection.aggregate.php
116
     * @param array $pipeline
117
     * @param array $op
118
     * @return array
119
     */
120
    public function aggregate(array $pipeline, array $op = [])
121
    {
122
        if (! TypeConverter::isNumericArray($pipeline)) {
123
            $pipeline = [];
124
            $options = [];
125
126
            $i = 0;
127
            foreach (func_get_args() as $operator) {
128
                $i++;
129
                if (! is_array($operator)) {
130
                    trigger_error("Argument $i is not an array", E_WARNING);
131
                    return;
132
                }
133
134
                $pipeline[] = $operator;
135
            }
136
        } else {
137
            $options = $op;
138
        }
139
140
        $command = [
141
            'aggregate' => $this->name,
142
            'pipeline' => $pipeline
143
        ];
144
145
        $command += $options;
146
147
        return $this->db->command($command, [], $hash);
148
    }
149
150
    /**
151
     * @link http://php.net/manual/en/mongocollection.aggregatecursor.php
152
     * @param array $pipeline
153
     * @param array $options
154
     * @return MongoCommandCursor
155
     */
156
    public function aggregateCursor(array $pipeline, array $options = [])
157
    {
158
        // Build command manually, can't use mongo-php-library here
159
        $command = [
160
            'aggregate' => $this->name,
161
            'pipeline' => $pipeline
162
        ];
163
164
        // Convert cursor option
165
        if (! isset($options['cursor']) || $options['cursor'] === true || $options['cursor'] === []) {
166
            // Cursor option needs to be an object convert bools and empty arrays since those won't be handled by TypeConverter
167
            $options['cursor'] = new \stdClass;
168
        }
169
170
        $command += $options;
171
172
        $cursor = new MongoCommandCursor($this->db->getConnection(), (string)$this, $command);
173
        $cursor->setReadPreference($this->getReadPreference());
0 ignored issues
show
Documentation introduced by
$this->getReadPreference() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
174
175
        return $cursor;
176
    }
177
178
    /**
179
     * Returns this collection's name
180
     * @link http://www.php.net/manual/en/mongocollection.getname.php
181
     * @return string
182
     */
183
    public function getName()
184
    {
185
        return $this->name;
186
    }
187
188
    /**
189
     * @link http://www.php.net/manual/en/mongocollection.getslaveokay.php
190
     * @return bool
191
     */
192
    public function getSlaveOkay()
193
    {
194
        $this->notImplemented();
195
    }
196
197
    /**
198
     * @link http://www.php.net/manual/en/mongocollection.setslaveokay.php
199
     * @param bool $ok
200
     * @return bool
201
     */
202
    public function setSlaveOkay($ok = true)
0 ignored issues
show
Unused Code introduced by
The parameter $ok is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
    {
204
        $this->notImplemented();
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function setReadPreference($readPreference, $tags = null)
211
    {
212
        $result = $this->setReadPreferenceFromParameters($readPreference, $tags);
213
        $this->createCollectionObject();
214
215
        return $result;
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221
    public function setWriteConcern($wstring, $wtimeout = 0)
222
    {
223
        $result = $this->setWriteConcernFromParameters($wstring, $wtimeout);
224
        $this->createCollectionObject();
225
226
        return $result;
227
    }
228
229
    /**
230
     * Drops this collection
231
     * @link http://www.php.net/manual/en/mongocollection.drop.php
232
     * @return array Returns the database response.
233
     */
234
    public function drop()
235
    {
236
        return $this->collection->drop();
237
    }
238
239
    /**
240
     * Validates this collection
241
     * @link http://www.php.net/manual/en/mongocollection.validate.php
242
     * @param bool $scan_data Only validate indices, not the base collection.
243
     * @return array Returns the database's evaluation of this object.
244
     */
245
    public function validate($scan_data = FALSE)
0 ignored issues
show
Unused Code introduced by
The parameter $scan_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
246
    {
247
        $this->notImplemented();
248
    }
249
250
    /**
251
     * Inserts an array into the collection
252
     * @link http://www.php.net/manual/en/mongocollection.insert.php
253
     * @param array|object $a
254
     * @param array $options
255
     * @throws MongoException if the inserted document is empty or if it contains zero-length keys. Attempting to insert an object with protected and private properties will cause a zero-length key error.
256
     * @throws MongoCursorException if the "w" option is set and the write fails.
257
     * @throws MongoCursorTimeoutException if the "w" option is set to a value greater than one and the operation takes longer than MongoCursor::$timeout milliseconds to complete. This does not kill the operation on the server, it is a client-side timeout. The operation in MongoCollection::$wtimeout is milliseconds.
258
     * @return bool|array Returns an array containing the status of the insertion if the "w" option is set.
259
     */
260
    public function insert($a, array $options = array())
261
    {
262
        return $this->collection->insertOne(TypeConverter::convertLegacyArrayToObject($a), $options);
263
    }
264
265
    /**
266
     * Inserts multiple documents into this collection
267
     * @link http://www.php.net/manual/en/mongocollection.batchinsert.php
268
     * @param array $a An array of arrays.
269
     * @param array $options Options for the inserts.
270
     * @throws MongoCursorException
271
     * @return mixed f "safe" is set, returns an associative array with the status of the inserts ("ok") and any error that may have occured ("err"). Otherwise, returns TRUE if the batch insert was successfully sent, FALSE otherwise.
272
     */
273
    public function batchInsert(array $a, array $options = array())
274
    {
275
        return $this->collection->insertMany($a, $options);
276
    }
277
278
    /**
279
     * Update records based on a given criteria
280
     * @link http://www.php.net/manual/en/mongocollection.update.php
281
     * @param array $criteria Description of the objects to update.
282
     * @param array $newobj The object with which to update the matching records.
283
     * @param array $options This parameter is an associative array of the form
284
     *        array("optionname" => boolean, ...).
285
     *
286
     *        Currently supported options are:
287
     *          "upsert": If no document matches $$criteria, a new document will be created from $$criteria and $$new_object (see upsert example).
288
     *
289
     *          "multiple": All documents matching $criteria will be updated. MongoCollection::update has exactly the opposite behavior of MongoCollection::remove- it updates one document by
290
     *          default, not all matching documents. It is recommended that you always specify whether you want to update multiple documents or a single document, as the
291
     *          database may change its default behavior at some point in the future.
292
     *
293
     *          "safe" Can be a boolean or integer, defaults to false. If false, the program continues executing without waiting for a database response. If true, the program will wait for
294
     *          the database response and throw a MongoCursorException if the update did not succeed. If you are using replication and the master has changed, using "safe" will make the driver
295
     *          disconnect from the master, throw and exception, and attempt to find a new master on the next operation (your application must decide whether or not to retry the operation on the new master).
296
     *          If you do not use "safe" with a replica set and the master changes, there will be no way for the driver to know about the change so it will continuously and silently fail to write.
297
     *          If safe is an integer, will replicate the update to that many machines before returning success (or throw an exception if the replication times out, see wtimeout).
298
     *          This overrides the w variable set on the collection.
299
     *
300
     *         "fsync": Boolean, defaults to false. Forces the update to be synced to disk before returning success. If true, a safe update is implied and will override setting safe to false.
301
     *
302
     *         "timeout" Integer, defaults to MongoCursor::$timeout. If "safe" is set, this sets how long (in milliseconds) for the client to wait for a database response. If the database does
303
     *         not respond within the timeout period, a MongoCursorTimeoutException will be thrown
304
     * @throws MongoCursorException
305
     * @return boolean
306
     */
307
    public function update(array $criteria , array $newobj, array $options = array())
308
    {
309
        $multiple = ($options['multiple']) ? $options['multiple'] : false;
310
//        $multiple = $options['multiple'] ?? false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
311
        $method = $multiple ? 'updateMany' : 'updateOne';
312
313
        return $this->collection->$method($criteria, $newobj, $options);
314
    }
315
316
    /**
317
     * (PECL mongo &gt;= 0.9.0)<br/>
318
     * Remove records from this collection
319
     * @link http://www.php.net/manual/en/mongocollection.remove.php
320
     * @param array $criteria [optional] <p>Query criteria for the documents to delete.</p>
321
     * @param array $options [optional] <p>An array of options for the remove operation. Currently available options
322
     * include:
323
     * </p><ul>
324
     * <li><p><em>"w"</em></p><p>See {@link http://www.php.net/manual/en/mongo.writeconcerns.php Write Concerns}. The default value for <b>MongoClient</b> is <em>1</em>.</p></li>
325
     * <li>
326
     * <p>
327
     * <em>"justOne"</em>
328
     * </p>
329
     * <p>
330
     * Specify <strong><code>TRUE</code></strong> to limit deletion to just one document. If <strong><code>FALSE</code></strong> or
331
     * omitted, all documents matching the criteria will be deleted.
332
     * </p>
333
     * </li>
334
     * <li><p><em>"fsync"</em></p><p>Boolean, defaults to <b>FALSE</b>. If journaling is enabled, it works exactly like <em>"j"</em>. If journaling is not enabled, the write operation blocks until it is synced to database files on disk. If <strong><code>TRUE</code></strong>, an acknowledged insert is implied and this option will override setting <em>"w"</em> to <em>0</em>.</p><blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">If journaling is enabled, users are strongly encouraged to use the <em>"j"</em> option instead of <em>"fsync"</em>. Do not use <em>"fsync"</em> and <em>"j"</em> simultaneously, as that will result in an error.</p></blockquote></li>
335
     * <li><p><em>"j"</em></p><p>Boolean, defaults to <b>FALSE</b>. Forces the write operation to block until it is synced to the journal on disk. If <strong><code>TRUE</code></strong>, an acknowledged write is implied and this option will override setting <em>"w"</em> to <em>0</em>.</p><blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">If this option is used and journaling is disabled, MongoDB 2.6+ will raise an error and the write will fail; older server versions will simply ignore the option.</p></blockquote></li>
336
     * <li><p><em>"socketTimeoutMS"</em></p><p>This option specifies the time limit, in milliseconds, for socket communication. If the server does not respond within the timeout period, a <b>MongoCursorTimeoutException</b> will be thrown and there will be no way to determine if the server actually handled the write or not. A value of <em>-1</em> may be specified to block indefinitely. The default value for <b>MongoClient</b> is <em>30000</em> (30 seconds).</p></li>
337
     * <li><p><em>"w"</em></p><p>See {@link http://www.php.net/manual/en/mongo.writeconcerns.php Write Concerns }. The default value for <b>MongoClient</b> is <em>1</em>.</p></li>
338
     * <li><p><em>"wTimeoutMS"</em></p><p>This option specifies the time limit, in milliseconds, for {@link http://www.php.net/manual/en/mongo.writeconcerns.php write concern} acknowledgement. It is only applicable when <em>"w"</em> is greater than <em>1</em>, as the timeout pertains to replication. If the write concern is not satisfied within the time limit, a <a href="class.mongocursorexception.php" class="classname">MongoCursorException</a> will be thrown. A value of <em>0</em> may be specified to block indefinitely. The default value for {@link http://www.php.net/manual/en/class.mongoclient.php MongoClient} is <em>10000</em> (ten seconds).</p></li>
339
     * </ul>
340
     *
341
     * <p>
342
     * The following options are deprecated and should no longer be used:
343
     * </p><ul>
344
     * <li><p><em>"safe"</em></p><p>Deprecated. Please use the {@link http://www.php.net/manual/en/mongo.writeconcerns.php write concern} <em>"w"</em> option.</p></li>
345
     * <li><p><em>"timeout"</em></p><p>Deprecated alias for <em>"socketTimeoutMS"</em>.</p></li>
346
     * <li><p><b>"wtimeout"</b></p><p>Deprecated alias for <em>"wTimeoutMS"</em>.</p></p>
347
     * @throws MongoCursorException
348
     * @throws MongoCursorTimeoutException
349
     * @return bool|array <p>Returns an array containing the status of the removal if the
350
     * <em>"w"</em> option is set. Otherwise, returns <b>TRUE</b>.
351
     * </p>
352
     * <p>
353
     * Fields in the status array are described in the documentation for
354
     * <b>MongoCollection::insert()</b>.
355
     * </p>
356
     */
357
    public function remove(array $criteria = array(), array $options = array())
358
    {
359
        $multiple = isset($options['justOne']) ? !$options['justOne'] : false;
360
//        $multiple = !$options['justOne'] ?? false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
361
        $method = $multiple ? 'deleteMany' : 'deleteOne';
362
363
        return $this->collection->$method($criteria, $options);
364
    }
365
366
    /**
367
     * Querys this collection
368
     * @link http://www.php.net/manual/en/mongocollection.find.php
369
     * @param array $query The fields for which to search.
370
     * @param array $fields Fields of the results to return.
371
     * @return MongoCursor
372
     */
373
    public function find(array $query = array(), array $fields = array())
374
    {
375
        $cursor = new MongoCursor($this->db->getConnection(), (string)$this, $query, $fields);
376
        $cursor->setReadPreference($this->getReadPreference());
0 ignored issues
show
Documentation introduced by
$this->getReadPreference() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
377
378
        return $cursor;
379
    }
380
381
    /**
382
     * Retrieve a list of distinct values for the given key across a collection
383
     * @link http://www.php.net/manual/ru/mongocollection.distinct.php
384
     * @param string $key The key to use.
385
     * @param array $query An optional query parameters
386
     * @return array|bool Returns an array of distinct values, or <b>FALSE</b> on failure
387
     */
388
    public function distinct($key, array $query = [])
389
    {
390
        return array_map([TypeConverter::class, 'convertToLegacyType'], $this->collection->distinct($key, $query));
391
    }
392
393
    /**
394
     * Update a document and return it
395
     * @link http://www.php.net/manual/ru/mongocollection.findandmodify.php
396
     * @param array $query The query criteria to search for.
397
     * @param array $update The update criteria.
398
     * @param array $fields Optionally only return these fields.
399
     * @param array $options An array of options to apply, such as remove the match document from the DB and return it.
400
     * @return array Returns the original document, or the modified document when new is set.
401
     */
402
    public function findAndModify(array $query, array $update = NULL, array $fields = NULL, array $options = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
403
    {
404
405
    }
406
407
    /**
408
     * Querys this collection, returning a single element
409
     * @link http://www.php.net/manual/en/mongocollection.findone.php
410
     * @param array $query The fields for which to search.
411
     * @param array $fields Fields of the results to return.
412
     * @return array|null
413
     */
414
    public function findOne(array $query = array(), array $fields = array())
415
    {
416
        $document = $this->collection->findOne(TypeConverter::convertLegacyArrayToObject($query), ['projection' => $fields]);
417
        if ($document !== null) {
418
            $document = TypeConverter::convertObjectToLegacyArray($document);
419
        }
420
421
        return $document;
422
    }
423
424
    /**
425
     * Creates an index on the given field(s), or does nothing if the index already exists
426
     * @link http://www.php.net/manual/en/mongocollection.createindex.php
427
     * @param array $keys Field or fields to use as index.
428
     * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
429
     * @return array Returns the database response.
430
     */
431
    public function createIndex(array $keys, array $options = array()) {}
0 ignored issues
show
Unused Code introduced by
The parameter $keys is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
432
433
    /**
434
     * @deprecated Use MongoCollection::createIndex() instead.
435
     * Creates an index on the given field(s), or does nothing if the index already exists
436
     * @link http://www.php.net/manual/en/mongocollection.ensureindex.php
437
     * @param array $keys Field or fields to use as index.
438
     * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
439
     * @return boolean always true
440
     */
441
    public function ensureIndex(array $keys, array $options = array()) {}
0 ignored issues
show
Unused Code introduced by
The parameter $keys is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
442
443
    /**
444
     * Deletes an index from this collection
445
     * @link http://www.php.net/manual/en/mongocollection.deleteindex.php
446
     * @param string|array $keys Field or fields from which to delete the index.
447
     * @return array Returns the database response.
448
     */
449
    public function deleteIndex($keys) {}
0 ignored issues
show
Unused Code introduced by
The parameter $keys is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
450
451
    /**
452
     * Delete all indexes for this collection
453
     * @link http://www.php.net/manual/en/mongocollection.deleteindexes.php
454
     * @return array Returns the database response.
455
     */
456
    public function deleteIndexes() {}
457
458
    /**
459
     * Returns an array of index names for this collection
460
     * @link http://www.php.net/manual/en/mongocollection.getindexinfo.php
461
     * @return array Returns a list of index names.
462
     */
463
    public function getIndexInfo() {}
464
465
    /**
466
     * Counts the number of documents in this collection
467
     * @link http://www.php.net/manual/en/mongocollection.count.php
468
     * @param array|stdClass $query
469
     * @return int Returns the number of documents matching the query.
470
     */
471
    public function count($query = array())
472
    {
473
        return $this->collection->count($query);
474
    }
475
476
    /**
477
     * Saves an object to this collection
478
     * @link http://www.php.net/manual/en/mongocollection.save.php
479
     * @param array|object $a Array to save. If an object is used, it may not have protected or private properties.
480
     * Note: If the parameter does not have an _id key or property, a new MongoId instance will be created and assigned to it.
481
     * See MongoCollection::insert() for additional information on this behavior.
482
     * @param array $options Options for the save.
483
     * <dl>
484
     * <dt>"w"
485
     * <dd>See WriteConcerns. The default value for MongoClient is 1.
486
     * <dt>"fsync"
487
     * <dd>Boolean, defaults to FALSE. Forces the insert to be synced to disk before returning success. If TRUE, an acknowledged insert is implied and will override setting w to 0.
488
     * <dt>"timeout"
489
     * <dd>Integer, defaults to MongoCursor::$timeout. If "safe" is set, this sets how long (in milliseconds) for the client to wait for a database response. If the database does not respond within the timeout period, a MongoCursorTimeoutException will be thrown.
490
     * <dt>"safe"
491
     * <dd>Deprecated. Please use the WriteConcern w option.
492
     * </dl>
493
     * @throws MongoException if the inserted document is empty or if it contains zero-length keys. Attempting to insert an object with protected and private properties will cause a zero-length key error.
494
     * @throws MongoCursorException if the "w" option is set and the write fails.
495
     * @throws MongoCursorTimeoutException if the "w" option is set to a value greater than one and the operation takes longer than MongoCursor::$timeout milliseconds to complete. This does not kill the operation on the server, it is a client-side timeout. The operation in MongoCollection::$wtimeout is milliseconds.
496
     * @return array|boolean If w was set, returns an array containing the status of the save.
497
     * Otherwise, returns a boolean representing if the array was not empty (an empty array will not be inserted).
498
     */
499
    public function save($a, array $options = array()) {}
0 ignored issues
show
Unused Code introduced by
The parameter $a is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
500
501
    /**
502
     * Creates a database reference
503
     * @link http://www.php.net/manual/en/mongocollection.createdbref.php
504
     * @param array $a Object to which to create a reference.
505
     * @return array Returns a database reference array.
506
     */
507
    public function createDBRef(array $a) {}
0 ignored issues
show
Unused Code introduced by
The parameter $a is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
508
509
    /**
510
     * Fetches the document pointed to by a database reference
511
     * @link http://www.php.net/manual/en/mongocollection.getdbref.php
512
     * @param array $ref A database reference.
513
     * @return array Returns the database document pointed to by the reference.
514
     */
515
    public function getDBRef(array $ref) {}
0 ignored issues
show
Unused Code introduced by
The parameter $ref is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
516
517
    /**
518
     * @param  mixed $keys
519
     * @static
520
     * @return string
521
     */
522
    protected static function toIndexString($keys) {}
0 ignored issues
show
Unused Code introduced by
The parameter $keys is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
523
524
    /**
525
     * Performs an operation similar to SQL's GROUP BY command
526
     * @link http://www.php.net/manual/en/mongocollection.group.php
527
     * @param mixed $keys Fields to group by. If an array or non-code object is passed, it will be the key used to group results.
528
     * @param array $initial Initial value of the aggregation counter object.
529
     * @param MongoCode $reduce A function that aggregates (reduces) the objects iterated.
530
     * @param array $condition An condition that must be true for a row to be considered.
531
     * @return array
532
     */
533
    public function group($keys, array $initial, MongoCode $reduce, array $condition = array()) {}
0 ignored issues
show
Unused Code introduced by
The parameter $keys is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $initial is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $reduce is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $condition is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
534
535
    protected function notImplemented()
536
    {
537
        throw new \Exception('Not implemented');
538
    }
539
540
    /**
541
     * @return \MongoDB\Collection
542
     */
543 View Code Duplication
    private function createCollectionObject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
544
    {
545
        $options = [
546
            'readPreference' => $this->readPreference,
547
            'writeConcern' => $this->writeConcern,
548
        ];
549
550
        if ($this->collection === null) {
551
            $this->collection = $this->db->getDb()->selectCollection($this->name, $options);
552
        } else {
553
            $this->collection = $this->collection->withOptions($options);
554
        }
555
    }
556
}
557
558