Completed
Pull Request — master (#1749)
by Gabriel
11:34
created

SchemaManager::doGetDocumentIndexes()   C

Complexity

Conditions 15
Paths 7

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 15

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 52
ccs 33
cts 33
cp 1
rs 5.9385
cc 15
eloc 35
nc 7
nop 2
crap 15

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB;
6
7
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
8
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
9
use MongoDB\Driver\Exception\RuntimeException;
10
use MongoDB\Model\IndexInfo;
11
use function array_filter;
12
use function array_unique;
13
use function iterator_to_array;
14
use function strpos;
15
16
class SchemaManager
17
{
18
    /**
19
     * @var DocumentManager
20
     */
21
    protected $dm;
22
23
    /**
24
     *
25
     * @var ClassMetadataFactory
26
     */
27
    protected $metadataFactory;
28
29 1619
    public function __construct(DocumentManager $dm, ClassMetadataFactory $cmf)
30
    {
31 1619
        $this->dm = $dm;
32 1619
        $this->metadataFactory = $cmf;
33 1619
    }
34
35
    /**
36
     * Ensure indexes are created for all documents that can be loaded with the
37
     * metadata factory.
38
     *
39
     * @param int $timeout Timeout (ms) for acknowledged index creation
40
     */
41 1 View Code Duplication
    public function ensureIndexes($timeout = null)
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...
42
    {
43 1
        foreach ($this->metadataFactory->getAllMetadata() as $class) {
44 1
            if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
45 1
                continue;
46
            }
47 1
            $this->ensureDocumentIndexes($class->name, $timeout);
48
        }
49 1
    }
50
51
    /**
52
     * Ensure indexes exist for all mapped document classes.
53
     *
54
     * Indexes that exist in MongoDB but not the document metadata will be
55
     * deleted.
56
     *
57
     * @param int $timeout Timeout (ms) for acknowledged index creation
58
     */
59 View Code Duplication
    public function updateIndexes($timeout = null)
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...
60
    {
61
        foreach ($this->metadataFactory->getAllMetadata() as $class) {
62
            if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
63
                continue;
64
            }
65
            $this->updateDocumentIndexes($class->name, $timeout);
66
        }
67
    }
68
69
    /**
70
     * Ensure indexes exist for the mapped document class.
71
     *
72
     * Indexes that exist in MongoDB but not the document metadata will be
73
     * deleted.
74
     *
75
     * @param string $documentName
76
     * @param int    $timeout      Timeout (ms) for acknowledged index creation
77
     * @throws \InvalidArgumentException
78
     */
79 3
    public function updateDocumentIndexes($documentName, $timeout = null)
80
    {
81 3
        $class = $this->dm->getClassMetadata($documentName);
82
83 3
        if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
84
            throw new \InvalidArgumentException('Cannot update document indexes for mapped super classes, embedded documents or aggregation result documents.');
85
        }
86
87 3
        $documentIndexes = $this->getDocumentIndexes($documentName);
88 3
        $collection = $this->dm->getDocumentCollection($documentName);
89 3
        $mongoIndexes = iterator_to_array($collection->listIndexes());
90
91
        /* Determine which Mongo indexes should be deleted. Exclude the ID index
92
         * and those that are equivalent to any in the class metadata.
93
         */
94 3
        $self = $this;
95 3
        $mongoIndexes = array_filter($mongoIndexes, function (IndexInfo $mongoIndex) use ($documentIndexes, $self) {
96 1
            if ($mongoIndex['name'] === '_id_') {
97
                return false;
98
            }
99
100 1
            foreach ($documentIndexes as $documentIndex) {
101 1
                if ($self->isMongoIndexEquivalentToDocumentIndex($mongoIndex, $documentIndex)) {
102 1
                    return false;
103
                }
104
            }
105
106 1
            return true;
107 3
        });
108
109
        // Delete indexes that do not exist in class metadata
110 3
        foreach ($mongoIndexes as $mongoIndex) {
111 1
            if (! isset($mongoIndex['name'])) {
112
                continue;
113
            }
114
115 1
            $collection->dropIndex($mongoIndex['name']);
116
        }
117
118 3
        $this->ensureDocumentIndexes($documentName, $timeout);
