Completed
Pull Request — master (#8)
by
unknown
02:47
created

MongoCollection::batchInsert()   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 2
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
use MongoDB\Driver\ReadPreference;
19
use MongoDB\Driver\WriteConcern;
20
21
/**
22
 * Represents a database collection.
23
 * @link http://www.php.net/manual/en/class.mongocollection.php
24
 */
25
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...
26
{
27
    use Helper\ReadPreference;
28
    use Helper\WriteConcern;
29
    use Helper\Slave;
30
31
    const ASCENDING = 1;
32
    const DESCENDING = -1;
33
34
    /**
35
     * @var MongoDB
36
     */
37
    public $db = NULL;
38
39
    /**
40
     * @var string
41
     */
42
    protected $name;
43
44
    /**
45
     * @var \MongoDB\Collection
46
     */
47
    protected $collection;
48
49
    /**
50
     * Creates a new collection
51
     * @link http://www.php.net/manual/en/mongocollection.construct.php
52
     * @param MongoDB $db Parent database.
53
     * @param string $name Name for this collection.
54
     * @throws Exception
55
     * @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...
56
     */
57
    public function __construct(MongoDB $db, $name)
58
    {
59
        $this->db = $db;
60
        $this->name = $name;
61
62
        $this->setReadPreferenceFromArray($db->getReadPreference());
63
        $this->setWriteConcernFromArray($db->getWriteConcern());
64
65
        $this->createCollectionObject();
66
    }
67
68
    /**
69
     * Gets the underlying collection for this object
70
     *
71
     * @internal This part is not of the ext-mongo API and should not be used
72
     * @return \MongoDB\Collection
73
     */
74
    public function getCollection()
75
    {
76
        return $this->collection;
77
    }
78
79
    /**
80
     * String representation of this collection
81
     * @link http://www.php.net/manual/en/mongocollection.--tostring.php
82
     * @return string Returns the full name of this collection.
83
     */
84
    public function __toString()
85
    {
86
        return (string) $this->db . '.' . $this->name;
87
    }
88
89
    /**
90
     * Gets a collection
91
     * @link http://www.php.net/manual/en/mongocollection.get.php
92
     * @param string $name The next string in the collection name.
93
     * @return MongoCollection
94
     */
95
    public function __get($name)
96
    {
97
        // Handle w and wtimeout properties that replicate data stored in $readPreference
98
        if ($name === 'w' || $name === 'wtimeout') {
99
            return $this->getWriteConcern()[$name];
100
        }
101
102
        return $this->db->selectCollection($this->name . '.' . $name);
103
    }
104
105
    /**
106
     * @param string $name
107
     * @param mixed $value
108
     */
109 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...
110
    {
111
        if ($name === 'w' || $name === 'wtimeout') {
112
            $this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern());
113
            $this->createCollectionObject();
114
        }
115
    }
116
117
    /**
118
     * @link http://www.php.net/manual/en/mongocollection.aggregate.php
119
     * @param array $pipeline
120
     * @param array $op
121
     * @return array
122
     */
123
    public function aggregate(array $pipeline, array $op = [])
124
    {
125
        if (! TypeConverter::isNumericArray($pipeline)) {
126
            $pipeline = [];
127
            $options = [];
128
129
            $i = 0;
130
            foreach (func_get_args() as $operator) {
131
                $i++;
132
                if (! is_array($operator)) {
133
                    trigger_error("Argument $i is not an array", E_WARNING);
134
                    return;
135
                }
136
137
                $pipeline[] = $operator;
138
            }
139
        } else {
140
            $options = $op;
141
        }
142
143
        $command = [
144
            'aggregate' => $this->name,
145
            'pipeline' => $pipeline
146
        ];
147
148
        $command += $options;
149
150
        return $this->db->command($command, [], $hash);
151
    }
152
153
    /**
154
     * @link http://php.net/manual/en/mongocollection.aggregatecursor.php
155
     * @param array $pipeline
156
     * @param array $options
157
     * @return MongoCommandCursor
158
     */
159
    public function aggregateCursor(array $pipeline, array $options = [])
160
    {
161
        // Build command manually, can't use mongo-php-library here
162
        $command = [
163
            'aggregate' => $this->name,
164
            'pipeline' => $pipeline
165
        ];
166
167
        // Convert cursor option
168
        if (! isset($options['cursor']) || $options['cursor'] === true || $options['cursor'] === []) {
169
            // Cursor option needs to be an object convert bools and empty arrays since those won't be handled by TypeConverter
170
            $options['cursor'] = new \stdClass;
171
        }
172
173
        $command += $options;
174
175
        $cursor = new MongoCommandCursor($this->db->getConnection(), (string)$this, $command);
176
        $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...
177
178
        return $cursor;
179
    }
180
181
    /**
182
     * Returns this collection's name
183
     * @link http://www.php.net/manual/en/mongocollection.getname.php
184
     * @return string
185
     */
186
    public function getName()
187
    {
188
        return $this->name;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function setSlaveOkay($ok = true)
195
    {
196
        $result = $this->setSlaveOkayFromParameter($ok);
197
        $this->createCollectionObject();
198
        return $result;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function setReadPreference($readPreference, $tags = null)
205
    {
206
        $result = $this->setReadPreferenceFromParameters($readPreference, $tags);
207
        $this->createCollectionObject();
208
209
        return $result;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function setWriteConcern($wstring, $wtimeout = 0)
216
    {
217
        $result = $this->setWriteConcernFromParameters($wstring, $wtimeout);
218
        $this->createCollectionObject();
219
220
        return $result;
221
    }
222
223
    /**
224
     * Drops this collection
225
     * @link http://www.php.net/manual/en/mongocollection.drop.php
226
     * @return array Returns the database response.
227
     */
228
    public function drop()
229
    {
230
        return $this->collection->drop();
231
    }
232
233
    /**
234
     * Validates this collection
235
     * @link http://www.php.net/manual/en/mongocollection.validate.php
236
     * @param bool $scan_data Only validate indices, not the base collection.
237
     * @return array Returns the database's evaluation of this object.
238
     */
239
    public function validate($scan_data = FALSE)
240
    {
241
        $command = [
242
            'validate' => $this->name,
243
            'full'     => $scan_data,
244
        ];
245
        $result = $this->db->command($command, [], $hash);
246
        if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
247
            return $result;
248
        }
249
250
        return false;
251
    }
252
253
    /**
254
     * Inserts an array into the collection
255
     * @link http://www.php.net/manual/en/mongocollection.insert.php
256
     * @param array|object $a
257
     * @param array $options
258
     * @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.
259
     * @throws MongoCursorException if the "w" option is set and the write fails.
260
     * @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.
261
     * @return bool|array Returns an array containing the status of the insertion if the "w" option is set.
262
     */
263
    public function insert($a, array $options = array())
264
    {
265
        return $this->collection->insertOne(TypeConverter::convertLegacyArrayToObject($a), $options);
266
    }
267
268
    /**
269
     * Inserts multiple documents into this collection
270
     * @link http://www.php.net/manual/en/mongocollection.batchinsert.php
271
     * @param array $a An array of arrays.
272
     * @param array $options Options for the inserts.
273
     * @throws MongoCursorException
274
     * @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.
275
     */
276
    public function batchInsert(array $a, array $options = array())
277
    {
278
        return $this->collection->insertMany($a, $options);
279
    }
280
281
    /**
282
     * Update records based on a given criteria
283
     * @link http://www.php.net/manual/en/mongocollection.update.php
284
     * @param array $criteria Description of the objects to update.
285
     * @param array $newobj The object with which to update the matching records.
286
     * @param array $options This parameter is an associative array of the form
287
     *        array("optionname" => boolean, ...).
288
     *
289
     *        Currently supported options are:
290
     *          "upsert": If no document matches $$criteria, a new document will be created from $$criteria and $$new_object (see upsert example).
291
     *
292
     *          "multiple": All documents matching $criteria will be updated. MongoCollection::update has exactly the opposite behavior of MongoCollection::remove- it updates one document by
293
     *          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
294
     *          database may change its default behavior at some point in the future.
295
     *
296
     *          "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
297
     *          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
298
     *          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).
299
     *          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.
300
     *          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).
301
     *          This overrides the w variable set on the collection.
302
     *
303
     *         "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.
304
     *
305
     *         "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
306
     *         not respond within the timeout period, a MongoCursorTimeoutException will be thrown
307
     * @throws MongoCursorException
308
     * @return boolean
309
     */
310
    public function update(array $criteria , array $newobj, array $options = array())
311
    {
312
        $multiple = ($options['multiple']) ? $options['multiple'] : false;
313
//        $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...
314
        $method = $multiple ? 'updateMany' : 'updateOne';
315
316
        return $this->collection->$method($criteria, $newobj, $options);
317
    }
318
319
    /**
320
     * (PECL mongo &gt;= 0.9.0)<br/>
321
     * Remove records from this collection
322
     * @link http://www.php.net/manual/en/mongocollection.remove.php
323
     * @param array $criteria [optional] <p>Query criteria for the documents to delete.</p>
324
     * @param array $options [optional] <p>An array of options for the remove operation. Currently available options
325
     * include:
326
     * </p><ul>
327
     * <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>
328
     * <li>
329
     * <p>
330
     * <em>"justOne"</em>
331
     * </p>
332
     * <p>
333
     * Specify <strong><code>TRUE</code></strong> to limit deletion to just one document. If <strong><code>FALSE</code></strong> or
334
     * omitted, all documents matching the criteria will be deleted.
335
     * </p>
336
     * </li>
337
     * <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>
338
     * <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>
339
     * <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>
340
     * <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>
341
     * <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>
342
     * </ul>
343
     *
344
     * <p>
345
     * The following options are deprecated and should no longer be used:
346
     * </p><ul>
347
     * <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>
348
     * <li><p><em>"timeout"</em></p><p>Deprecated alias for <em>"socketTimeoutMS"</em>.</p></li>
349
     * <li><p><b>"wtimeout"</b></p><p>Deprecated alias for <em>"wTimeoutMS"</em>.</p></p>
350
     * @throws MongoCursorException
351
     * @throws MongoCursorTimeoutException
352
     * @return bool|array <p>Returns an array containing the status of the removal if the
353
     * <em>"w"</em> option is set. Otherwise, returns <b>TRUE</b>.
354
     * </p>
355
     * <p>
356
     * Fields in the status array are described in the documentation for
357
     * <b>MongoCollection::insert()</b>.
358
     * </p>
359
     */
360
    public function remove(array $criteria = array(), array $options = array())
361
    {
362
        $multiple = isset($options['justOne']) ? !$options['justOne'] : false;
363
//        $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...
364
        $method = $multiple ? 'deleteMany' : 'deleteOne';
365
366
        return $this->collection->$method($criteria, $options);
367
    }
368
369
    /**
370
     * Querys this collection
371
     * @link http://www.php.net/manual/en/mongocollection.find.php
372
     * @param array $query The fields for which to search.
373
     * @param array $fields Fields of the results to return.
374
     * @return MongoCursor
375
     */
376
    public function find(array $query = array(), array $fields = array())
377
    {
378
        $cursor = new MongoCursor($this->db->getConnection(), (string)$this, $query, $fields);
379
        $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...
380
381
        return $cursor;
382
    }
383
384
    /**
385
     * Retrieve a list of distinct values for the given key across a collection
386
     * @link http://www.php.net/manual/ru/mongocollection.distinct.php
387
     * @param string $key The key to use.
388
     * @param array $query An optional query parameters
389
     * @return array|bool Returns an array of distinct values, or <b>FALSE</b> on failure
390
     */
391
    public function distinct($key, array $query = [])
392
    {
393
        return array_map([TypeConverter::class, 'convertToLegacyType'], $this->collection->distinct($key, $query));
394
    }
395
396
    /**
397
     * Update a document and return it
398
     * @link http://www.php.net/manual/ru/mongocollection.findandmodify.php
399
     * @param array $query The query criteria to search for.
400
     * @param array $update The update criteria.
401
     * @param array $fields Optionally only return these fields.
402
     * @param array $options An array of options to apply, such as remove the match document from the DB and return it.
403
     * @return array Returns the original document, or the modified document when new is set.
404
     */
405
    public function findAndModify(array $query, array $update = NULL, array $fields = NULL, array $options = [])
406
    {
407
        $query = TypeConverter::convertLegacyArrayToObject($query);
408
        if (isset($options['remove'])) {
409
            unset($options['remove']);
410
            $document = $this->collection->findOneAndDelete($query, $options);
411
        } else {
412
            if (is_array($update)) {
413
                $update = TypeConverter::convertLegacyArrayToObject($update);
414
            }
415
            if (is_array($fields)) {
416
                $fields = TypeConverter::convertLegacyArrayToObject($fields);
417
            }
418
            if (isset($options['new'])) {
419
                $options['returnDocument'] = \MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER;
420
                unset($options['new']);
421
            }
422
            if ($fields) {
423
                $options['projection'] = $fields;
424
            }
425
            $document = $this->collection->findOneAndUpdate($query, $update, $options);
0 ignored issues
show
Bug introduced by
It seems like $update defined by parameter $update on line 405 can also be of type null; however, MongoDB\Collection::findOneAndUpdate() does only seem to accept array|object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
426
        }
427
        if ($document) {
428
            $document = TypeConverter::convertObjectToLegacyArray($document);
429
        }
430
        return $document;
431
    }
432
433
    /**
434
     * Querys this collection, returning a single element
435
     * @link http://www.php.net/manual/en/mongocollection.findone.php
436
     * @param array $query The fields for which to search.
437
     * @param array $fields Fields of the results to return.
438
     * @return array|null
439
     */
440
    public function findOne(array $query = array(), array $fields = array())
441
    {
442
        $document = $this->collection->findOne(TypeConverter::convertLegacyArrayToObject($query), ['projection' => $fields]);
443
        if ($document !== null) {
444
            $document = TypeConverter::convertObjectToLegacyArray($document);
445
        }
446
447
        return $document;
448
    }
449
450
    /**
451
     * Creates an index on the given field(s), or does nothing if the index already exists
452
     * @link http://www.php.net/manual/en/mongocollection.createindex.php
453
     * @param array $keys Field or fields to use as index.
454
     * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
455
     * @return array Returns the database response.
456
     */
457
    public function createIndex(array $keys, array $options = array())
458
    {
459
        return $this->collection->createIndex($keys, $options);
460
    }
461
462
    /**
463
     * @deprecated Use MongoCollection::createIndex() instead.
464
     * Creates an index on the given field(s), or does nothing if the index already exists
465
     * @link http://www.php.net/manual/en/mongocollection.ensureindex.php
466
     * @param array $keys Field or fields to use as index.
467
     * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
468
     * @return boolean always true
469
     */
470
    public function ensureIndex(array $keys, array $options = array())
471
    {
472
        $this->createIndex($keys, $options);
473
        return true;
474
    }
475
476
    /**
477
     * Deletes an index from this collection
478
     * @link http://www.php.net/manual/en/mongocollection.deleteindex.php
479
     * @param string|array $keys Field or fields from which to delete the index.
480
     * @return array Returns the database response.
481
     */
482
    public function deleteIndex($keys)
483
    {
484
        if (is_string($keys)) {
485
            $indexName = $keys;
486
        } elseif (is_array($keys)) {
487
            $indexName = self::toIndexString($keys);
488
        } else {
489
            throw new \InvalidArgumentException();
490
        }
491
        return $this->collection->dropIndex($indexName);
492
    }
493
494
    /**
495
     * Delete all indexes for this collection
496
     * @link http://www.php.net/manual/en/mongocollection.deleteindexes.php
497
     * @return array Returns the database response.
498
     */
499
    public function deleteIndexes()
500
    {
501
        return $this->collection->dropIndexes();
502
    }
503
504
    /**
505
     * Returns an array of index names for this collection
506
     * @link http://www.php.net/manual/en/mongocollection.getindexinfo.php
507
     * @return array Returns a list of index names.
508
     */
509
    public function getIndexInfo()
510
    {
511
        $convertIndex = function($indexInfo) {
512
            return $indexInfo->__debugInfo();
513
        };
514
        return array_map($convertIndex, iterator_to_array($this->collection->listIndexes()));
515
    }
516
517
    /**
518
     * Counts the number of documents in this collection
519
     * @link http://www.php.net/manual/en/mongocollection.count.php
520
     * @param array|stdClass $query
521
     * @return int Returns the number of documents matching the query.
522
     */
523
    public function count($query = array())
524
    {
525
        return $this->collection->count($query);
526
    }
527
528
    /**
529
     * Saves an object to this collection
530
     * @link http://www.php.net/manual/en/mongocollection.save.php
531
     * @param array|object $a Array to save. If an object is used, it may not have protected or private properties.
532
     * Note: If the parameter does not have an _id key or property, a new MongoId instance will be created and assigned to it.
533
     * See MongoCollection::insert() for additional information on this behavior.
534
     * @param array $options Options for the save.
535
     * <dl>
536
     * <dt>"w"
537
     * <dd>See WriteConcerns. The default value for MongoClient is 1.
538
     * <dt>"fsync"
539
     * <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.
540
     * <dt>"timeout"
541
     * <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.
542
     * <dt>"safe"
543
     * <dd>Deprecated. Please use the WriteConcern w option.
544
     * </dl>
545
     * @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.
546
     * @throws MongoCursorException if the "w" option is set and the write fails.
547
     * @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.
548
     * @return array|boolean If w was set, returns an array containing the status of the save.
549
     * Otherwise, returns a boolean representing if the array was not empty (an empty array will not be inserted).
550
     */
551
    public function save($a, array $options = array())
0 ignored issues
show
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...
552
    {
553
        if (is_object($a)) {
554
            $a = (array)$a;
555
        }
556
        if ( ! array_key_exists('_id', $a)) {
557
            $id = new \MongoId();
558
        } else {
559
            $id = $a['_id'];
560
            unset($a['_id']);
561
        }
562
        $filter = ['_id' => $id];
563
        $filter = TypeConverter::convertLegacyArrayToObject($filter);
564
        $a = TypeConverter::convertLegacyArrayToObject($a);
565
        return $this->collection->updateOne($filter, ['$set' => $a], ['upsert' => true]);
566
    }
567
568
    /**
569
     * Creates a database reference
570
     * @link http://www.php.net/manual/en/mongocollection.createdbref.php
571
     * @param array $a Object to which to create a reference.
572
     * @return array Returns a database reference array.
573
     */
574
    public function createDBRef(array $a)
575
    {
576
        return \MongoDBRef::create($this->name, $a['_id']);
577
    }
578
579
    /**
580
     * Fetches the document pointed to by a database reference
581
     * @link http://www.php.net/manual/en/mongocollection.getdbref.php
582
     * @param array $ref A database reference.
583
     * @return array Returns the database document pointed to by the reference.
584
     */
585
    public function getDBRef(array $ref)
586
    {
587
        return \MongoDBRef::get($this->db, $ref);
588
    }
589
590
    /**
591
     * @param  mixed $keys
592
     * @static
593
     * @return string
594
     */
595
    protected static function toIndexString($keys)
596
    {
597
        $result = '';
598
        foreach ($keys as $name => $direction) {
599
            $result .= sprintf('%s_%d', $name, $direction);
600
        }
601
        return $result;
602
    }
603
604
    /**
605
     * Performs an operation similar to SQL's GROUP BY command
606
     * @link http://www.php.net/manual/en/mongocollection.group.php
607
     * @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.
608
     * @param array $initial Initial value of the aggregation counter object.
609
     * @param MongoCode $reduce A function that aggregates (reduces) the objects iterated.
610
     * @param array $condition An condition that must be true for a row to be considered.
611
     * @return array
612
     */
613
    public function group($keys, array $initial, $reduce, array $condition = [])
614
    {
615
        if (is_string($reduce)) {
616
            $reduce = new MongoCode($reduce);
617
        }
618
        if ( ! $reduce instanceof MongoCode) {
619
            throw new \InvalidArgumentExcption('reduce parameter should be a string or MongoCode instance.');
620
        }
621
        $command = [
622
            'group' => [
623
                'ns'      => $this->name,
624
                '$reduce' => (string)$reduce,
625
                'initial' => $initial,
626
                'cond'    => $condition,
627
            ],
628
        ];
629
630
        if ($keys instanceof MongoCode) {
631
            $command['group']['$keyf'] = (string)$keys;
632
        } else {
633
            $command['group']['key'] = $keys;
634
        }
635
        if (array_key_exists('condition', $condition)) {
636
            $command['group']['cond'] = $condition['condition'];
637
        }
638
        if (array_key_exists('finalize', $condition)) {
639
            if ($condition['finalize'] instanceof MongoCode) {
640
                $condition['finalize'] = (string)$condition['finalize'];
641
            }
642
            $command['group']['finalize'] = $condition['finalize'];
643
        }
644
645
        $result = $this->db->command($command, [], $hash);
646
        unset($result['waitedMS']);
647
        $result['ok'] = (int)$result['ok'];
648
        if (count($result['retval'])) {
649
            $result['retval'] = current($result['retval']);
650
        }
651
        return $result;
652
    }
653
654
    // public function parallelCollectionScan(int $num_cursors)
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
655
    // {
656
    //     $command = [
657
    //         'parallelCollectionScan' => $this->name,
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
658
    //         'numCursors'             => $num_cursors,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
659
    //     ];
660
    //     $result = $this->db->command($command, [], $hash);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
661
    //     if ($result) {
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...
662
    //         return $result;
663
    //     }
664
    //     return false;
665
    // }
666
667
    protected function notImplemented()
668
    {
669
        throw new \Exception('Not implemented');
670
    }
671
672
    /**
673
     * @return \MongoDB\Collection
674
     */
675 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...
676
    {
677
        $options = [
678
            'readPreference' => $this->readPreference,
679
            'writeConcern' => $this->writeConcern,
680
        ];
681
682
        if ($this->collection === null) {
683
            $this->collection = $this->db->getDb()->selectCollection($this->name, $options);
684
        } else {
685
            $this->collection = $this->collection->withOptions($options);
686
        }
687
    }
688
}
689
690