Passed
Push — master ( 0ed784...b28e07 )
by CodexShaper
14:16
created

Index::getType()   D

Complexity

Conditions 24
Paths 14

Size

Total Lines 34
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 24
eloc 32
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 34
rs 4.1666

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