119 3
    }
120
121
    /**
122
     * @param string $documentName
123
     * @return array
124
     */
125 19
    public function getDocumentIndexes($documentName)
126
    {
127 19
        $visited = [];
128 19
        return $this->doGetDocumentIndexes($documentName, $visited);
129
    }
130
131
    /**
132
     * @param string $documentName
133
     * @param array  $visited
134
     * @return array
135
     */
136 19
    private function doGetDocumentIndexes($documentName, array &$visited)
137
    {
138 19
        if (isset($visited[$documentName])) {
139 1
            return [];
140
        }
141
142 19
        $visited[$documentName] = true;
143
144 19
        $class = $this->dm->getClassMetadata($documentName);
145 19
        $indexes = $this->prepareIndexes($class);
146 19
        $embeddedDocumentIndexes = [];
147
148
        // Add indexes from embedded & referenced documents
149 19
        foreach ($class->fieldMappings as $fieldMapping) {
150 19
            if (isset($fieldMapping['embedded'])) {
151 3
                if (isset($fieldMapping['targetDocument'])) {
152 3
                    $possibleEmbeds = [$fieldMapping['targetDocument']];
153 2
                } elseif (isset($fieldMapping['discriminatorMap'])) {
154 1
                    $possibleEmbeds = array_unique($fieldMapping['discriminatorMap']);
155
                } else {
156 1
                    continue;
157
                }
158 3
                foreach ($possibleEmbeds as $embed) {
159 3
                    if (isset($embeddedDocumentIndexes[$embed])) {
160 2
                        $embeddedIndexes = $embeddedDocumentIndexes[$embed];
161
                    } else {
162 3
                        $embeddedIndexes = $this->doGetDocumentIndexes($embed, $visited);
163 3
                        $embeddedDocumentIndexes[$embed] = $embeddedIndexes;
164
                    }
165 3
                    foreach ($embeddedIndexes as $embeddedIndex) {
166 2
                        foreach ($embeddedIndex['keys'] as $key => $value) {
167 2
                            $embeddedIndex['keys'][$fieldMapping['name'] . '.' . $key] = $value;
168 2
                            unset($embeddedIndex['keys'][$key]);
169
                        }
170 3
                        $indexes[] = $embeddedIndex;
171
                    }
172
                }
173 19
            } elseif (isset($fieldMapping['reference']) && isset($fieldMapping['targetDocument'])) {
174 8
                foreach ($indexes as $idx => $index) {
175 8
                    $newKeys = [];
176 8
                    foreach ($index['keys'] as $key => $v) {
177 8
                        if ($key === $fieldMapping['name']) {
178 2
                            $key = ClassMetadata::getReferenceFieldName($fieldMapping['storeAs'], $key);
179
                        }
180 8
                        $newKeys[$key] = $v;
181
                    }
182 19
                    $indexes[$idx]['keys'] = $newKeys;
183
                }
184
            }
185
        }
186 19
        return $indexes;
187
    }
188
189
    /**
190
     * @return array
191
     */
192 19
    private function prepareIndexes(ClassMetadata $class)
193
    {
194 19
        $persister = $this->dm->getUnitOfWork()->getDocumentPersister($class->name);
195 19
        $indexes = $class->getIndexes();
196 19
        $newIndexes = [];
197
198 19
        foreach ($indexes as $index) {
199
            $newIndex = [
200 19
                'keys' => [],
201 19
                'options' => $index['options'],
202
            ];
203 19
            foreach ($index['keys'] as $key => $value) {
204 19
                $key = $persister->prepareFieldName($key);
205 19
                if ($class->hasField($key)) {
206 17
                    $mapping = $class->getFieldMapping($key);
207 17
                    $newIndex['keys'][$mapping['name']] = $value;
208
                } else {
209 19
                    $newIndex['keys'][$key] = $value;
210
                }
211
            }
212
213 19
            $newIndexes[] = $newIndex;
214
        }
215
216 19
        return $newIndexes;
217
    }
218
219
    /**
220
     * Ensure the given document's indexes are created.
221
     *
222
     * @param string $documentName
223
     * @param int    $timeout      Timeout (ms) for acknowledged index creation
224
     * @throws \InvalidArgumentException
225
     */
