Completed
Push — master ( 9578a4...c971cb )
by Andreas
02:30
created

MongoCollection::aggregate()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 29
rs 8.5806
cc 4
eloc 18
nc 3
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
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\SlaveOkay;
27
    use Helper\WriteConcern;
28
29
    const ASCENDING = 1;
30
    const DESCENDING = -1;
31
32
    /**
33
     * @var MongoDB
34
     */
35
    public $db = NULL;
36
37
    /**
38
     * @var string
39
     */
40
    protected $name;
41
42
    /**
43
     * @var \MongoDB\Collection
44
     */
45
    protected $collection;
46
47
    /**
48
     * Creates a new collection
49
     *
50
     * @link http://www.php.net/manual/en/mongocollection.construct.php
51
     * @param MongoDB $db Parent database.
52
     * @param string $name Name for this collection.
53
     * @throws Exception
54
     * @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...
55
     */
56
    public function __construct(MongoDB $db, $name)
57
    {
58
        $this->db = $db;
59
        $this->name = $name;
60
61
        $this->setReadPreferenceFromArray($db->getReadPreference());
62
        $this->setWriteConcernFromArray($db->getWriteConcern());
63
64
        $this->createCollectionObject();
65
    }
66
67
    /**
68
     * Gets the underlying collection for this object
69
     *
70
     * @internal This part is not of the ext-mongo API and should not be used
71
     * @return \MongoDB\Collection
72
     */
73
    public function getCollection()
74
    {
75
        return $this->collection;
76
    }
77
78
    /**
79
     * String representation of this collection
80
     *
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
     *
92
     * @link http://www.php.net/manual/en/mongocollection.get.php
93
     * @param string $name The next string in the collection name.
94
     * @return MongoCollection
95
     */
96
    public function __get($name)
97
    {
98
        // Handle w and wtimeout properties that replicate data stored in $readPreference
99
        if ($name === 'w' || $name === 'wtimeout') {
100
            return $this->getWriteConcern()[$name];
101
        }
102
103
        return $this->db->selectCollection($this->name . '.' . $name);
104
    }
105
106
    /**
107
     * @param string $name
108
     * @param mixed $value
109
     */
110 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...
111
    {
112
        if ($name === 'w' || $name === 'wtimeout') {
113
            $this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern());
114
            $this->createCollectionObject();
115
        }
116
    }
117
118
    /**
119
     * Perform an aggregation using the aggregation framework
120
     *
121
     * @link http://www.php.net/manual/en/mongocollection.aggregate.php
122
     * @param array $pipeline
123
     * @param array $op
124
     * @return array
125
     */
126
    public function aggregate(array $pipeline, array $op = [])
127
    {
128
        if (! TypeConverter::isNumericArray($pipeline)) {
129
            $pipeline = [];
130
            $options = [];
131
132
            $i = 0;
133
            foreach (func_get_args() as $operator) {
134
                $i++;
135
                if (! is_array($operator)) {
136
                    trigger_error("Argument $i is not an array", E_WARNING);
137
                    return;
138
                }
139
140
                $pipeline[] = $operator;
141
            }
142
        } else {
143
            $options = $op;
144
        }
145
146
        $command = [
147
            'aggregate' => $this->name,
148
            'pipeline' => $pipeline
149
        ];
150
151
        $command += $options;
152
153
        return $this->db->command($command, [], $hash);
154
    }
155
156
    /**
157
     * Execute an aggregation pipeline command and retrieve results through a cursor
158
     *
159
     * @link http://php.net/manual/en/mongocollection.aggregatecursor.php
160
     * @param array $pipeline
161
     * @param array $options
162
     * @return MongoCommandCursor
163
     */
164
    public function aggregateCursor(array $pipeline, array $options = [])
