Passed
Push — master ( 47ed60...652a71 )
by CodexShaper
04:59
created

Index::getType()   C

Complexity

Conditions 17
Paths 64

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 15
nc 64
nop 1
dl 0
loc 31
ccs 0
cts 23
cp 0
crap 306
rs 5.2166
c 1
b 0
f 0

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