Completed
Push — master ( c2eef9...47ed60 )
by CodexShaper
07:04 queued 02:29
created

Index::getDefaultType()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 18
ccs 0
cts 15
cp 0
crap 20
rs 9.9332
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
        if ($index->isText()) {
35
            return "TEXT";
36
        }
37
38
        if ($index->isUnique() && !$index->isSparse() && !static::checkDescending($index)) {
39
            return "UNIQUE";
40
        }
41
42
        if ($index->isUnique() && !$index->isSparse() && static::checkDescending($index)) {
43
            return "UNIQUE_DESC";
44
        }
45
46
        if ($index->isSparse() && !static::checkDescending($index)) {
47
            return "SPARSE";
48
        }
49
50
        if ($index->isSparse() && $index->isUnique() && !static::checkDescending($index)) {
51
            return "SPARSE_UNIQUE";
52
        }
53
54
        if ($index->isSparse() && $index->isUnique() && static::checkDescending($index)) {
55
            return "SPARSE_UNIQUE_DESC";
56
        }
57
58
        if ($index->isSparse() && static::checkDescending($index)) {
59
            return "SPARSE_DESC";
60
        }
61
62
        if ($index->is2dSphere()) {
63
            return "2DSPARSE";
64
        }
65
66
        if ($index->isTtl()) {
67
            return "TTL";
68
        }
69
70
        if ($index->isGeoHaystack()) {
71
            return "GEOHAYSTACK";
72
        }
73
74
        return static::getDefaultType($index);
75
    }
76
77
    protected static function getDefaultType(IndexInfo $index)
78
    {
79
        $name     = $index->getName();
80
        $partials = explode("_", $name);
81
        $type     = end($partials);
82
        if ($type == 'asc') {
83
            return "ASC";
84
        }
85
86
        if ($type == 'index') {
87
            return "INDEX";
88
        }
89
90
        if ($type == 'desc') {
91
            return "DESC";
92
        }
93
94
        return "";
95
    }
96
97
    protected static function checkDescending($index)
98
    {
99
        $keys = $index->getKey();
100
101
        foreach ($keys as $key => $value) {
102
            if ($value == -1) {
103
                return true;
104
            }
105
        }
106
107
        return false;
108
    }
109
110
    public static function setIndexes(Collection $collection, $indexes)
111
    {
112
113
        $collection->dropIndexes();
114
115
        foreach ($indexes as $index) {
116
            $columns = $index['columns'];
117
            $name    = $index['name'];
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
118
            $type    = $index['type'];
119
120
            foreach ($columns as $column) {
121
                if ($column == '_id') {
122
                    continue;
123
                }
124
125
                switch ($type) {
126
127
                    case 'TEXT':
128
                        $indexType = 'text';
129
                        break;
130
                    case 'INDEX':
131
                        $indexType = 1;
132
                        break;
133
                    case 'UNIQUE':
134
                        $indexType         = 1;
135
                        $options['unique'] = true;
136
                        break;
137
                    case 'UNIQUE_DESC':
138
                        $indexType         = -1;
139
                        $options['unique'] = true;
140
                        break;
141
                    case 'TTL':
142
                        $indexType                     = 1;
143
                        $options['expireAfterSeconds'] = 3600;
144
                        break;
145
                    case 'SPARSE':
146
                        $indexType         = 1;
147
                        $options['sparse'] = true;
148
                        break;
149
                    case 'SPARSE_DESC':
150
                        $indexType         = -1;
151
                        $options['sparse'] = true;
152
                        break;
153
                    case 'SPARSE_UNIQUE':
154
                        $indexType         = 1;
155
                        $options['sparse'] = true;
156
                        $options['unique'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
157
                    case 'SPARSE_UNIQUE_DESC':
158
                        $indexType         = -1;
159
                        $options['sparse'] = true;
160
                        $options['unique'] = true;
161
                        break;
162
                    case 'ASC':
163
                        $indexType = 1;
164
                        break;
165
                    case 'DESC':
166
                        $indexType = -1;
167
                        break;
168
                    default:
169
                        $indexType = 1;
170
                        break;
171
                }
172
173
                $options['name'] = strtolower($collection->getCollectionName() . "_" . $column . "_" . $type);
174
175
                $options['ns'] = $collection->getNamespace();
176
177
                $collection->createIndex([$column => $indexType], $options);
178
            }
179
        }
180
181
        return true;
182
    }
183
}
184