Passed
Branch next (97e380)
by Bas
03:07
created

Indexes::executeDropIndexCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 8
cp 0.75
rs 10
cc 2
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Schema\Concerns;
4
5
use ArangoDBClient\Exception;
0 ignored issues
show
Bug introduced by
The type ArangoDBClient\Exception 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...
6
use Illuminate\Support\Fluent;
7
8
trait Indexes
9
{
10
    /**
11
     * Add a new index command to the blueprint.
12
     *
13
     * @param string            $type
14
     * @param string|array|null $columns
15
     * @param $name
16
     * @param array $indexOptions
17
     *
18
     * @return Fluent
19
     */
20 7
    protected function indexCommand($type = '', $columns = null, $name = null, $indexOptions = [])
21
    {
22 7
        if ($type == '') {
23
            $type = $this->mapIndexAlgorithm('persistent');
24
        }
25
26 7
        if ($columns === null) {
27 3
            $columns = end($this->columns);
28
        }
29
30 7
        if (is_string($columns)) {
31 3
            $columns = [$columns];
32
        }
33
34 7
        $indexOptions['name'] = $name ?: $this->createIndexName($type, $columns, $indexOptions);
0 ignored issues
show
Bug introduced by
It seems like $columns can also be of type null and string; however, parameter $columns of LaravelFreelancerNL\Aran...exes::createIndexName() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $indexOptions['name'] = $name ?: $this->createIndexName($type, /** @scrutinizer ignore-type */ $columns, $indexOptions);
Loading history...
35
36 7
        return $this->addCommand('index', compact('type', 'columns', 'indexOptions'));
0 ignored issues
show
Bug introduced by
It seems like addCommand() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        return $this->/** @scrutinizer ignore-call */ addCommand('index', compact('type', 'columns', 'indexOptions'));
Loading history...
37
    }
38
39
    /**
40
     * Specify an index for the table.
41
     *
42
     * @param string|array $columns
43
     * @param string       $name
44
     * @param string|null  $algorithm
45
     *
46
     * @return Fluent
47
     */
48 7
    public function index($columns = null, $name = null, $algorithm = null)
49
    {
50 7
        $type = $this->mapIndexAlgorithm($algorithm);
51
52 7
        return $this->indexCommand($type, $columns, $name);
53
    }
54
55
    /**
56
     * Create a hash index for fast exact matching.
57
     *
58
     * @param null  $columns
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $columns is correct as it would always require null to be passed?
Loading history...
59
     * @param array $indexOptions
60
     *
61
     * @return Fluent
62
     */
63
    public function hashIndex($columns = null, $indexOptions = [])
64
    {
65
        return $this->indexCommand('hash', $columns, $indexOptions);
66
    }
67
68
    /**
69
     * @param null|string $column
70
     * @param $name
71
     * @param array $indexOptions
72
     *
73
     * @return Fluent
74
     */
75
    public function fulltextIndex($column = null, $name = null, $indexOptions = [])
76
    {
77
        return $this->indexCommand('fulltext', $column, $name, $indexOptions);
78
    }
79
80
    /**
81
     *  Specify a spatial index for the table.
82
     *
83
     * @param $columns
84
     * @param null  $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
85
     * @param array $indexOptions
86
     *
87
     * @return Fluent
88
     */
89
    public function geoIndex($columns, $name = null, $indexOptions = [])
90
    {
91
        return $this->indexCommand('geo', $columns, $name, $indexOptions);
92
    }
93
94
    /**
95
     * Specify a spatial index for the table.
96
     *
97
     * @param string|array $columns
98
     * @param string       $name
99
     *
100
     * @return Fluent
101
     */
102
    public function spatialIndex($columns, $name = null)
103
    {
104
        return $this->geoIndex($columns, $name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string; however, parameter $name of LaravelFreelancerNL\Aran...rns\Indexes::geoIndex() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
        return $this->geoIndex($columns, /** @scrutinizer ignore-type */ $name);
Loading history...
105
    }
106
107
    /**
108
     * @param $columns
109
     * @param string|null $name
110
     * @param array       $indexOptions
111
     *
112
     * @return Fluent
113
     */
114
    public function skiplistIndex($columns, $name = null, $indexOptions = [])
115
    {
116
        return $this->indexCommand('skiplist', $columns, $name, $indexOptions);
117
    }
118
119
    public function persistentIndex($columns, $name = null, $indexOptions = [])
120
    {
121
        return $this->indexCommand('persistent', $columns, $name, $indexOptions);
122
    }
123
124
    /**
125
     * Create a TTL index for the table.
126
     *
127
     * @param $columns
128
     * @param null  $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
129
     * @param array $indexOptions
130
     *
131
     * @return Fluent
132
     */
133
    public function ttlIndex($columns, $name = null, $indexOptions = [])
134
    {
135
        return $this->indexCommand('ttl', $columns, $name, $indexOptions);
136
    }
137
138
    /**
139
     * Specify a unique index for the table.
140
     *
141
     * @param string|array $columns
142
     * @param string       $name
143
     * @param string|null  $algorithm
144
     *
145
     * @return Fluent
146
     */
147 1
    public function unique($columns = null, $name = null, $algorithm = null)
148
    {
149 1
        $type = $this->mapIndexAlgorithm($algorithm);
150
151 1
        $indexOptions = [];
152 1
        $indexOptions['unique'] = true;
153
154 1
        return $this->indexCommand($type, $columns, $name, $indexOptions);
155
    }
156
157
    /**
158
     * @param $command
159
     */
160 7
    public function executeIndexCommand($command)
161
    {
162 7
        if ($this->connection->pretending()) {
163
            $this->connection->logQuery('/* ' . $command->explanation . " */\n", []);
164
165
            return;
166
        }
167
168
        $options = [
169 7
            'type'    => $command->type,
170 7
            'fields'  => $command->columns,
171 7
            'unique'  => $command->unique,
172 7
            'options' => $command->indexOptions,
173
        ];
174
175 7
        if (isset($command->indexOptions) && is_array($command->indexOptions)) {
176 7
            $options = array_merge($options, $command->indexOptions);
177
        }
178
179 7
        $this->schemaManager->createIndex($this->table, $options);
180 7
    }
181
182
    /**
183
     * Indicate that the given index should be dropped.
184
     *
185
     * @param $name
186
     *
187
     * @return Fluent
188
     */
189 3
    public function dropIndex($name)
190
    {
191 3
        $parameters = [];
192 3
        $parameters['name'] = 'dropIndex';
193 3
        $parameters['index'] = $name;
194 3
        $parameters['explanation'] = "Drop the '" . $name . "' index on the {$this->table} table.";
195 3
        $parameters['handler'] = 'collection';
196
197 3
        return $this->addCommand('dropIndex', $parameters);
198
    }
199
200
    /**
201
     * Drop the index by first getting all the indexes on the table; then selecting the matching one
202
     * by name.
203
     *
204
     * @param $command
205
     */
206 3
    public function executeDropIndexCommand($command)
207
    {
208 3
        if ($this->connection->pretending()) {
209
            $this->connection->logQuery('/* ' . $command->explanation . " */\n", []);
210
211
            return;
212
        }
213 3
        $indexes = $this->schemaManager->getIndexes($this->table);
214 3
        $arrayIndex = array_search($command->index, array_column($indexes, 'name'), true);
215 3
        $indexId = $indexes[$arrayIndex]['id'];
216
217 3
        $this->schemaManager->deleteIndex($indexId);
218 3
    }
219
220
    /**
221
     * @param string|null $algorithm
222
     *
223
     * @return mixed|string
224
     */
225 7
    protected function mapIndexAlgorithm($algorithm)
226
    {
227
        $algorithmConversion = [
228 7
            'HASH'  => 'hash',
229
            'BTREE' => 'persistent',
230
            'RTREE' => 'geo',
231
            'TTL'   => 'ttl',
232
        ];
233 7
        $algorithm = strtoupper($algorithm);
0 ignored issues
show
Bug introduced by
It seems like $algorithm can also be of type null; however, parameter $string of strtoupper() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

233
        $algorithm = strtoupper(/** @scrutinizer ignore-type */ $algorithm);
Loading history...
234
235 7
        return (isset($algorithmConversion[$algorithm])) ? $algorithmConversion[$algorithm] : 'persistent';
236
    }
237
238
    /**
239
     * Create a default index name for the table.
240
     *
241
     * @param string $type
242
     * @param array  $columns
243
     * @param array  $options
244
     *
245
     * @return string
246
     */
247 9
    public function createIndexName($type, array $columns, array $options = [])
248
    {
249 9
        $nameParts = [];
250 9
        $nameParts[] = $this->prefix . $this->table;
251 9
        $nameParts = array_merge($nameParts, $columns);
252 9
        $nameParts[] = $type;
253 9
        $nameParts = array_merge($nameParts, array_keys($options));
254 9
        array_filter($nameParts);
255
256 9
        $index = strtolower(implode('_', $nameParts));
257 9
        $index = preg_replace("/\[\*+\]+/", '_array', $index);
258
259 9
        return preg_replace('/[^A-Za-z0-9]+/', '_', $index);
260
    }
261
}
262