Passed
Push — master ( 1ff4bb...713349 )
by CodexShaper
05:24
created

Index::getType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 6
rs 10
1
<?php
2
3
namespace CodexShaper\DBM\Database\Drivers\MongoDB;
4
5
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...
6
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...
7
8
class Index
9
{
10
    public static function getIndexes(Collection $collection)
11
    {
12
        $listIndexes = $collection->listIndexes();
13
        $indexes     = [];
14
        foreach ($listIndexes as $index) {
15
            $indexes[] = [
16
                "name"        => $index->getName(),
17
                "oldName"     => $index->getName(),
18
                "columns"     => array_keys($index->getKey()),
19
                "type"        => static::getType($index),
20
                "isPrimary"   => false,
21
                "isUnique"    => $index->isUnique(),
22
                "isComposite" => (count($index->getKey()) > 1) ? true : false,
23
                "flags"       => [],
24
                "options"     => [],
25
                "namespace"   => $index->getNamespace(),
26
            ];
27
        }
28
29
        return $indexes;
30
    }
31
32
    public static function getType(IndexInfo $index)
33
    {
34
        $type = static::getCommonType($index);
35
36
        if (empty($type)) {
37
            $type = static::getSpecialType($index);
38
        }
39
40
        return $type;
41
    }
42
43
    protected static function getCommonType(IndexInfo $index)
44
    {
45
        $type = "";
46
47
        if ($index->isText()) {
48
            $type = "TEXT";
49
        }
50
51
        if ($index->is2dSphere()) {
52
            $type = "2DSPARSE";
53
        }
54
55
        if ($index->isTtl()) {
56
            $type = "TTL";
57
        }
58
59
        if ($index->isGeoHaystack()) {
60
            $type = "GEOHAYSTACK";
61
        }
62
63
        if ($type === "") {
64
            $type = static::getDefaultType($index);
65
        }
66
67
        return $type;
68
    }
69
70
    protected static function getSpecialType(IndexInfo $index)
71
    {
72
        $type = "";
73
74
        if ($index->isUnique() && !$index->isSparse() && !static::checkDescending($index)) {
75
            $type = "UNIQUE";
76
        }
77
78
        if ($index->isUnique() && !$index->isSparse() && static::checkDescending($index)) {
79
            $type = "UNIQUE_DESC";
80
        }
81
82
        if ($index->isSparse() && !static::checkDescending($index)) {
83
            $type = "SPARSE";
84
        }
85
86
        if ($index->isSparse() && $index->isUnique() && !static::checkDescending($index)) {
87
            $type = "SPARSE_UNIQUE";
88
        }
89
90
        if ($index->isSparse() && $index->isUnique() && static::checkDescending($index)) {
91
            $type = "SPARSE_UNIQUE_DESC";
92
        }
93
94
        if ($index->isSparse() && static::checkDescending($index)) {
95
            $type = "SPARSE_DESC";
96
        }
97
98
        return $type;
99
    }
100
101
    protected static function getDefaultType(IndexInfo $index)
102
    {
103
        $name     = $index->getName();
104
        $partials = explode("_", $name);
105
        $type     = end($partials);
106
107
        if ($type == 'asc') {
108
            return "ASC";
109
        }
110
111
        if ($type == 'index') {
112
            return "INDEX";
113
        }
114
115
        if ($type == 'desc') {
116
            return "DESC";
117
        }
118
119
        return "";
120
    }
121
122
    protected static function checkDescending($index)
123
    {
124
        $keys = $index->getKey();
125
126
        foreach ($keys as $key => $value) {
127
            if ($value == -1) {
128
                return true;
129
            }
130
        }
131
132
        return false;
133
    }
134
135
    public static function setIndexes(Collection $collection, $indexes)
136
    {
137
138
        $collection->dropIndexes();
139
140
        foreach ($indexes as $index) {
141
            $columns = $index['columns'];
142
            $name    = $index['name'];
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
143
            $type    = $index['type'];
144
145
            foreach ($columns as $column) {
146
                if ($column == '_id') {
147
                    continue;
148
                }
149
150
                switch ($type) {
151
152
                    case 'TEXT':
153
                        $indexType = 'text';
154
                        break;
155
                    case 'INDEX':
156
                        $indexType = 1;
157
                        break;
158
                    case 'UNIQUE':
159
                        $indexType         = 1;
160
                        $options['unique'] = true;
161
                        break;
162
                    case 'UNIQUE_DESC':
163
                        $indexType         = -1;
164
                        $options['unique'] = true;
165
                        break;
166
                    case 'TTL':
167
                        $indexType                     = 1;
168
                        $options['expireAfterSeconds'] = 3600;
169
                        break;
170
                    case 'SPARSE':
171
                        $indexType         = 1;
172
                        $options['sparse'] = true;
173
                        break;
174
                    case 'SPARSE_DESC':
175
                        $indexType         = -1;
176
                        $options['sparse'] = true;
177
                        break;
178
                    case 'SPARSE_UNIQUE':
179
                        $indexType         = 1;
180
                        $options['sparse'] = true;
181
                        $options['unique'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
182
                    case 'SPARSE_UNIQUE_DESC':
183
                        $indexType         = -1;
184
                        $options['sparse'] = true;
185
                        $options['unique'] = true;
186
                        break;
187
                    case 'ASC':
188
                        $indexType = 1;
189
                        break;
190
                    case 'DESC':
191
                        $indexType = -1;
192
                        break;
193
                    default:
194
                        $indexType = 1;
195
                        break;
196
                }
197
198
                $options['name'] = strtolower($collection->getCollectionName() . "_" . $column . "_" . $type);
199
200
                $options['ns'] = $collection->getNamespace();
201
202
                $collection->createIndex([$column => $indexType], $options);
203
            }
204
        }
205
206
        return true;
207
    }
208
}
209