Completed
Push — master ( 652a71...1ff4bb )
by CodexShaper
04:35
created

Index::checkDescending()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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