Index   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 83
c 5
b 0
f 0
dl 0
loc 146
ccs 0
cts 100
cp 0
rs 10
wmc 22

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndexes() 0 20 3
A getType() 0 13 3
A setIndexes() 0 27 4
C getIndexDetails() 0 51 12
1
<?php
2
3
namespace CodexShaper\DBM\Database\Drivers\MongoDB;
4
5
use CodexShaper\DBM\Database\Drivers\MongoDB\Traits\IndexTrait;
6
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...
7
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...
8
9
class Index
10
{
11
    use IndexTrait;
12
13
    /**
14
     * Get Indexes.
15
     *
16
     * @return  array
17
     */
18
    public static function getIndexes(Collection $collection)
19
    {
20
        $listIndexes = $collection->listIndexes();
21
        $indexes = [];
22
        foreach ($listIndexes as $index) {
23
            $indexes[] = [
24
                'name' => $index->getName(),
25
                'oldName' => $index->getName(),
26
                'columns' => array_keys($index->getKey()),
27
                'type' => static::getType($index),
28
                'isPrimary' => false,
29
                'isUnique' => $index->isUnique(),
30
                'isComposite' => (count($index->getKey()) > 1) ? true : false,
31
                'flags' => [],
32
                'options' => [],
33
                'namespace' => $index->getNamespace(),
34
            ];
35
        }
36
37
        return $indexes;
38
    }
39
40
    /**
41
     * Get index type.
42
     *
43
     * @return  string
44
     */
45
    public static function getType(IndexInfo $index)
46
    {
47
        $type = static::getCommonType($index);
48
49
        if (empty($type)) {
50
            $type = static::getSpecialType($index);
51
        }
52
53
        if (empty($type)) {
54
            $type = static::getDefaultType($index);
55
        }
56
57
        return $type;
58
    }
59
60
    /**
61
     * Set Indexes.
62
     *
63
     * @param   \MongoDB\Collection     $collection
64
     * @param   array   $indexes
65
     *
66
     * @return  bool
67
     */
68
    public static function setIndexes(Collection $collection, $indexes)
69
    {
70
        $collection->dropIndexes();
71
72
        foreach ($indexes as $index) {
73
            $columns = $index['columns'];
74
            $name = $index['name'];
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
75
            $type = $index['type'];
76
77
            foreach ($columns as $column) {
78
                if ($column == '_id') {
79
                    continue;
80
                }
81
82
                $indexDetails = static::getIndexDetails($type);
83
                $indexType = $indexDetails['type'];
84
                $options = $indexDetails['options'];
85
86
                $options['name'] = strtolower($collection->getCollectionName().'_'.$column.'_'.$type);
87
88
                $options['ns'] = $collection->getNamespace();
89
90
                $collection->createIndex([$column => $indexType], $options);
91
            }
92
        }
93
94
        return true;
95
    }
96
97
    /**
98
     * Get Index Details.
99
     *
100
     * @param   string   $type
101
     *
102
     * @return  array
103
     */
104
    public static function getIndexDetails($type)
105
    {
106
        $indexType = 1;
107
        $options = [];
108
109
        switch ($type) {
110
111
            case 'TEXT':
112
                $indexType = 'text';
113
                break;
114
            case 'INDEX':
115
                $indexType = 1;
116
                break;
117
            case 'UNIQUE':
118
                $indexType = 1;
119
                $options['unique'] = true;
120
                break;
121
            case 'UNIQUE_DESC':
122
                $indexType = -1;
123
                $options['unique'] = true;
124
                break;
125
            case 'TTL':
126
                $indexType = 1;
127
                $options['expireAfterSeconds'] = 3600;
128
                break;
129
            case 'SPARSE':
130
                $indexType = 1;
131
                $options['sparse'] = true;
132
                break;
133
            case 'SPARSE_DESC':
134
                $indexType = -1;
135
                $options['sparse'] = true;
136
                break;
137
            case 'SPARSE_UNIQUE':
138
                $indexType = 1;
139
                $options['sparse'] = true;
140
                $options['unique'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
141
            case 'SPARSE_UNIQUE_DESC':
142
                $indexType = -1;
143
                $options['sparse'] = true;
144
                $options['unique'] = true;
145
                break;
146
            case 'ASC':
147
                $indexType = 1;
148
                break;
149
            case 'DESC':
150
                $indexType = -1;
151
                break;
152
        }
153
154
        return ['type' => $indexType, 'options' => $options];
155
    }
156
}
157