226 15
    public function ensureDocumentIndexes($documentName, $timeout = null)
227
    {
228 15
        $class = $this->dm->getClassMetadata($documentName);
229 15
        if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
230
            throw new \InvalidArgumentException('Cannot create document indexes for mapped super classes, embedded documents or query result documents.');
231
        }
232
233 15
        $indexes = $this->getDocumentIndexes($documentName);
234 15
        if ($indexes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $indexes 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...
235 15
            $collection = $this->dm->getDocumentCollection($class->name);
236 15
            foreach ($indexes as $index) {
237 15
                $keys = $index['keys'];
238 15
                $options = $index['options'];
239
240 15
                if (! isset($options['timeout']) && isset($timeout)) {
241 1
                    $options['timeout'] = $timeout;
242
                }
243
244 15
                $collection->createIndex($keys, $options);
245
            }
246
        }
247 15
    }
248
249
    /**
250
     * Delete indexes for all documents that can be loaded with the
251
     * metadata factory.
252
     */
253 1 View Code Duplication
    public function deleteIndexes()
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...
254
    {
255 1
        foreach ($this->metadataFactory->getAllMetadata() as $class) {
256 1
            if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
257 1
                continue;
258
            }
259 1
            $this->deleteDocumentIndexes($class->name);
260
        }
261 1
    }
262
263
    /**
264
     * Delete the given document's indexes.
265
     *
266
     * @param string $documentName
267
     * @throws \InvalidArgumentException
268
     */
269 2 View Code Duplication
    public function deleteDocumentIndexes($documentName)
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...
270
    {
271 2
        $class = $this->dm->getClassMetadata($documentName);
272 2
        if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
273
            throw new \InvalidArgumentException('Cannot delete document indexes for mapped super classes, embedded documents or query result documents.');
274
        }
275 2
        $this->dm->getDocumentCollection($documentName)->dropIndexes();
276 2
    }
277
278
    /**
279
     * Create all the mapped document collections in the metadata factory.
280
     */
281 1 View Code Duplication
    public function createCollections()
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...
282
    {
283 1
        foreach ($this->metadataFactory->getAllMetadata() as $class) {
284 1
            if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
285 1
                continue;
286
            }
287 1
            $this->createDocumentCollection($class->name);
288
        }
289 1
    }
290
291
    /**
292
     * Create the document collection for a mapped class.
293
     *
294
     * @param string $documentName
295
     * @throws \InvalidArgumentException
296
     */
297 4
    public function createDocumentCollection($documentName)
298
    {
299 4
        $class = $this->dm->getClassMetadata($documentName);
300
301 4
        if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
302
            throw new \InvalidArgumentException('Cannot create document collection for mapped super classes, embedded documents or query result documents.');
303
        }
304
305 4
        $this->dm->getDocumentDatabase($documentName)->createCollection(
306 4
            $class->getCollection(),
307
            [
308 4
                'capped' => $class->getCollectionCapped(),
309 4
                'size' => $class->getCollectionSize(),
310 4
                'max' => $class->getCollectionMax(),
311
            ]
312
        );
313 4
    }
314
315
    /**
316
     * Drop all the mapped document collections in the metadata factory.
317
     */
318 1 View Code Duplication
    public function dropCollections()
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...
319
    {
320 1
        foreach ($this->metadataFactory->getAllMetadata() as $class) {
321 1
            if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
322 1
                continue;
323
            }
324 1
            $this->dropDocumentCollection($class->name);
325
        }
326 1
    }
327
328
    /**
329
     * Drop the document collection for a mapped class.
330
     *
331
     * @param string $documentName
332
     * @throws \InvalidArgumentException
333
     */
334 4 View Code Duplication
    public function dropDocumentCollection($documentName)
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...
335
    {
336 4
        $class = $this->dm->getClassMetadata($documentName);
337 4
        if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
338
            throw new \InvalidArgumentException('Cannot delete document indexes for mapped super classes, embedded documents or query result documents.');
339
        }
340 4
        $this->dm->getDocumentCollection($documentName)->drop();
341 4
    }
