Completed
Push — master ( b252c9...97ec88 )
by CodexShaper
05:03
created

Index::checkSparseUnique()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 1
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 20
rs 10
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
    public static function getIndexes(Collection $collection)
14
    {
15
        $listIndexes = $collection->listIndexes();
16
        $indexes     = [];
17
        foreach ($listIndexes as $index) {
18
            $indexes[] = [
19
                "name"        => $index->getName(),
20
                "oldName"     => $index->getName(),
21
                "columns"     => array_keys($index->getKey()),
22
                "type"        => static::getType($index),
23
                "isPrimary"   => false,
24
                "isUnique"    => $index->isUnique(),
25
                "isComposite" => (count($index->getKey()) > 1) ? true : false,
26
                "flags"       => [],
27
                "options"     => [],
28
                "namespace"   => $index->getNamespace(),
29
            ];
30
        }
31
32
        return $indexes;
33
    }
34
35
    public static function getType(IndexInfo $index)
36
    {
37
        $type = static::getCommonType($index);
38
39
        if (empty($type)) {
40
            $type = static::getSpecialType($index);
41
        }
42
43
        if (empty($type)) {
44
            $type = static::getDefaultType($index);
45
        }
46
47
        return $type;
48
    }
49
50
    public static function setIndexes(Collection $collection, $indexes)
51
    {
52
53
        $collection->dropIndexes();
54
55
        foreach ($indexes as $index) {
56
            $columns = $index['columns'];
57
            $name    = $index['name'];
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
58
            $type    = $index['type'];
59
60
            foreach ($columns as $column) {
61
                if ($column == '_id') {
62
                    continue;
63
                }
64
65
                switch ($type) {
66
67
                    case 'TEXT':
68
                        $indexType = 'text';
69
                        break;
70
                    case 'INDEX':
71
                        $indexType = 1;
72
                        break;
73
                    case 'UNIQUE':
74
                        $indexType         = 1;
75
                        $options['unique'] = true;
76
                        break;
77
                    case 'UNIQUE_DESC':
78
                        $indexType         = -1;
79
                        $options['unique'] = true;
80
                        break;
81
                    case 'TTL':
82
                        $indexType                     = 1;
83
                        $options['expireAfterSeconds'] = 3600;
84
                        break;
85
                    case 'SPARSE':
86
                        $indexType         = 1;
87
                        $options['sparse'] = true;
88
                        break;
89
                    case 'SPARSE_DESC':
90
                        $indexType         = -1;
91
                        $options['sparse'] = true;
92
                        break;
93
                    case 'SPARSE_UNIQUE':
94
                        $indexType         = 1;
95
                        $options['sparse'] = true;
96
                        $options['unique'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
97
                    case 'SPARSE_UNIQUE_DESC':
98
                        $indexType         = -1;
99
                        $options['sparse'] = true;
100
                        $options['unique'] = true;
101
                        break;
102
                    case 'ASC':
103
                        $indexType = 1;
104
                        break;
105
                    case 'DESC':
106
                        $indexType = -1;
107
                        break;
108
                    default:
109
                        $indexType = 1;
110
                        break;
111
                }
112
113
                $options['name'] = strtolower($collection->getCollectionName() . "_" . $column . "_" . $type);
114
115
                $options['ns'] = $collection->getNamespace();
116
117
                $collection->createIndex([$column => $indexType], $options);
118
            }
119
        }
120
121
        return true;
122
    }
123
}
124