165
    {
166
        // Build command manually, can't use mongo-php-library here
167
        $command = [
168
            'aggregate' => $this->name,
169
            'pipeline' => $pipeline
170
        ];
171
172
        // Convert cursor option
173
        if (! isset($options['cursor']) || $options['cursor'] === true || $options['cursor'] === []) {
174
            // Cursor option needs to be an object convert bools and empty arrays since those won't be handled by TypeConverter
175
            $options['cursor'] = new \stdClass;
176
        }
177
178
        $command += $options;
179
180
        $cursor = new MongoCommandCursor($this->db->getConnection(), (string)$this, $command);
181
        $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...
182
183
        return $cursor;
184
    }
185
186
    /**
187
     * Returns this collection's name
188
     *
189
     * @link http://www.php.net/manual/en/mongocollection.getname.php
190
     * @return string
191
     */
192
    public function getName()
193
    {
194
        return $this->name;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function setReadPreference($readPreference, $tags = null)
201
    {
202
        $result = $this->setReadPreferenceFromParameters($readPreference, $tags);
203
        $this->createCollectionObject();
204
205
        return $result;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function setWriteConcern($wstring, $wtimeout = 0)
212
    {
213
        $result = $this->setWriteConcernFromParameters($wstring, $wtimeout);
214
        $this->createCollectionObject();
215
216
        return $result;
217
    }
218
219
    /**
220
     * Drops this collection
221
     *
222
     * @link http://www.php.net/manual/en/mongocollection.drop.php
223
     * @return array Returns the database response.
224
     */
225
    public function drop()
226
    {
227
        return $this->collection->drop();
228
    }
229
230
    /**
231
     * Validates this collection
232
     *
233
     * @link http://www.php.net/manual/en/mongocollection.validate.php
234
     * @param bool $scan_data Only validate indices, not the base collection.
235
     * @return array Returns the database's evaluation of this object.
236
     */
237
    public function validate($scan_data = FALSE)
238
    {
239
        $command = [
240
            'validate' => $this->name,
241
            'full'     => $scan_data,
242
        ];
243
244
        return $this->db->command($command, [], $hash);
245
    }
246
247
    /**
248
     * Inserts an array into the collection
249
     *
250
     * @link http://www.php.net/manual/en/mongocollection.insert.php
251
     * @param array|object $a
252
     * @param array $options
253
     * @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.
254
     * @throws MongoCursorException if the "w" option is set and the write fails.
255
     * @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.
256
     * @return bool|array Returns an array containing the status of the insertion if the "w" option is set.
257
     */
258
    public function insert($a, array $options = [])
259
    {
260
        return $this->collection->insertOne(TypeConverter::convertLegacyArrayToObject($a), $options);
261
    }
262
263
    /**
264
     * Inserts multiple documents into this collection
265
     *
266
     * @link http://www.php.net/manual/en/mongocollection.batchinsert.php
267
     * @param array $a An array of arrays.
268
     * @param array $options Options for the inserts.
269
     * @throws MongoCursorException
270
     * @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.
271
     */
272
    public function batchInsert(array $a, array $options = [])
273
    {
274
        return $this->collection->insertMany($a, $options);
275
    }
276
277
    /**
278
     * Update records based on a given criteria
279
     *
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
284
     * @throws MongoCursorException
285
     * @return boolean
286
     */
287
    public function update(array $criteria , array $newobj, array $options = [])
288
    {
289
        $multiple = ($options['multiple']) ? $options['multiple'] : false;
290
        $method = $multiple ? 'updateMany' : 'updateOne';
291
292
        return $this->collection->$method($criteria, $newobj, $options);
293
    }
294
295
    /**
296
     * Remove records from this collection
297
     *
298
     * @link http://www.php.net/manual/en/mongocollection.remove.php
299
     * @param array $criteria Query criteria for the documents to delete.
300
     * @param array $options An array of options for the remove operation.
301
     * @throws MongoCursorException
302
     * @throws MongoCursorTimeoutException
303
     * @return bool|array Returns an array containing the status of the removal
304
     * if the "w" option is set. Otherwise, returns TRUE.
305
     */
306
    public function remove(array $criteria = [], array $options = [])
307
    {
308
        $multiple = isset($options['justOne']) ? !$options['justOne'] : false;
309
        $method = $multiple ? 'deleteMany' : 'deleteOne';
310
311
        return $this->collection->$method($criteria, $options);
312
    }
313
314
    /**
315
     * Querys this collection
316
     *
317
     * @link http://www.php.net/manual/en/mongocollection.find.php
318
     * @param array $query The fields for which to search.
319
     * @param array $fields Fields of the results to return.
320
     * @return MongoCursor
321
     */
322
    public function find(array $query = [], array $fields = [])
323
    {
324
        $cursor = new MongoCursor($this->db->getConnection(), (string)$this, $query, $fields);
325
        $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...
326
327
        return $cursor;
328
    }
329
330
    /**
331
     * Retrieve a list of distinct values for the given key across a collection
332
     *
333
     * @link http://www.php.net/manual/ru/mongocollection.distinct.php
334
     * @param string $key The key to use.
335
     * @param array $query An optional query parameters
336
     * @return array|bool Returns an array of distinct values, or FALSE on failure
337
     */
338
    public function distinct($key, array $query = [])
339
    {
340
        return array_map([TypeConverter::class, 'convertToLegacyType'], $this->collection->distinct($key, $query));
341
    }
342
343
    /**
344
     * Update a document and return it
345
     * @link http://www.php.net/manual/ru/mongocollection.findandmodify.php
346
     * @param array $query The query criteria to search for.
347
     * @param array $update The update criteria.
348
     * @param array $fields Optionally only return these fields.
349
     * @param array $options An array of options to apply, such as remove the match document from the DB and return it.
350
     * @return array Returns the original document, or the modified document when new is set.
351
     */
352
    public function findAndModify(array $query, array $update = null, array $fields = null, array $options = [])
353
    {
354
        $query = TypeConverter::convertLegacyArrayToObject($query);
355
356
        if (isset($options['remove'])) {
357
            unset($options['remove']);
358
            $document = $this->collection->findOneAndDelete($query, $options);
359
        } else {
360
            $update = is_array($update) ? TypeConverter::convertLegacyArrayToObject($update) : [];
361
362
            if (isset($options['new'])) {
363
                $options['returnDocument'] = \MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER;
364
                unset($options['new']);
365
            }
366
367
            $options['projection'] = is_array($fields) ? TypeConverter::convertLegacyArrayToObject($fields) : [];
368
369
            $document = $this->collection->findOneAndUpdate($query, $update, $options);
370
        }
371
372
        if ($document) {
373
            $document = TypeConverter::convertObjectToLegacyArray($document);
374
        }
375
376
        return $document;
377
    }
378
379
    /**
380
     * Querys this collection, returning a single element
381
     * @link http://www.php.net/manual/en/mongocollection.findone.php
382
     * @param array $query The fields for which to search.
383
     * @param array $fields Fields of the results to return.
384
     * @return array|null
385
     */
386
    public function findOne(array $query = [], array $fields = [])
387
    {
388
        $document = $this->collection->findOne(TypeConverter::convertLegacyArrayToObject($query), ['projection' => $fields]);
389
        if ($document !== null) {
390
            $document = TypeConverter::convertObjectToLegacyArray($document);
391
        }
392
393
        return $document;
394
    }
395
396
    /**
397
     * Creates an index on the given field(s), or does nothing if the index already exists
398
     * @link http://www.php.net/manual/en/mongocollection.createindex.php
399
     * @param array $keys Field or fields to use as index.
400
     * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
401
     * @return array Returns the database response.
402
     *
403
     * @todo This method does not yet return the correct result
404
     */
405
    public function createIndex(array $keys, array $options = [])
406
    {
407
        // Note: this is what the result array should look like
408
//        $expected = [
409
//            'createdCollectionAutomatically' => true,
410
//            'numIndexesBefore' => 1,
411
//            'numIndexesAfter' => 2,
412
//            'ok' => 1.0
413
//        ];
414
415
        return $this->collection->createIndex($keys, $options);
416
    }
417
418
    /**
419
     * @deprecated Use MongoCollection::createIndex() instead.
420
     * Creates an index on the given field(s), or does nothing if the index already exists
421
     * @link http://www.php.net/manual/en/mongocollection.ensureindex.php
422
     * @param array $keys Field or fields to use as index.
423
     * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
424
     * @return boolean always true
425
     */
426
    public function ensureIndex(array $keys, array $options = [])
427
    {
428
        $this->createIndex($keys, $options);
429
430
        return true;
431
    }
432
433
    /**
434
     * Deletes an index from this collection
435
     * @link http://www.php.net/manual/en/mongocollection.deleteindex.php
436
     * @param string|array $keys Field or fields from which to delete the index.
437
     * @return array Returns the database response.
438
     */
439
    public function deleteIndex($keys)
440
    {
441
        if (is_string($keys)) {
442
            $indexName = $keys;
443
        } elseif (is_array($keys)) {
444
            $indexName = self::toIndexString($keys);
445
        } else {
446
            throw new \InvalidArgumentException();
447
        }
448
449
        return TypeConverter::convertObjectToLegacyArray($this->collection->dropIndex($indexName));
450
    }
451
452
    /**
453
     * Delete all indexes for this collection
454
     * @link http://www.php.net/manual/en/mongocollection.deleteindexes.php
455
     * @return array Returns the database response.
456
     */
457
    public function deleteIndexes()
458
    {
459
        return TypeConverter::convertObjectToLegacyArray($this->collection->dropIndexes());
460
    }
461
462
    /**
463
     * Returns an array of index names for this collection
464
     * @link http://www.php.net/manual/en/mongocollection.getindexinfo.php
465
     * @return array Returns a list of index names.
466
     */
467
    public function getIndexInfo()
468
    {
469
        $convertIndex = function(\MongoDB\Model\IndexInfo $indexInfo) {
470
            return [
471
                'v' => $indexInfo->getVersion(),
472
                'key' => $indexInfo->getKey(),
473
                'name' => $indexInfo->getName(),
474
                'ns' => $indexInfo->getNamespace(),
475
            ];
476
        };
477
478
        return array_map($convertIndex, iterator_to_array($this->collection->listIndexes()));
479
    }
480
481
    /**
482
     * Counts the number of documents in this collection
483
     * @link http://www.php.net/manual/en/mongocollection.count.php
484
     * @param array|stdClass $query
485
     * @return int Returns the number of documents matching the query.
486
     */
487
    public function count($query = [])
488
    {
489
        return $this->collection->count($query);
490
    }
491
492
    /**
493
     * Saves an object to this collection
494
     *
495
     * @link http://www.php.net/manual/en/mongocollection.save.php
496
     * @param array|object $a Array to save. If an object is used, it may not have protected or private properties.
497
     * @param array $options Options for the save.
498
     * @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.
499
     * @throws MongoCursorException if the "w" option is set and the write fails.
500
     * @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.
501
     * @return array|boolean If w was set, returns an array containing the status of the save.
502
     * Otherwise, returns a boolean representing if the array was not empty (an empty array will not be inserted).
503
     */
504
    public function save($a, array $options = [])
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...
505
    {
506
        if (is_object($a)) {
507
            $a = (array)$a;
508
        }
509
        if ( ! array_key_exists('_id', $a)) {
510
            $id = new \MongoId();
511
        } else {
512
            $id = $a['_id'];
513
            unset($a['_id']);
514
        }
515
        $filter = ['_id' => $id];
516
        $filter = TypeConverter::convertLegacyArrayToObject($filter);
517
        $a = TypeConverter::convertLegacyArrayToObject($a);
518
        return $this->collection->updateOne($filter, ['$set' => $a], ['upsert' => true]);
519
    }
520
521
    /**
522
     * Creates a database reference
523
     *
524
     * @link http://www.php.net/manual/en/mongocollection.createdbref.php
525
     * @param array $a Object to which to create a reference.
526
     * @return array Returns a database reference array.
527
     */
528
    public function createDBRef(array $a)
529
    {
530
        return \MongoDBRef::create($this->name, $a['_id']);
531
    }
532
533
    /**
534
     * Fetches the document pointed to by a database reference
535
     *
536
     * @link http://www.php.net/manual/en/mongocollection.getdbref.php
537
     * @param array $ref A database reference.
538
     * @return array Returns the database document pointed to by the reference.
539
     */
540
    public function getDBRef(array $ref)
541
    {
542
        return \MongoDBRef::get($this->db, $ref);
543
    }
544
545
    /**
546
     * @param mixed $keys
547
     * @static
548
     * @return string
549
     */
550
    protected static function toIndexString($keys)
551
    {
552
        $result = '';
553
        foreach ($keys as $name => $direction) {
554
            $result .= sprintf('%s_%d', $name, $direction);
555
        }
556
        return $result;
557
    }
558
559
    /**
560
     * Performs an operation similar to SQL's GROUP BY command
561
     *
562
     * @link http://www.php.net/manual/en/mongocollection.group.php
563
     * @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.
564
     * @param array $initial Initial value of the aggregation counter object.
565
     * @param MongoCode $reduce A function that aggregates (reduces) the objects iterated.
566
     * @param array $condition An condition that must be true for a row to be considered.
567
     * @return array
568
     */
569
    public function group($keys, array $initial, $reduce, array $condition = [])
570
    {
571
        if (is_string($reduce)) {
572
            $reduce = new MongoCode($reduce);
573
        }
574
        if ( ! $reduce instanceof MongoCode) {
575
            throw new \InvalidArgumentExcption('reduce parameter should be a string or MongoCode instance.');
576
        }
577
        $command = [
578
            'group' => [
579
                'ns' => $this->name,
580
                '$reduce' => (string)$reduce,
581
                'initial' => $initial,
582
                'cond' => $condition,
583
            ],
584
        ];
585
586
        if ($keys instanceof MongoCode) {
587
            $command['group']['$keyf'] = (string)$keys;
588
        } else {
589
            $command['group']['key'] = $keys;
590
        }
591
        if (array_key_exists('condition', $condition)) {
592
            $command['group']['cond'] = $condition['condition'];
593
        }
594
        if (array_key_exists('finalize', $condition)) {
595
            if ($condition['finalize'] instanceof MongoCode) {
596
                $condition['finalize'] = (string)$condition['finalize'];
597
            }
598
            $command['group']['finalize'] = $condition['finalize'];
599
        }
600
601
        return $this->db->command($command, [], $hash);
602
    }
603
604
    /**
605
     * Returns an array of cursors to iterator over a full collection in parallel
606
     *
607
     * @link http://www.php.net/manual/en/mongocollection.parallelcollectionscan.php
608
     * @param int $num_cursors The number of cursors to request from the server. Please note, that the server can return less cursors than you requested.
609
     * @return MongoCommandCursor[]
610
     */
611
    public function parallelCollectionScan($num_cursors)
0 ignored issues
show
Unused Code introduced by
The parameter $num_cursors 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...
612
    {
613
        $this->notImplemented();
614
    }
615
616
    protected function notImplemented()
617
    {
618
        throw new \Exception('Not implemented');
619
    }
620
621
    /**
622
     * @return \MongoDB\Collection
623
     */
624 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...
625
    {
626
        $options = [
627
            'readPreference' => $this->readPreference,
628
            'writeConcern' => $this->writeConcern,
629
        ];
630
631
        if ($this->collection === null) {
632
            $this->collection = $this->db->getDb()->selectCollection($this->name, $options);
633
        } else {
634
            $this->collection = $this->collection->withOptions($options);
635
        }
636
    }
637
}
638
639