342
343
    /**
344
     * Drop all the mapped document databases in the metadata factory.
345
     */
346 1 View Code Duplication
    public function dropDatabases()
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...
347
    {
348 1
        foreach ($this->metadataFactory->getAllMetadata() as $class) {
349 1
            if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
350 1
                continue;
351
            }
352 1
            $this->dropDocumentDatabase($class->name);
353
        }
354 1
    }
355
356
    /**
357
     * Drop the document database for a mapped class.
358
     *
359
     * @param string $documentName
360
     * @throws \InvalidArgumentException
361
     */
362 2 View Code Duplication
    public function dropDocumentDatabase($documentName)
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...
363
    {
364 2
        $class = $this->dm->getClassMetadata($documentName);
365 2
        if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
366
            throw new \InvalidArgumentException('Cannot drop document database for mapped super classes, embedded documents or query result documents.');
367
        }
368 2
        $this->dm->getDocumentDatabase($documentName)->drop();
369 2
    }
370
371
    /**
372
     * Determine if an index returned by MongoCollection::getIndexInfo() can be
373
     * considered equivalent to an index in class metadata.
374
     *
375
     * Indexes are considered different if:
376
     *
377
     *   (a) Key/direction pairs differ or are not in the same order
378
     *   (b) Sparse or unique options differ
379
     *   (c) Mongo index is unique without dropDups and mapped index is unique
380
     *       with dropDups
381
     *   (d) Geospatial options differ (bits, max, min)
382
     *   (e) The partialFilterExpression differs
383
     *
384
     * Regarding (c), the inverse case is not a reason to delete and
385
     * recreate the index, since dropDups only affects creation of
386
     * the unique index. Additionally, the background option is only
387
     * relevant to index creation and is not considered.
388
     *
389
     * @param array|IndexInfo $mongoIndex    Mongo index data.
390
     * @param array           $documentIndex Document index data.
391
     * @return bool True if the indexes are equivalent, otherwise false.
392
     */
393 30
    public function isMongoIndexEquivalentToDocumentIndex($mongoIndex, $documentIndex)
394
    {
395 30
        $documentIndexOptions = $documentIndex['options'];
396
397 30
        if ($mongoIndex['key'] !== $documentIndex['keys']) {
398 2
            return false;
399
        }
400
401 28
        if (empty($mongoIndex['sparse']) xor empty($documentIndexOptions['sparse'])) {
402 2
            return false;
403
        }
404
405 26
        if (empty($mongoIndex['unique']) xor empty($documentIndexOptions['unique'])) {
406 2
            return false;
407
        }
408
409 24
        if (! empty($mongoIndex['unique']) && empty($mongoIndex['dropDups']) &&
410 24
            ! empty($documentIndexOptions['unique']) && ! empty($documentIndexOptions['dropDups'])) {
411 1
            return false;
412
        }
413
414 23
        foreach (['bits', 'max', 'min'] as $option) {
415 23
            if (isset($mongoIndex[$option]) xor isset($documentIndexOptions[$option])) {
416 6
                return false;
417
            }
418
419 21
            if (isset($mongoIndex[$option]) && isset($documentIndexOptions[$option]) &&
420 21
                $mongoIndex[$option] !== $documentIndexOptions[$option]) {
421 21
                return false;
422
            }
423
        }
424
425 14
        if (empty($mongoIndex['partialFilterExpression']) xor empty($documentIndexOptions['partialFilterExpression'])) {
426 2
            return false;
427
        }
428
429 12
        if (isset($mongoIndex['partialFilterExpression']) && isset($documentIndexOptions['partialFilterExpression']) &&
430 12
            $mongoIndex['partialFilterExpression'] !== $documentIndexOptions['partialFilterExpression']) {
431 1
            return false;
432
        }
433
434 11
        return true;
435
    }
436
437
    /**
438
     * Ensure collections are sharded for all documents that can be loaded with the
439
     * metadata factory.
440
     *
441
     * @param array $indexOptions Options for `ensureIndex` command. It's performed on an existing collections
442
     *
443
     * @throws MongoDBException
444
     */
445
    public function ensureSharding(array $indexOptions = [])
