Completed
Push — master ( 97ec88...d41805 )
by CodexShaper
04:29
created

Index::getIndexes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 20
ccs 0
cts 19
cp 0
crap 12
rs 9.7666
1
<?php
2
3
namespace CodexShaper\DBM\Database\Drivers\MongoDB;
4
5
use CodexShaper\DBM\Database\Drivers\MongoDB\Traits\IndexTrait;
6
use MongoDB\Collection;
0 ignored issues
show
Bug introduced by
The type MongoDB\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, CodexShaper\DBM\Database...vers\MongoDB\Collection. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use MongoDB\Model\IndexInfo;
0 ignored issues
show
Bug introduced by
The type MongoDB\Model\IndexInfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class Index
10
{
11
    use IndexTrait;
12
13
    public static function getIndexes(Collection $collection)
14
    {
15
        $listIndexes = $collection->listIndexes();
16
        $indexes     = [];
17
        foreach ($listIndexes as $index) {
18
            $indexes[] = [
19
                "name"        => $index->getName(),
20
                "oldName"     => $index->getName(),
21
                "columns"     => array_keys($index->getKey()),
22
                "type"        => static::getType($index),
23
                "isPrimary"   => false,
24
                "isUnique"    => $index->isUnique(),
25
                "isComposite" => (count($index->getKey()) > 1) ? true : false,
26
                "flags"       => [],
27
                "options"     => [],
28
                "namespace"   => $index->getNamespace(),
29
            ];
30
        }
31
32
        return $indexes;
33
    }
34
35
    public static function getType(IndexInfo $index)
36
    {
37
        $type = static::getCommonType($index);
38
39
        if (empty($type)) {
40
            $type = static::getSpecialType($index);
41
        }
42
43
        if (empty($type)) {
44
            $type = static::getDefaultType($index);
45
        }
46
47
        return $type;
48
    }
49
50
    public static function setIndexes(Collection $collection, $indexes)
51
    {
52
53
        $collection->dropIndexes();
54
55
        foreach ($indexes as $index) {
56
            $columns = $index['columns'];
57
            $name    = $index['name'];
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
58
            $type    = $index['type'];
59
60
            foreach ($columns as $column) {
61
                if ($column == '_id') {
62
                    continue;
63
                }
64
65
                $indexDetails = static::getIndexDetails($type);
66
                $indexType    = $indexDetails['type'];
67
                $options      = $indexDetails['options'];
68
69
                $options['name'] = strtolower($collection->getCollectionName() . "_" . $column . "_" . $type);
70
71
                $options['ns'] = $collection->getNamespace();
72
73
                $collection->createIndex([$column => $indexType], $options);
74
            }
75
        }
76
77
        return true;
78
    }
79
80
    public static function getIndexDetails($type)
81
    {
82
        $indexType = 1;
83
        $options   = [];
84
85
        switch ($type) {
86
87
            case 'TEXT':
88
                $indexType = 'text';
89
                break;
90
            case 'INDEX':
91
                $indexType = 1;
92
                break;
93
            case 'UNIQUE':
94
                $indexType         = 1;
95
                $options['unique'] = true;
96
                break;
97
            case 'UNIQUE_DESC':
98
                $indexType         = -1;
99
                $options['unique'] = true;
100
                break;
101
            case 'TTL':
102
                $indexType                     = 1;
103
                $options['expireAfterSeconds'] = 3600;
104
                break;
105
            case 'SPARSE':
106
                $indexType         = 1;
107
                $options['sparse'] = true;
108
                break;
109
            case 'SPARSE_DESC':
110
                $indexType         = -1;
111
                $options['sparse'] = true;
112
                break;
113
            case 'SPARSE_UNIQUE':
114
                $indexType         = 1;
115
                $options['sparse'] = true;
116
                $options['unique'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
117
            case 'SPARSE_UNIQUE_DESC':
118
                $indexType         = -1;
119
                $options['sparse'] = true;
120
                $options['unique'] = true;
121
                break;
122
            case 'ASC':
123
                $indexType = 1;
124
                break;
125
            case 'DESC':
126
                $indexType = -1;
127
                break;
128
        }
129
130
        return ["type" => $indexType, "options" => $options];
131
    }
132
}
133