MongoIndex::__construct()   C
last analyzed

Complexity

Conditions 16
Paths 8

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
cc 16
eloc 24
nc 8
nop 3
dl 0
loc 40
ccs 0
cts 40
cp 0
crap 272
rs 5.0151
c 0
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
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur\Db;
22
23
/**
24
 * A MongoDB Index definition
25
 */
26
class MongoIndex
27
{
28
    /**
29
     * @var array<string,mixed>
30
     */
31
    private $values;
32
33
    private const BOOL_FIELDS = ['background', 'unique', 'sparse'];
34
    private const FLOAT_FIELDS = ['min', 'max', 'bucketSize'];
35
    private const INT_FIELDS = ['bits', '2dsphereIndexVersion', 'textIndexVersion', 'expireAfterSeconds'];
36
    private const STRING_FIELDS = ['default_language', 'language_override'];
37
    private const DOCUMENT_FIELDS = ['partialFilterExpression', 'storageEngine', 'weights'];
38
39
    /**
40
     * Creates an immutable MongoDB Index creation definition
41
     *
42
     * @param $keys - The key definition
43
     * @param $name - Optional. The index name.
44
     * @param $options - Optional. Any index creation options.
45
     * @see https://docs.mongodb.com/manual/reference/command/createIndexes/
46
     */
47
    public function __construct(iterable $keys, string $name = null, ?iterable $options = null)
48
    {
49
        $values = [
50
            'key' => is_array($keys) ? $keys : iterator_to_array($keys, true)
51
        ];
52
        if ($name !== null) {
53
            $values['name'] = $name;
54
        }
55
        if ($options) {
56
            foreach (self::BOOL_FIELDS as $v) {
57
                if (array_key_exists($v, $options)) {
58
                    $values[$v] = (bool) $options[$v];
59
                }
60
            }
61
            foreach (self::FLOAT_FIELDS as $v) {
62
                if (array_key_exists($v, $options)) {
63
                    $values[$v] = (float) $options[$v];
64
                }
65
            }
66
            foreach (self::INT_FIELDS as $v) {
67
                if (array_key_exists($v, $options)) {
68
                    $values[$v] = (int) $options[$v];
69
                }
70
            }
71
            foreach (self::STRING_FIELDS as $v) {
72
                if (array_key_exists($v, $options)) {
73
                    $values[$v] = (string) $options[$v];
74
                }
75
            }
76
            foreach (self::DOCUMENT_FIELDS as $v) {
77
                if (array_key_exists($v, $options)) {
78
                    $doc = $options[$v];
79
                    if (is_iterable($doc)) {
80
                        $values[$v] = is_array($doc) ? $doc : iterator_to_array($doc, true);
81
                    }
82
                }
83
            }
84
        }
85
        $this->values = $values;
86
    }
87
88
    /**
89
     * Gets the array version of this index.
90
     *
91
     * @return array<string,mixed> The array version
92
     */
93
    public function toArray(): array
94
    {
95
        return $this->values;
96
    }
97
}
98