446
    {
447
        foreach ($this->metadataFactory->getAllMetadata() as $class) {
448
            if ($class->isMappedSuperclass || ! $class->isSharded()) {
449
                continue;
450
            }
451
452
            $this->ensureDocumentSharding($class->name, $indexOptions);
453
        }
454
    }
455
456
    /**
457
     * Ensure sharding for collection by document name.
458
     *
459
     * @param string $documentName
460
     * @param array  $indexOptions Options for `ensureIndex` command. It's performed on an existing collections.
461
     *
462
     * @throws MongoDBException
463
     */
464 2
    public function ensureDocumentSharding($documentName, array $indexOptions = [])
465
    {
466 2
        $class = $this->dm->getClassMetadata($documentName);
467 2
        if (! $class->isSharded()) {
468
            return;
469
        }
470
471 2
        $this->enableShardingForDbByDocumentName($documentName);
472
473 2
        $try = 0;
474
        do {
475
            try {
476 2
                $result = $this->runShardCollectionCommand($documentName);
477 2
                $done = true;
478
479
                // Need to check error message because MongoDB 3.0 does not return a code for this error
480 2
                if (! (bool) $result['ok'] && strpos($result['errmsg'], 'please create an index that starts') !== false) {
481
                    // The proposed key is not returned when using mongo-php-adapter with ext-mongodb.
482
                    // See https://github.com/mongodb/mongo-php-driver/issues/296 for details
483
                    $key = $result['proposedKey'] ?? $this->dm->getClassMetadata($documentName)->getShardKey()['keys'];
484
485
                    $this->dm->getDocumentCollection($documentName)->ensureIndex($key, $indexOptions);
486 2
                    $done = false;
487
                }
488 1
            } catch (RuntimeException $e) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Exception\RuntimeException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
489 1
                if ($e->getCode() === 20 || $e->getCode() === 23 || $e->getMessage() === 'already sharded') {
490 1
                    return;
491
                }
492
493
                throw $e;
494
            }
495 2
        } while (! $done && $try < 2);
0 ignored issues
show
introduced by
It seems like the condition $try < 2 is always satisfied by any possible value of $try. Are you sure you do not have a deadlock here?
Loading history...
496
497
        // Starting with MongoDB 3.2, this command returns code 20 when a collection is already sharded.
498
        // For older MongoDB versions, check the error message
499 2
        if ((bool) $result['ok'] || (isset($result['code']) && $result['code'] === 20) || $result['errmsg'] === 'already sharded') {
500 2
            return;
501
        }
502
503
        throw MongoDBException::failedToEnsureDocumentSharding($documentName, $result['errmsg']);
504
    }
505
506
    /**
507
     * Enable sharding for database which contains documents with given name.
508
     *
509
     * @param string $documentName
510
     *
511
     * @throws MongoDBException
512
     */
513 2
    public function enableShardingForDbByDocumentName($documentName)
514
    {
515 2
        $dbName = $this->dm->getDocumentDatabase($documentName)->getDatabaseName();
516 2
        $adminDb = $this->dm->getClient()->selectDatabase('admin');
517
518
        try {
519 2
            $adminDb->command(['enableSharding' => $dbName]);
520 1
        } catch (RuntimeException $e) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Exception\RuntimeException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
521 1
            if ($e->getCode() !== 23 || $e->getMessage() === 'already enabled') {
522
                throw MongoDBException::failedToEnableSharding($dbName, $e->getMessage());
523
            }
524
        }
525 2
    }
526
527
    /**
528
     * @param string $documentName
529
     *
530
     * @return array
531
     */
532 2
    private function runShardCollectionCommand($documentName)
533
    {
534 2
        $class = $this->dm->getClassMetadata($documentName);
535 2
        $dbName = $this->dm->getDocumentDatabase($documentName)->getDatabaseName();
536 2
        $shardKey = $class->getShardKey();
537 2
        $adminDb = $this->dm->getClient()->selectDatabase('admin');
538
539 2
        $result = $adminDb->command(
540
            [
541 2
                'shardCollection' => $dbName . '.' . $class->getCollection(),
542 2
                'key'             => $shardKey['keys'],
543
            ]
544 2
        )->toArray()[0];
545
546 2
        return $result;
547
    }
548
}
549